2023-01-27 03:22:44 -08:00
import type {
2020-10-21 14:30:07 -07:00
IDataObject ,
2023-03-06 08:33:32 -08:00
IExecuteFunctions ,
2020-10-21 14:32:07 -07:00
ILoadOptionsFunctions ,
2020-10-21 14:30:07 -07:00
INodeExecutionData ,
2020-10-21 14:32:07 -07:00
INodePropertyOptions ,
2020-10-21 14:30:07 -07:00
INodeType ,
INodeTypeDescription ,
} from 'n8n-workflow' ;
2022-08-17 08:50:24 -07:00
import { pushoverApiRequest } from './GenericFunctions' ;
2020-10-21 14:30:07 -07:00
export class Pushover implements INodeType {
description : INodeTypeDescription = {
displayName : 'Pushover' ,
name : 'pushover' ,
2022-05-07 04:25:53 -07:00
icon : 'file:pushover.svg' ,
2020-10-21 14:30:07 -07:00
group : [ 'input' ] ,
version : 1 ,
subtitle : '={{$parameter["operation"] + ": " + $parameter["resource"]}}' ,
2021-07-03 05:40:16 -07:00
description : 'Consume Pushover API' ,
2020-10-21 14:30:07 -07:00
defaults : {
name : 'Pushover' ,
} ,
inputs : [ 'main' ] ,
outputs : [ 'main' ] ,
credentials : [
{
name : 'pushoverApi' ,
required : true ,
} ,
] ,
properties : [
{
displayName : 'Resource' ,
name : 'resource' ,
type : 'options' ,
2022-05-07 04:25:53 -07:00
noDataExpression : true ,
2020-10-21 14:30:07 -07:00
options : [
{
name : 'Message' ,
value : 'message' ,
} ,
] ,
default : 'message' ,
} ,
{
displayName : 'Operation' ,
name : 'operation' ,
type : 'options' ,
2022-05-07 04:25:53 -07:00
noDataExpression : true ,
2020-10-21 14:30:07 -07:00
displayOptions : {
show : {
2022-08-17 08:50:24 -07:00
resource : [ 'message' ] ,
2020-10-21 14:30:07 -07:00
} ,
} ,
options : [
{
name : 'Push' ,
value : 'push' ,
2022-07-10 13:50:51 -07:00
action : 'Push a message' ,
2020-10-21 14:30:07 -07:00
} ,
] ,
default : 'push' ,
} ,
{
displayName : 'User Key' ,
name : 'userKey' ,
type : 'string' ,
required : true ,
displayOptions : {
show : {
2022-08-17 08:50:24 -07:00
resource : [ 'message' ] ,
operation : [ 'push' ] ,
2020-10-21 14:30:07 -07:00
} ,
} ,
default : '' ,
2022-08-17 08:50:24 -07:00
description :
'The user/group key (not e-mail address) of your user (or you), viewable when logged into the <a href="https://pushover.net/">dashboard</a> (often referred to as <code>USER_KEY</code> in the <a href="https://support.pushover.net/i44-example-code-and-pushover-libraries">libraries</a> and code examples)' ,
2020-10-21 14:30:07 -07:00
} ,
{
displayName : 'Message' ,
name : 'message' ,
type : 'string' ,
required : true ,
displayOptions : {
show : {
2022-08-17 08:50:24 -07:00
resource : [ 'message' ] ,
operation : [ 'push' ] ,
2020-10-21 14:30:07 -07:00
} ,
} ,
default : '' ,
2022-05-06 14:01:25 -07:00
description : 'Your message' ,
2020-10-21 14:30:07 -07:00
} ,
2023-08-01 06:32:33 -07:00
2020-10-21 14:30:07 -07:00
{
displayName : 'Priority' ,
name : 'priority' ,
type : 'options' ,
displayOptions : {
show : {
2022-08-17 08:50:24 -07:00
resource : [ 'message' ] ,
operation : [ 'push' ] ,
2020-10-21 14:30:07 -07:00
} ,
} ,
2022-06-03 10:23:49 -07:00
// eslint-disable-next-line n8n-nodes-base/node-param-options-type-unsorted-items
2020-10-21 14:30:07 -07:00
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 ,
2022-08-17 08:50:24 -07:00
description :
"Send as -2 to generate no notification/alert, -1 to always send as a quiet notification, 1 to display as high-priority and bypass the user's quiet hours, or 2 to also require confirmation from the user" ,
2020-10-21 14:30:07 -07:00
} ,
{
2022-05-07 04:25:53 -07:00
displayName : 'Retry (Seconds)' ,
2020-10-21 14:30:07 -07:00
name : 'retry' ,
type : 'number' ,
typeOptions : {
minValue : 0 ,
} ,
required : true ,
displayOptions : {
show : {
2022-08-17 08:50:24 -07:00
resource : [ 'message' ] ,
operation : [ 'push' ] ,
priority : [ 2 ] ,
2020-10-21 14:30:07 -07:00
} ,
} ,
default : 30 ,
2022-08-17 08:50:24 -07:00
description :
'Specifies how often (in seconds) the Pushover servers will send the same notification to the user. This parameter must have a value of at least 30 seconds between retries.' ,
2020-10-21 14:30:07 -07:00
} ,
{
2022-05-07 04:25:53 -07:00
displayName : 'Expire (Seconds)' ,
2020-10-21 14:30:07 -07:00
name : 'expire' ,
type : 'number' ,
typeOptions : {
minValue : 0 ,
maxValue : 10800 ,
} ,
required : true ,
displayOptions : {
show : {
2022-08-17 08:50:24 -07:00
resource : [ 'message' ] ,
operation : [ 'push' ] ,
priority : [ 2 ] ,
2020-10-21 14:30:07 -07:00
} ,
} ,
default : 30 ,
2022-08-17 08:50:24 -07:00
description :
'Specifies how many seconds your notification will continue to be retried for (every retry seconds)' ,
2020-10-21 14:30:07 -07:00
} ,
{
displayName : 'Additional Fields' ,
name : 'additionalFields' ,
type : 'collection' ,
placeholder : 'Add Field' ,
displayOptions : {
show : {
2022-08-17 08:50:24 -07:00
resource : [ 'message' ] ,
operation : [ 'push' ] ,
2020-10-21 14:30:07 -07:00
} ,
} ,
default : { } ,
options : [
{
displayName : 'Attachment' ,
name : 'attachmentsUi' ,
placeholder : 'Add Attachments' ,
type : 'fixedCollection' ,
typeOptions : {
multipleValues : false ,
} ,
options : [
{
name : 'attachmentsValues' ,
displayName : 'Attachment Property' ,
values : [
{
2024-01-03 03:08:16 -08:00
displayName : 'Input Binary Field' ,
2020-10-21 14:30:07 -07:00
name : 'binaryPropertyName' ,
type : 'string' ,
default : '' ,
placeholder : 'data' ,
2024-01-03 03:08:16 -08:00
hint : 'The name of the input binary field containing the file which should be added to email as attachment' ,
2020-10-21 14:30:07 -07:00
} ,
] ,
} ,
] ,
2022-04-22 09:29:51 -07:00
default : { } ,
2020-10-21 14:30:07 -07:00
} ,
{
displayName : 'Device' ,
name : 'device' ,
type : 'string' ,
default : '' ,
2022-08-17 08:50:24 -07:00
description :
"Your user's device name to send the message directly to that device, rather than all of the user's devices (multiple devices may be separated by a comma)" ,
2020-10-21 14:30:07 -07:00
} ,
2022-05-07 04:25:53 -07:00
{
displayName : 'HTML Formatting' ,
name : 'html' ,
type : 'boolean' ,
default : false ,
description : 'Whether to enable messages formatting with HTML tags' ,
} ,
2020-10-21 14:30:07 -07:00
{
2022-06-03 10:23:49 -07:00
displayName : 'Sound Name or ID' ,
2020-10-21 14:30:07 -07:00
name : 'sound' ,
type : 'options' ,
typeOptions : {
loadOptionsMethod : 'getSounds' ,
} ,
default : '' ,
2022-08-17 08:50:24 -07:00
description :
'The name of one of the sounds supported by device clients to override the user\'s default sound choice. Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>.' ,
2020-10-21 14:30:07 -07:00
} ,
2022-05-07 04:25:53 -07:00
{
displayName : 'Timestamp' ,
name : 'timestamp' ,
type : 'dateTime' ,
default : '' ,
2022-08-17 08:50:24 -07:00
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" ,
2022-05-07 04:25:53 -07:00
} ,
2020-10-21 14:30:07 -07:00
{
displayName : 'Title' ,
name : 'title' ,
type : 'string' ,
default : '' ,
2022-08-17 08:50:24 -07:00
description : "Your message's title, otherwise your app's name is used" ,
2020-10-21 14:30:07 -07:00
} ,
{
displayName : 'Timestamp' ,
name : 'timestamp' ,
type : 'dateTime' ,
default : '' ,
2022-08-17 08:50:24 -07:00
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" ,
2020-10-21 14:30:07 -07:00
} ,
{
displayName : 'URL' ,
name : 'url' ,
type : 'string' ,
default : '' ,
2022-05-07 04:25:53 -07:00
description : 'A supplementary URL to show with your message' ,
2020-10-21 14:30:07 -07:00
} ,
{
displayName : 'URL Title' ,
name : 'url_title' ,
type : 'string' ,
default : '' ,
2022-05-06 14:01:25 -07:00
description : 'A title for your supplementary URL, otherwise just the URL is shown' ,
2020-10-21 14:30:07 -07:00
} ,
] ,
} ,
] ,
} ;
methods = {
loadOptions : {
async getSounds ( this : ILoadOptionsFunctions ) : Promise < INodePropertyOptions [ ] > {
const { sounds } = await pushoverApiRequest . call ( this , 'GET' , '/sounds.json' , { } ) ;
const returnData : INodePropertyOptions [ ] = [ ] ;
2023-02-27 19:39:43 -08:00
for ( const key of Object . keys ( sounds as IDataObject ) ) {
2020-10-21 14:30:07 -07:00
returnData . push ( {
name : sounds [ key ] ,
value : key ,
} ) ;
}
return returnData ;
} ,
} ,
} ;
async execute ( this : IExecuteFunctions ) : Promise < INodeExecutionData [ ] [ ] > {
const items = this . getInputData ( ) ;
2023-01-31 11:39:20 -08:00
const returnData : INodeExecutionData [ ] = [ ] ;
2022-04-22 09:29:51 -07:00
const length = items . length ;
2020-10-21 14:30:07 -07:00
let responseData ;
2022-12-02 03:53:59 -08:00
const resource = this . getNodeParameter ( 'resource' , 0 ) ;
const operation = this . getNodeParameter ( 'operation' , 0 ) ;
2020-10-21 14:30:07 -07:00
for ( let i = 0 ; i < length ; i ++ ) {
2021-07-19 23:58:54 -07:00
try {
if ( resource === 'message' ) {
if ( operation === 'push' ) {
const userKey = this . getNodeParameter ( 'userKey' , i ) as string ;
2020-10-21 14:30:07 -07:00
2021-07-19 23:58:54 -07:00
const message = this . getNodeParameter ( 'message' , i ) as string ;
2020-10-21 14:30:07 -07:00
2021-07-19 23:58:54 -07:00
const priority = this . getNodeParameter ( 'priority' , i ) as number ;
2020-10-21 14:30:07 -07:00
2022-11-18 07:29:44 -08:00
const additionalFields = this . getNodeParameter ( 'additionalFields' , i ) ;
2020-10-21 14:30:07 -07:00
2022-05-07 04:25:53 -07:00
if ( additionalFields . html !== undefined ) {
additionalFields . html = additionalFields . html ? '1' : '' ;
}
2021-07-19 23:58:54 -07:00
const body : IDataObject = {
user : userKey ,
message ,
priority ,
} ;
2020-10-21 14:30:07 -07:00
2021-07-19 23:58:54 -07:00
if ( priority === 2 ) {
body . retry = this . getNodeParameter ( 'retry' , i ) as number ;
2020-10-21 14:30:07 -07:00
2021-07-19 23:58:54 -07:00
body . expire = this . getNodeParameter ( 'expire' , i ) as number ;
}
2020-10-21 14:30:07 -07:00
2021-07-19 23:58:54 -07:00
Object . assign ( body , additionalFields ) ;
2020-10-21 14:30:07 -07:00
2021-07-19 23:58:54 -07:00
if ( body . attachmentsUi ) {
2022-08-17 08:50:24 -07:00
const attachment = ( body . attachmentsUi as IDataObject )
. attachmentsValues as IDataObject ;
2020-10-21 14:30:07 -07:00
2021-07-19 23:58:54 -07:00
if ( attachment ) {
const binaryPropertyName = attachment . binaryPropertyName as string ;
2023-03-06 08:33:32 -08:00
const binaryData = this . helpers . assertBinaryData ( i , binaryPropertyName ) ;
2021-08-20 09:08:40 -07:00
const dataBuffer = await this . helpers . getBinaryDataBuffer ( i , binaryPropertyName ) ;
2021-07-19 23:58:54 -07:00
body . attachment = {
2021-08-20 09:08:40 -07:00
value : dataBuffer ,
2021-07-19 23:58:54 -07:00
options : {
filename : binaryData.fileName ,
} ,
} ;
2020-10-21 14:30:07 -07:00
2021-07-19 23:58:54 -07:00
delete body . attachmentsUi ;
}
2020-10-21 14:30:07 -07:00
}
2022-12-29 03:20:43 -08:00
responseData = await pushoverApiRequest . call ( this , 'POST' , '/messages.json' , body ) ;
2023-01-31 11:39:20 -08:00
const executionData = this . helpers . constructExecutionMetaData (
2023-02-27 19:39:43 -08:00
this . helpers . returnJsonArray ( responseData as IDataObject ) ,
2023-01-31 11:39:20 -08:00
{ itemData : { item : i } } ,
) ;
returnData . push ( . . . executionData ) ;
2021-07-19 23:58:54 -07:00
}
}
} catch ( error ) {
if ( this . continueOnFail ( ) ) {
2023-01-31 11:39:20 -08:00
const executionData = this . helpers . constructExecutionMetaData (
this . helpers . returnJsonArray ( { error : error.message } ) ,
{ itemData : { item : i } } ,
) ;
returnData . push ( . . . executionData ) ;
2021-07-19 23:58:54 -07:00
continue ;
2020-10-21 14:30:07 -07:00
}
2021-07-19 23:58:54 -07:00
throw error ;
2020-10-21 14:30:07 -07:00
}
}
2023-09-05 03:59:02 -07:00
return [ returnData ] ;
2020-10-21 14:30:07 -07:00
}
}