Skip to content

Commit 05b4b8b

Browse files
committed
Add more channels
1 parent 431b8ff commit 05b4b8b

File tree

4 files changed

+529
-295
lines changed

4 files changed

+529
-295
lines changed

nodes/Infobip/InfobipApi.node.ts

Lines changed: 165 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export class InfobipApi implements INodeType {
1717
group: ['transform'],
1818
version: 1,
1919
subtitle: '={{$parameter["operation"]}}',
20-
description: 'Interact with Infobip SMS API',
20+
description: 'Interact with Infobip API',
2121
defaults: {
2222
name: 'InfobipApi',
2323
},
@@ -37,19 +37,70 @@ export class InfobipApi implements INodeType {
3737
type: 'options',
3838
options: [
3939
{
40-
name: 'Send an SMS',
41-
value: 'sendSms',
42-
action: 'Send an SMS',
40+
name: 'Check Delivery Report',
41+
value: 'checkDeliveryReport',
42+
action: 'Check delivery report',
4343
},
4444
{
45-
name: 'View SMS Delivery Report',
46-
value: 'smsDeliveryReport',
47-
action: 'View sms delivery report',
45+
name: 'Send Message',
46+
value: 'sendMessage',
47+
action: 'Send message',
4848
},
4949
],
50-
default: 'sendSms',
50+
default: 'sendMessage',
5151
noDataExpression: true,
5252
},
53+
{
54+
displayName: 'Channel',
55+
name: 'channel',
56+
type: 'options',
57+
options: [
58+
{
59+
name: 'RCS',
60+
value: 'rcs',
61+
},
62+
{
63+
name: 'SMS',
64+
value: 'sms',
65+
},
66+
{
67+
name: 'Viber',
68+
value: 'viber',
69+
},
70+
{
71+
name: 'WhatsApp',
72+
value: 'whatsapp',
73+
},
74+
],
75+
default: 'sms',
76+
displayOptions: {
77+
show: {
78+
operation: [
79+
'sendMessage',
80+
],
81+
},
82+
},
83+
description: 'The messaging channel to use',
84+
},
85+
{
86+
displayName: 'Sender',
87+
name: 'sender',
88+
type: 'string',
89+
required: true,
90+
displayOptions: {
91+
show: {
92+
operation: [
93+
'sendMessage',
94+
],
95+
channel: [
96+
'sms',
97+
],
98+
},
99+
},
100+
default: '',
101+
placeholder: 'Enter sender ID',
102+
description: 'The sender ID',
103+
},
53104
{
54105
displayName: 'Sender',
55106
name: 'sender',
@@ -58,14 +109,53 @@ export class InfobipApi implements INodeType {
58109
displayOptions: {
59110
show: {
60111
operation: [
61-
'sendSms',
112+
'sendMessage',
113+
],
114+
channel: [
115+
'viber',
62116
],
63117
},
64118
},
65119
default: '',
66120
placeholder: 'Enter sender ID',
67121
description: 'The sender ID',
68122
},
123+
{
124+
displayName: 'Sender',
125+
name: 'sender',
126+
type: 'string',
127+
required: true,
128+
displayOptions: {
129+
show: {
130+
operation: [
131+
'sendMessage',
132+
],
133+
channel: [
134+
'rcs',
135+
'whatsapp',
136+
],
137+
},
138+
},
139+
default: '',
140+
placeholder: 'Enter sender ID',
141+
description: 'The sender ID',
142+
},
143+
{
144+
displayName: 'To send a WhatsApp message, the user needs to have replied to the conversation in the last 24 hours. You can still start the conversation by sending a template message. <a href="https://www.infobip.com/docs/tutorials/send-whatsapp-template-messages" target="_blank">Learn more</a>',
145+
name: 'whatsappNotice',
146+
type: 'notice',
147+
default: '',
148+
displayOptions: {
149+
show: {
150+
operation: [
151+
'sendMessage',
152+
],
153+
channel: [
154+
'whatsapp',
155+
],
156+
},
157+
},
158+
},
69159
{
70160
displayName: 'Phone Number',
71161
name: 'phone',
@@ -74,7 +164,7 @@ export class InfobipApi implements INodeType {
74164
displayOptions: {
75165
show: {
76166
operation: [
77-
'sendSms',
167+
'sendMessage',
78168
],
79169
},
80170
},
@@ -91,7 +181,7 @@ export class InfobipApi implements INodeType {
91181
displayOptions: {
92182
show: {
93183
operation: [
94-
'sendSms',
184+
'sendMessage',
95185
],
96186
},
97187
},
@@ -107,28 +197,27 @@ export class InfobipApi implements INodeType {
107197
displayOptions: {
108198
show: {
109199
operation: [
110-
'smsDeliveryReport',
200+
'checkDeliveryReport',
111201
],
112202
},
113203
},
114204
default: '',
115205
placeholder: 'Message ID',
116-
description: 'Unique identifier of the SMS message whose delivery report you want to retrieve',
206+
description: 'Unique identifier of the message whose delivery report you want to retrieve',
117207
},
118208
],
119209
};
120210
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
121-
// Handle data coming from previous nodes
122211
const items = this.getInputData();
123212
const operation = this.getNodeParameter('operation', 0) as string;
124213
const returnData = [];
125214
const credentials = await this.getCredentials('infobipApi');
126215
const baseUrl = credentials?.domain as string;
127216

128217
for (let i = 0; i < items.length; i++) {
129-
if (operation === 'sendSms') {
218+
if (operation === 'sendMessage') {
130219
try {
131-
const responseData = await sendSms.call(this, i, baseUrl);
220+
const responseData = await sendMessage.call(this, i, baseUrl);
132221
returnData.push(responseData);
133222
} catch (error) {
134223
if (this.continueOnFail()) {
@@ -137,10 +226,9 @@ export class InfobipApi implements INodeType {
137226
}
138227
throw new NodeApiError(this.getNode(), error);
139228
}
140-
}
141-
if (operation === 'smsDeliveryReport') {
229+
} else if (operation === 'checkDeliveryReport') {
142230
try {
143-
const responseData = await smsDeliveryReport.call(this, i, baseUrl);
231+
const responseData = await checkDeliveryReport.call(this, i, baseUrl);
144232
returnData.push(responseData);
145233
} catch (error) {
146234
if (this.continueOnFail()) {
@@ -160,43 +248,83 @@ export class InfobipApi implements INodeType {
160248

161249
}
162250

163-
async function sendSms(
251+
async function sendMessage(
164252
this: IExecuteFunctions,
165253
iter: number,
166254
baseURL: string
167255
): Promise<any> {
256+
const channel = this.getNodeParameter('channel', iter) as string;
168257
const phone = this.getNodeParameter('phone', iter) as string;
169258
const content = this.getNodeParameter('content', iter) as string;
170259
const sender = this.getNodeParameter('sender', iter) as string;
171-
const data: IDataObject = {
172-
sender,
173-
destinations: [
174-
{ to: phone }
175-
],
176-
content: {
177-
text: content
178-
}
179-
};
260+
261+
let url: string;
262+
let body: IDataObject;
263+
264+
switch (channel) {
265+
case 'sms':
266+
url = '/sms/3/messages';
267+
body = {
268+
messages: [
269+
{
270+
sender,
271+
destinations: [{ to: phone }],
272+
content: { text: content },
273+
},
274+
],
275+
};
276+
break;
277+
case 'rcs':
278+
url = '/rcs/2/messages';
279+
body = {
280+
messages: [
281+
{
282+
sender,
283+
destinations: [{ to: phone }],
284+
content: { text: content, type: 'TEXT' },
285+
},
286+
],
287+
};
288+
break;
289+
case 'viber':
290+
url = '/viber/2/messages';
291+
body = {
292+
messages: [
293+
{
294+
sender,
295+
destinations: [{ to: phone }],
296+
content: { type: 'TEXT', text: content },
297+
},
298+
],
299+
};
300+
break;
301+
case 'whatsapp':
302+
url = '/whatsapp/1/message/text';
303+
body = {
304+
from: sender,
305+
to: phone,
306+
content: { text: content },
307+
};
308+
break;
309+
default:
310+
throw new NodeApiError(this.getNode(), { message: `Unsupported channel: ${channel}` } as any);
311+
}
180312

181313
const options: IHttpRequestOptions = {
182314
headers: {
183315
'Accept': 'application/json',
184316
'Content-Type': 'application/json',
185317
},
186318
method: 'POST',
187-
body: {
188-
messages: [
189-
data,
190-
],
191-
},
319+
body,
192320
baseURL,
193-
url: '/sms/3/messages',
321+
url,
194322
json: true,
195323
};
196324
return await this.helpers.httpRequestWithAuthentication.call(this, 'infobipApi', options);
197325
}
198326

199-
async function smsDeliveryReport(
327+
async function checkDeliveryReport(
200328
this: IExecuteFunctions,
201329
iter: number,
202330
baseURL: string
@@ -213,7 +341,7 @@ async function smsDeliveryReport(
213341
messageId: messageId,
214342
},
215343
baseURL,
216-
url: '/sms/3/reports',
344+
url: '/messages-api/1/reports',
217345
json: true,
218346
};
219347
return await this.helpers.httpRequestWithAuthentication.call(this, 'infobipApi', options);

nodes/InfobipApi.node.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
],
1212
"primaryDocumentation": [
1313
{
14-
"url": "https://www.infobip.com/docs/api/channels/sms"
14+
"url": "https://www.infobip.com/docs/api"
1515
}
1616
]
1717
}

0 commit comments

Comments
 (0)