2023-01-27 03:22:44 -08:00
import type {
2023-03-09 09:13:15 -08:00
IHookFunctions ,
IWebhookFunctions ,
2021-02-07 14:32:12 -08:00
IDataObject ,
ILoadOptionsFunctions ,
INodePropertyOptions ,
INodeType ,
INodeTypeDescription ,
IWebhookResponseData ,
2023-02-27 19:39:43 -08:00
JsonObject ,
2021-02-07 14:32:12 -08:00
} from 'n8n-workflow' ;
2023-01-27 03:22:44 -08:00
import { NodeApiError } from 'n8n-workflow' ;
2021-02-07 14:32:12 -08:00
2022-08-17 08:50:24 -07:00
import { getresponseApiRequest , getResponseApiRequestAllItems } from './GenericFunctions' ;
2021-02-07 14:32:12 -08:00
export class GetResponseTrigger implements INodeType {
description : INodeTypeDescription = {
displayName : 'GetResponse Trigger' ,
name : 'getResponseTrigger' ,
2022-06-20 07:54:01 -07:00
// eslint-disable-next-line n8n-nodes-base/node-class-description-icon-not-svg
2021-02-07 14:32:12 -08:00
icon : 'file:getResponse.png' ,
group : [ 'trigger' ] ,
version : 1 ,
2021-07-03 05:40:16 -07:00
description : 'Starts the workflow when GetResponse events occur' ,
2021-02-07 14:32:12 -08:00
defaults : {
name : 'GetResponse Trigger' ,
} ,
inputs : [ ] ,
outputs : [ 'main' ] ,
credentials : [
{
name : 'getResponseApi' ,
required : true ,
displayOptions : {
show : {
2022-08-17 08:50:24 -07:00
authentication : [ 'apiKey' ] ,
2021-02-07 14:32:12 -08:00
} ,
} ,
} ,
{
name : 'getResponseOAuth2Api' ,
required : true ,
displayOptions : {
show : {
2022-08-17 08:50:24 -07:00
authentication : [ 'oAuth2' ] ,
2021-02-07 14:32:12 -08:00
} ,
} ,
} ,
] ,
webhooks : [
{
name : 'default' ,
httpMethod : 'GET' ,
responseMode : 'onReceived' ,
path : 'webhook' ,
} ,
] ,
properties : [
{
displayName : 'Authentication' ,
name : 'authentication' ,
type : 'options' ,
options : [
{
name : 'API Key' ,
value : 'apiKey' ,
} ,
{
name : 'OAuth2' ,
value : 'oAuth2' ,
} ,
] ,
default : 'apiKey' ,
} ,
{
displayName : 'Events' ,
name : 'events' ,
type : 'multiOptions' ,
options : [
{
name : 'Customer Subscribed' ,
value : 'subscribe' ,
2022-05-06 14:01:25 -07:00
description : 'Receive notifications when a customer is subscribed to a list' ,
2021-02-07 14:32:12 -08:00
} ,
{
name : 'Customer Unsubscribed' ,
value : 'unsubscribe' ,
2022-05-06 14:01:25 -07:00
description : 'Receive notifications when a customer is unsubscribed from a list' ,
2021-02-07 14:32:12 -08:00
} ,
{
name : 'Email Clicked' ,
value : 'click' ,
2022-05-06 14:01:25 -07:00
description : 'Receive notifications when a email is clicked' ,
2021-02-07 14:32:12 -08:00
} ,
2022-06-20 07:54:01 -07:00
{
name : 'Email Opened' ,
value : 'open' ,
description : 'Receive notifications when a email is opened' ,
} ,
2021-02-07 14:32:12 -08:00
{
name : 'Survey Submitted' ,
value : 'survey' ,
2022-05-06 14:01:25 -07:00
description : 'Receive notifications when a survey is submitted' ,
2021-02-07 14:32:12 -08:00
} ,
] ,
default : [ ] ,
required : true ,
} ,
{
2022-06-20 07:54:01 -07:00
displayName : 'List Names or IDs' ,
2021-02-07 14:32:12 -08:00
name : 'listIds' ,
type : 'multiOptions' ,
2022-08-17 08:50:24 -07:00
description :
'Choose from the list, or specify IDs using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>' ,
2021-02-07 14:32:12 -08:00
typeOptions : {
loadOptionsMethod : 'getLists' ,
} ,
default : [ ] ,
} ,
{
displayName : 'Options' ,
name : 'options' ,
placeholder : 'Add Option' ,
type : 'collection' ,
default : { } ,
options : [
{
displayName : 'Delete Current Subscription' ,
name : 'delete' ,
type : 'boolean' ,
default : false ,
2022-06-20 07:54:01 -07:00
description : 'Whether to delete the current subscription' ,
2021-02-07 14:32:12 -08:00
} ,
] ,
} ,
] ,
} ;
methods = {
loadOptions : {
// Get all the available teams to display them to user so that he can
// select them easily
async getLists ( this : ILoadOptionsFunctions ) : Promise < INodePropertyOptions [ ] > {
const returnData : INodePropertyOptions [ ] = [ ] ;
const lists = await getResponseApiRequestAllItems . call ( this , 'GET' , '/campaigns' ) ;
returnData . push ( { name : '*' , value : '*' } ) ;
for ( const list of lists ) {
returnData . push ( {
name : list.name ,
value : list.campaignId ,
} ) ;
}
return returnData ;
} ,
} ,
} ;
webhookMethods = {
default : {
async checkExists ( this : IHookFunctions ) : Promise < boolean > {
const webhookUrl = this . getNodeWebhookUrl ( 'default' ) ;
const deleteCurrentSubscription = this . getNodeParameter ( 'options.delete' , false ) as boolean ;
try {
const data = await getresponseApiRequest . call ( this , 'GET' , '/accounts/callbacks' , { } ) ;
if ( data . url !== webhookUrl ) {
2022-12-02 12:54:28 -08:00
if ( ! deleteCurrentSubscription ) {
2023-02-27 19:39:43 -08:00
throw new NodeApiError ( this . getNode ( ) , data as JsonObject , {
2022-08-17 08:50:24 -07:00
message : ` The webhook ( ${ data . url } ) is active in the account. Delete it manually or set the parameter "Delete Current Subscription" to true, and the node will delete it for you. ` ,
} ) ;
2021-02-07 14:32:12 -08:00
}
}
} catch ( error ) {
2021-04-30 19:12:11 -07:00
if ( error . httpCode === '404' ) {
2021-02-07 14:32:12 -08:00
return false ;
}
}
await getresponseApiRequest . call ( this , 'DELETE' , '/accounts/callbacks' ) ;
return false ;
} ,
async create ( this : IHookFunctions ) : Promise < boolean > {
const webhookUrl = this . getNodeWebhookUrl ( 'default' ) ;
const events = this . getNodeParameter ( 'events' ) as string [ ] ;
const body = {
url : webhookUrl ,
2022-08-17 08:50:24 -07:00
actions : events.reduce (
( accumulator : { [ key : string ] : boolean } , currentValue : string ) = > {
accumulator [ currentValue ] = true ;
return accumulator ;
} ,
{ } ,
) ,
2021-02-07 14:32:12 -08:00
} ;
await getresponseApiRequest . call ( this , 'POST' , '/accounts/callbacks' , body ) ;
return true ;
} ,
async delete ( this : IHookFunctions ) : Promise < boolean > {
try {
await getresponseApiRequest . call ( this , 'DELETE' , '/accounts/callbacks' ) ;
2021-04-16 09:33:36 -07:00
} catch ( error ) {
2021-02-07 14:32:12 -08:00
return false ;
}
return true ;
} ,
} ,
} ;
async webhook ( this : IWebhookFunctions ) : Promise < IWebhookResponseData > {
const query = this . getQueryData ( ) as IDataObject ;
const listIds = this . getNodeParameter ( 'listIds' ) as string [ ] ;
2022-12-02 12:54:28 -08:00
if ( ! listIds . includes ( '*' ) && ! listIds . includes ( query . CAMPAIGN_ID as string ) ) {
2021-02-07 14:32:12 -08:00
return { } ;
}
return {
2022-08-17 08:50:24 -07:00
workflowData : [ this . helpers . returnJsonArray ( query ) ] ,
2021-02-07 14:32:12 -08:00
} ;
}
}