n8n/packages/nodes-base/nodes/Pushover/Pushover.node.ts
Omar Ajoue d6239d5bfb
Add full continue-on-fail support to all nodes (#1996)
* Update Compression node

* Update Crypto node

* Update DateTime node

* Update EditImage node

* Update EmailSend node

* Update ExecuteWorkflow node

* Update FTP node

* Update Function node

* Update FunctionItem node

* Update ExecuteCommand node

* Update OpenWeatherMap node

* Update ReadBinaryFile node

* Update ReadPdf node

* Update RssFeedRead node & add URL validation

* Update SpreadsheetFile node

* Update Switch node

* Update WriteBinaryFile node

* Update Xml node

* Update ActiveCampaign node

* Update Airtable node

* Update ApiTemplateIo node

* Update Asana node

* Update AwsLambda node

* Update AwsSns node

* Update AwsComprehend node

* Update AwsRekognition node

* Update AwsS3 node

* Fix Error item

* Update AwsSes node

* Update AwsSqs node

* Update Amqp node

* Update Bitly node

* Update Box node

* Update Brandfetch node

* Update CircleCi node

* Update Clearbit node

* Update ClickUp node

* Update Cockpit node

* Update CoinGecko node

* Update Contentful node

* Update ConvertKit node

* Update Cortex node

* Update CustomerIo node

* Update DeepL node

* Update Demio node

* Update Disqus node

* Update Drift node

* Update Dropbox node

* Update GetResponse node

* Refactor & Update Ghost node

* Update Github node

* Update Gitlab node

* Update GoogleAnalytics node

* Update GoogleBooks node

* Update GoogleCalendar node

* Update GoogleDrive node

* Update Gmail node

* Update GoogleSheets node

* Update GoogleSlides node

* Update GoogleTasks node

* Update Gotify node

* Update GraphQL node

* Update HackerNews node

* Update Harvest node

* Update HtmlExtract node

* Update Hubspot node

* Update Hunter node

* Update Intercom node

* Update Kafka node

* Refactor & update Line node

* Update LinkedIn node

* Update Mailchimp node

* Update Mandrill node

* Update Matrix node

* Update Mautic node

* Update Medium node

* Update MessageBird node

* Update Mindee node

* Update Mocean node

* Update MondayCom node

* Update MicrosoftExcel node

* Update MicrosoftOneDrive node

* Update MicrosoftOutlook node

* Update Affinity node

* Update Chargebee node

* Update Discourse node

* Update Freshdesk node

* Update YouTube node

* Update InvoiceNinja node

* Update MailerLite node

* Update Mailgun node

* Update Mailjet node

* Update Mattermost node

* Update Nasa node

* Update NextCloud node

* Update OpenThesaurus node

* Update Orbit node

* Update PagerDuty node

* Update PayPal node

* Update Peekalink node

* Update Phantombuster node

* Update PostHog node

* Update ProfitWell node

* Refactor & Update Pushbullet node

* Update QuickBooks node

* Update Raindrop node

* Update Reddit node

* Update Rocketchat node

* Update S3 node

* Update Salesforce node

* Update SendGrid node

* Update SentryIo node

* Update Shopify node

* Update Signl4 node

* Update Slack node

* Update Spontit node

* Update Spotify node

* Update Storyblok node

* Refactor & Update Strapi node

* Refactor & Update Strava node

* Update Taiga node

* Refactor & update Tapfiliate node

* Update Telegram node

* Update TheHive node

* Update Todoist node

* Update TravisCi node

* Update Trello node

* Update Twilio node

* Update Twist node

* Update Twitter node

* Update Uplead node

* Update UProc node

* Update Vero node

* Update Webflow node

* Update Wekan node

* Update Wordpress node

* Update Xero node

* Update Yourls node

* Update Zendesk node

* Update ZohoCrm node

* Refactor & Update Zoom node

* Update Zulip node

* Update Clockify node

* Update MongoDb node

* Update MySql node

* Update MicrosoftTeams node

* Update Stackby node

* Refactor Discourse node

* Support corner-case in Github node update

* Support corner-case in Gitlab node update

* Refactor & Update GoogleContacts node

* Refactor Mindee node

* Update Coda node

* Lint fixes

* Update Beeminder node

* Update Google Firebase RealtimeDatabase node

* Update HelpScout node

* Update Mailcheck node

* Update Paddle node

* Update Pipedrive node

* Update Pushover node

* Update Segment node

* Refactor & Update Vonage node

* Added new conditions to warnings on execute batch cmd

* Added keep only properties flag

* Fixed code for keep only props

* Added dependencies for image editing

Co-authored-by: dali <servfrdali@yahoo.fr>
2021-07-20 08:58:54 +02:00

404 lines
9.3 KiB
TypeScript

import {
BINARY_ENCODING,
IExecuteFunctions,
} from 'n8n-core';
import {
IBinaryData,
IBinaryKeyData,
IDataObject,
ILoadOptionsFunctions,
INodeExecutionData,
INodePropertyOptions,
INodeType,
INodeTypeDescription,
NodeOperationError,
} from 'n8n-workflow';
import {
pushoverApiRequest,
} from './GenericFunctions';
export class Pushover implements INodeType {
description: INodeTypeDescription = {
displayName: 'Pushover',
name: 'pushover',
icon: 'file:pushover.png',
group: ['input'],
version: 1,
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
description: 'Consume Pushover API',
defaults: {
name: 'Pushover',
color: '#4b9cea',
},
inputs: ['main'],
outputs: ['main'],
credentials: [
{
name: 'pushoverApi',
required: true,
},
],
properties: [
{
displayName: 'Resource',
name: 'resource',
type: 'options',
options: [
{
name: 'Message',
value: 'message',
},
],
default: 'message',
description: 'The resource to operate on.',
},
{
displayName: 'Operation',
name: 'operation',
type: 'options',
displayOptions: {
show: {
resource: [
'message',
],
},
},
options: [
{
name: 'Push',
value: 'push',
},
],
default: 'push',
description: 'The resource to operate on.',
},
{
displayName: 'User Key',
name: 'userKey',
type: 'string',
required: true,
displayOptions: {
show: {
resource: [
'message',
],
operation: [
'push',
],
},
},
default: '',
description: `The user/group key (not e-mail address) of your user (or you),<br>
viewable when logged into our <a href="https://pushover.net/" target="_blank">dashboard</a> (often referred to as USER_KEY in our <a href="https://support.pushover.net/i44-example-code-and-pushover-libraries" target="_blank"></a> and code examples)`,
},
{
displayName: 'Message',
name: 'message',
type: 'string',
required: true,
displayOptions: {
show: {
resource: [
'message',
],
operation: [
'push',
],
},
},
default: '',
description: `Your message`,
},
{
displayName: 'Priority',
name: 'priority',
type: 'options',
displayOptions: {
show: {
resource: [
'message',
],
operation: [
'push',
],
},
},
options: [
{
name: 'Lowest Priority',
value: -2,
},
{
name: 'Low Priority',
value: -1,
},
{
name: 'Normal Priority',
value: 0,
},
{
name: 'High Priority',
value: 1,
},
{
name: 'Emergency Priority',
value: 2,
},
],
default: -2,
description: `send as -2 to generate no notification/alert,<br>
-1 to always send as a quiet notification,<br>
1 to display as high-priority and bypass the user's quiet hours, or<br>
2 to also require confirmation from the user`,
},
{
displayName: 'Retry (seconds)',
name: 'retry',
type: 'number',
typeOptions: {
minValue: 0,
},
required: true,
displayOptions: {
show: {
resource: [
'message',
],
operation: [
'push',
],
priority: [
2,
],
},
},
default: 30,
description: `Specifies how often (in seconds) the Pushover servers will send the same notification to the user.<br>
This parameter must have a value of at least 30 seconds between retries.`,
},
{
displayName: 'Expire (seconds)',
name: 'expire',
type: 'number',
typeOptions: {
minValue: 0,
maxValue: 10800,
},
required: true,
displayOptions: {
show: {
resource: [
'message',
],
operation: [
'push',
],
priority: [
2,
],
},
},
default: 30,
description: `Specifies how many seconds your notification will continue to be retried for (every retry seconds)`,
},
{
displayName: 'Additional Fields',
name: 'additionalFields',
type: 'collection',
placeholder: 'Add Field',
displayOptions: {
show: {
resource: [
'message',
],
operation: [
'push',
],
},
},
default: {},
options: [
{
displayName: 'Attachment',
name: 'attachmentsUi',
placeholder: 'Add Attachments',
type: 'fixedCollection',
typeOptions: {
multipleValues: false,
},
options: [
{
name: 'attachmentsValues',
displayName: 'Attachment Property',
values: [
{
displayName: 'Binary Property',
name: 'binaryPropertyName',
type: 'string',
default: '',
placeholder: 'data',
description: 'Name of the binary properties which contain data which should be added to email as attachment',
},
],
},
],
default: '',
},
{
displayName: 'Device',
name: 'device',
type: 'string',
default: '',
description: `Your user's device name to send the message directly to that device,<br>
rather than all of the user's devices (multiple devices may be separated by a comma)`,
},
{
displayName: 'Sound',
name: 'sound',
type: 'options',
typeOptions: {
loadOptionsMethod: 'getSounds',
},
default: '',
description: `The name of one of the sounds supported by device clients to override the user's default sound choice`,
},
{
displayName: 'Title',
name: 'title',
type: 'string',
default: '',
description: `Your message's title, otherwise your app's name is used`,
},
{
displayName: 'Timestamp',
name: 'timestamp',
type: 'dateTime',
default: '',
description: `A Unix timestamp of your message's date and time to display to the user, rather than the time your message is received by our API`,
},
{
displayName: 'URL',
name: 'url',
type: 'string',
default: '',
description: `a supplementary URL to show with your message`,
},
{
displayName: 'URL Title',
name: 'url_title',
type: 'string',
default: '',
description: `A title for your supplementary URL, otherwise just the URL is shown`,
},
],
},
],
};
methods = {
loadOptions: {
async getSounds(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const { sounds } = await pushoverApiRequest.call(this, 'GET', '/sounds.json', {});
const returnData: INodePropertyOptions[] = [];
for (const key of Object.keys(sounds)) {
returnData.push({
name: sounds[key],
value: key,
});
}
return returnData;
},
},
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData();
const returnData: IDataObject[] = [];
const length = (items.length as unknown) as number;
const qs: IDataObject = {};
let responseData;
const resource = this.getNodeParameter('resource', 0) as string;
const operation = this.getNodeParameter('operation', 0) as string;
for (let i = 0; i < length; i++) {
try {
if (resource === 'message') {
if (operation === 'push') {
const userKey = this.getNodeParameter('userKey', i) as string;
const message = this.getNodeParameter('message', i) as string;
const priority = this.getNodeParameter('priority', i) as number;
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
const body: IDataObject = {
user: userKey,
message,
priority,
};
if (priority === 2) {
body.retry = this.getNodeParameter('retry', i) as number;
body.expire = this.getNodeParameter('expire', i) as number;
}
Object.assign(body, additionalFields);
if (body.attachmentsUi) {
const attachment = (body.attachmentsUi as IDataObject).attachmentsValues as IDataObject;
if (attachment) {
const binaryPropertyName = attachment.binaryPropertyName as string;
if (items[i].binary === undefined) {
throw new NodeOperationError(this.getNode(), 'No binary data exists on item!');
}
const item = items[i].binary as IBinaryKeyData;
const binaryData = item[binaryPropertyName] as IBinaryData;
if (binaryData === undefined) {
throw new NodeOperationError(this.getNode(), `No binary data property "${binaryPropertyName}" does not exists on item!`);
}
body.attachment = {
value: Buffer.from(binaryData.data, BINARY_ENCODING),
options: {
filename: binaryData.fileName,
},
};
delete body.attachmentsUi;
}
}
responseData = await pushoverApiRequest.call(
this,
'POST',
`/messages.json`,
body,
);
}
}
} catch (error) {
if (this.continueOnFail()) {
returnData.push({ error: error.message });
continue;
}
throw error;
}
}
if (Array.isArray(responseData)) {
returnData.push.apply(returnData, responseData as IDataObject[]);
} else if (responseData !== undefined) {
returnData.push(responseData as IDataObject);
}
return [this.helpers.returnJsonArray(returnData)];
}
}