🔀 Merge branch 'Walkover-Web-Solution-msg91-integration-new'

This commit is contained in:
Jan Oberhauser 2020-01-07 18:51:12 -06:00
commit 10a7fbe307
5 changed files with 265 additions and 0 deletions

View file

@ -0,0 +1,19 @@
import {
ICredentialType,
NodePropertyTypes,
} from 'n8n-workflow';
export class Msg91Api implements ICredentialType {
name = 'msg91Api';
displayName = 'Msg91 Api';
properties = [
// User authentication key
{
displayName: 'Authentication Key',
name: 'authkey',
type: 'string' as NodePropertyTypes,
default: '',
},
];
}

View file

@ -0,0 +1,60 @@
import {
IExecuteFunctions,
IHookFunctions,
} from 'n8n-core';
import {
IDataObject,
} from 'n8n-workflow';
/**
* Make an API request to MSG91
*
* @param {IHookFunctions} this
* @param {string} method
* @param {string} url
* @param {object} body
* @returns {Promise<any>}
*/
export async function msg91ApiRequest(this: IHookFunctions | IExecuteFunctions, method: string, endpoint: string, body: IDataObject, query?: IDataObject): Promise<any> { // tslint:disable-line:no-any
const credentials = this.getCredentials('msg91Api');
if (credentials === undefined) {
throw new Error('No credentials got returned!');
}
if (query === undefined) {
query = {};
}
query.authkey = credentials.authkey as string;
const options = {
method,
form: body,
qs: query,
uri: `https://api.msg91.com/api${endpoint}`,
json: true
};
try {
return await this.helpers.request(options);
} catch (error) {
if (error.statusCode === 401) {
// Return a clear error
throw new Error('The MSG91 credentials are not valid!');
}
if (error.response && error.response.body && error.response.body.message) {
// Try to return the error prettier
let errorMessage = `MSG91 error response [${error.statusCode}]: ${error.response.body.message}`;
if (error.response.body.more_info) {
errorMessage = `errorMessage (${error.response.body.more_info})`;
}
throw new Error(errorMessage);
}
// If that data does not exist for some reason return the actual error
throw error;
}
}

View file

@ -0,0 +1,184 @@
import { IExecuteFunctions } from 'n8n-core';
import {
IDataObject,
INodeExecutionData,
INodeType,
INodeTypeDescription,
} from 'n8n-workflow';
import {
msg91ApiRequest,
} from './GenericFunctions';
export class Msg91 implements INodeType {
description: INodeTypeDescription = {
displayName: 'Msg91',
name: 'msg91',
icon: 'file:msg91.png',
group: ['transform'],
version: 1,
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
description: 'Send Transactional SMS',
defaults: {
name: 'Msg91',
color: '#0000ff',
},
inputs: ['main'],
outputs: ['main'],
credentials: [
{
name: 'msg91Api',
required: true,
}
],
properties: [
{
displayName: 'Resource',
name: 'resource',
type: 'options',
options: [
{
name: 'SMS',
value: 'sms',
},
],
default: 'sms',
description: 'The resource to operate on.',
},
{
displayName: 'Operation',
name: 'operation',
type: 'options',
displayOptions: {
show: {
resource: [
'sms',
],
},
},
options: [
{
name: 'Send',
value: 'send',
description: 'Send SMS',
},
],
default: 'send',
description: 'The operation to perform.',
},
{
displayName: 'From',
name: 'from',
type: 'string',
default: '',
placeholder: '4155238886',
required: true,
displayOptions: {
show: {
operation: [
'send',
],
resource: [
'sms',
],
},
},
description: 'The number from which to send the message.',
},
{
displayName: 'To',
name: 'to',
type: 'string',
default: '',
placeholder: '+14155238886',
required: true,
displayOptions: {
show: {
operation: [
'send',
],
resource: [
'sms',
],
},
},
description: 'The number, with coutry code, to which to send the message.',
},
{
displayName: 'Message',
name: 'message',
type: 'string',
default: '',
required: true,
displayOptions: {
show: {
operation: [
'send',
],
resource: [
'sms',
],
},
},
description: 'The message to send',
},
]
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData();
const returnData: IDataObject[] = [];
let operation: string;
let resource: string;
// For Post
let body: IDataObject;
// For Query string
let qs: IDataObject;
let requestMethod: string;
let endpoint: string;
for (let i = 0; i < items.length; i++) {
endpoint = '';
body = {};
qs = {};
resource = this.getNodeParameter('resource', i) as string;
operation = this.getNodeParameter('operation', i) as string;
if (resource === 'sms') {
if (operation === 'send') {
// ----------------------------------
// sms:send
// ----------------------------------
requestMethod = 'GET';
endpoint = '/sendhttp.php';
qs.route = 4;
qs.country = 0;
qs.sender = this.getNodeParameter('from', i) as string;
qs.mobiles = this.getNodeParameter('to', i) as string;
qs.message = this.getNodeParameter('message', i) as string;
} else {
throw new Error(`The operation "${operation}" is not known!`);
}
} else {
throw new Error(`The resource "${resource}" is not known!`);
}
const responseData = await msg91ApiRequest.call(this, requestMethod, endpoint, body, qs);
returnData.push({ requestId: responseData });
}
return [this.helpers.returnJsonArray(returnData)];
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.9 KiB

View file

@ -72,6 +72,7 @@
"dist/credentials/TodoistApi.credentials.js",
"dist/credentials/TrelloApi.credentials.js",
"dist/credentials/TwilioApi.credentials.js",
"dist/credentials/Msg91Api.credentials.js",
"dist/credentials/TypeformApi.credentials.js",
"dist/credentials/MandrillApi.credentials.js",
"dist/credentials/TodoistApi.credentials.js",
@ -165,6 +166,7 @@
"dist/nodes/Trello/Trello.node.js",
"dist/nodes/Trello/TrelloTrigger.node.js",
"dist/nodes/Twilio/Twilio.node.js",
"dist/nodes/Msg91/Msg91.node.js",
"dist/nodes/Typeform/TypeformTrigger.node.js",
"dist/nodes/Toggl/TogglTrigger.node.js",
"dist/nodes/Vero/Vero.node.js",