/
send-text-message.js
122 lines (89 loc) · 2.7 KB
/
send-text-message.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
module.exports = {
friendlyName: 'Send text message',
description: 'Send a text message using the Nexmo SMS API',
cacheable: false,
sync: false,
idempotent: false,
inputs: {
apiKey: {
example: 'n3xm0rocks',
description: 'Your NEXMO API Key.',
required: true
},
apiSecret: {
example: '12ab34cd',
description: 'Your NEXMO API Secret.',
required: true
},
from: {
example: '18479121345',
description: 'Sender SMS number.',
extendedDescription: 'The number may be alphanumeric (Ex: from=MyCompany20). Restrictions may apply, depending on the destination.',
required: true
},
to: {
example: '447525856424',
description: 'Recipient SMS number.',
extendedDescription: 'Mobile number in international format, and one recipient per request. Ex: to=447525856424 or to=00447525856424 when sending to UK.',
required: true
},
text: {
example: 'D%c3%a9j%c3%a0+vu',
description: 'Body of the text message.',
extendedDescription: 'Maximum length of a text message is 3200 characters, UTF-8 and URL encoded value.',
required: true
},
protocol: {
example: 'http',
description: 'Default protocol for the request is https. User can switch to http.'
}
},
exits: {
error: {
description: 'An unexpected error occurred.'
},
requestError: {
description: 'Fail to send the text message.'
},
success: {
description: 'Message has been successfully sent.'
}
},
fn: function (inputs,exits) {
var request = require('superagent');
var protocol = inputs.protocol || 'https';
var baseUrl = protocol + '://rest.nexmo.com';
var smsUrl = baseUrl + '/sms/json';
request
.get(smsUrl)
.query({
api_key: inputs.apiKey,
api_secret: inputs.apiSecret,
from: inputs.from,
to: inputs.to,
text: inputs.text
})
.end(function(err, res) {
if (err) return exits.error(err);
var result = res.body.messages.reduce(function (initialValue, currentValue, index, array) {
if (parseInt(currentValue.status) !== 0) {
initialValue.numFails += 1;
initialValue.fails.push({
messageId: currentValue['message-id'],
statusCode: currentValue['status'],
errorText: currentValue['error-text']
});
}
return initialValue;
}, {
numFails: 0,
fails: []
});
if (result.numFails === 0) {
return exits.success(res.body);
} else {
return exits.requestError(result);
}
});
},
};