@corbit2/node-red-corbit-sms 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,351 +1,351 @@
1
- <!-- ===================================================== account (config) -->
2
- <script type="text/javascript">
3
- RED.nodes.registerType('corbit-sms-account', {
4
- category: 'config',
5
- paletteLabel: 'Corbit SMS account',
6
- defaults: {
7
- name: { value: '' },
8
- baseUrl: {
9
- value: 'https://api.corbit.sa',
10
- required: true,
11
- validate: RED.validators.regex(/^(https:\/\/[^\s/]+|http:\/\/(localhost|127\.0\.0\.1|\[::1\])([:/]|$))/i),
12
- },
13
- },
14
- credentials: {
15
- apiToken: { type: 'password', required: true },
16
- },
17
- label: function () {
18
- return this.name || 'Corbit SMS account';
19
- },
20
- oneditprepare: function () {
21
- const id = this.id;
22
- $('#corbit-sms-test').on('click', function (e) {
23
- e.preventDefault();
24
- const out = $('#corbit-sms-test-result');
25
- out.css('color', '').text('Testing…');
26
- $.ajax({
27
- url: 'corbit-sms/test',
28
- type: 'POST',
29
- contentType: 'application/json',
30
- data: JSON.stringify({
31
- account: id,
32
- apiToken: $('#node-config-input-apiToken').val(),
33
- baseUrl: $('#node-config-input-baseUrl').val(),
34
- }),
35
- })
36
- .done(function (r) {
37
- out.css('color', 'green').text('Connected. Balance: ' + r.balance);
38
- })
39
- .fail(function (x) {
40
- const r = x.responseJSON || {};
41
- out.css('color', '#c00').text((r.error || 'Failed') + (r.hint ? ' — ' + r.hint : ''));
42
- });
43
- });
44
- },
45
- });
46
- </script>
47
-
48
- <script type="text/html" data-template-name="corbit-sms-account">
49
- <div class="form-row">
50
- <label for="node-config-input-name"><i class="fa fa-tag"></i> Name</label>
51
- <input type="text" id="node-config-input-name" placeholder="Corbit SMS account" />
52
- </div>
53
- <div class="form-row">
54
- <label for="node-config-input-apiToken"><i class="fa fa-key"></i> API token</label>
55
- <input type="password" id="node-config-input-apiToken" placeholder="From sms.corbit.sa" />
56
- </div>
57
- <div class="form-row">
58
- <label for="node-config-input-baseUrl"><i class="fa fa-globe"></i> Base URL</label>
59
- <input type="text" id="node-config-input-baseUrl" placeholder="https://api.corbit.sa" />
60
- </div>
61
- <div class="form-row">
62
- <label></label>
63
- <button type="button" class="red-ui-button" id="corbit-sms-test"><i class="fa fa-plug"></i> Test connection</button>
64
- </div>
65
- <div class="form-row">
66
- <label></label>
67
- <span id="corbit-sms-test-result" style="display:inline-block; width:70%;"></span>
68
- </div>
69
- <div class="form-tips">
70
- Copy the API token from your account at <b>sms.corbit.sa</b>. Keep the base URL as
71
- <code>https://api.corbit.sa</code> unless Corbit support gives you another address.
72
- </div>
73
- </script>
74
-
75
- <script type="text/markdown" data-help-name="corbit-sms-account">
76
- Holds the API token of your Corbit SMS account. Several Corbit SMS nodes can share one account.
77
-
78
- ### Properties
79
-
80
- - **API token**: the token from your account at [sms.corbit.sa](https://sms.corbit.sa). It is stored with the Node-RED credentials and is not exported with the flow.
81
- - **Base URL**: keep `https://api.corbit.sa` unless Corbit support gives you another address.
82
-
83
- **Test connection** reads your balance with the token you typed, before you deploy.
84
- </script>
85
-
86
- <!-- ================================================================ node -->
87
- <script type="text/javascript">
88
- (function () {
89
- const OPERATIONS = {
90
- send: 'Send SMS',
91
- sendBulk: 'Send SMS to many numbers',
92
- getStatus: 'Get message status',
93
- getReport: 'Get delivery report',
94
- getBalance: 'Get balance',
95
- getSenderNames: 'Get sender names',
96
- };
97
- const ROWS = {
98
- send: ['sender', 'to', 'message', 'options'],
99
- sendBulk: ['sender', 'numbers', 'message', 'options'],
100
- getStatus: ['messageId'],
101
- getReport: ['messageId'],
102
- getBalance: [],
103
- getSenderNames: ['senderStatus'],
104
- };
105
- const ALL_ROWS = ['sender', 'to', 'numbers', 'message', 'options', 'messageId', 'senderStatus'];
106
-
107
- RED.nodes.registerType('corbit-sms', {
108
- category: 'network',
109
- color: '#F6A26B',
110
- defaults: {
111
- name: { value: '' },
112
- account: { value: '', type: 'corbit-sms-account', required: true },
113
- operation: { value: 'send' },
114
- sender: { value: '' },
115
- senderType: { value: 'str' },
116
- to: { value: 'to' },
117
- toType: { value: 'msg' },
118
- numbers: { value: 'numbers' },
119
- numbersType: { value: 'msg' },
120
- message: { value: 'payload' },
121
- messageType: { value: 'msg' },
122
- messageId: { value: 'messageId' },
123
- messageIdType: { value: 'msg' },
124
- idempotencyKey: { value: '' },
125
- idempotencyKeyType: { value: 'none' },
126
- allowDuplicate: { value: false },
127
- senderStatus: { value: 'all' },
128
- },
129
- inputs: 1,
130
- outputs: 1,
131
- icon: 'corbit-sms.svg',
132
- paletteLabel: 'Corbit SMS',
133
- label: function () {
134
- return this.name || OPERATIONS[this.operation] || 'Corbit SMS';
135
- },
136
- labelStyle: function () {
137
- return this.name ? 'node_label_italic' : '';
138
- },
139
- outputLabels: function () {
140
- return 'result in msg.payload';
141
- },
142
- oneditprepare: function () {
143
- const node = this;
144
- const anyTypes = ['msg', 'flow', 'global', 'str', 'env', 'jsonata'];
145
- $('#node-input-sender').typedInput({ types: ['str', 'msg', 'flow', 'global', 'env'], typeField: '#node-input-senderType' });
146
- $('#node-input-to').typedInput({ types: anyTypes, typeField: '#node-input-toType' });
147
- $('#node-input-numbers').typedInput({ types: ['msg', 'flow', 'global', 'str', 'json', 'env', 'jsonata'], typeField: '#node-input-numbersType' });
148
- $('#node-input-message').typedInput({ types: anyTypes, typeField: '#node-input-messageType' });
149
- $('#node-input-messageId').typedInput({ types: ['msg', 'flow', 'global', 'str', 'env', 'jsonata'], typeField: '#node-input-messageIdType' });
150
- $('#node-input-idempotencyKey').typedInput({
151
- types: [{ value: 'none', label: 'none', hasValue: false }, 'msg', 'str', 'flow', 'global', 'env', 'jsonata'],
152
- typeField: '#node-input-idempotencyKeyType',
153
- });
154
-
155
- // Switching a field from msg.<name> to a fixed value would keep the old
156
- // property name as the value (the SMS would read "payload"), so clear it.
157
- [['#node-input-to', 'to'], ['#node-input-numbers', 'numbers'], ['#node-input-message', 'payload'], ['#node-input-messageId', 'messageId']].forEach(function (f) {
158
- let lastType = $(f[0]).typedInput('type');
159
- $(f[0]).on('change', function (event, type) {
160
- if (type && type !== lastType) {
161
- if (type !== 'msg' && $(f[0]).typedInput('value') === f[1]) $(f[0]).typedInput('value', '');
162
- lastType = type;
163
- }
164
- });
165
- });
166
-
167
- function showRows() {
168
- const op = $('#node-input-operation').val();
169
- ALL_ROWS.forEach(function (r) {
170
- $('.corbit-row-' + r).not('.corbit-sender-pick-row').toggle(ROWS[op].indexOf(r) !== -1);
171
- });
172
- const hasList = $('#corbit-sms-sender-pick option').length > 0;
173
- $('.corbit-sender-pick-row').toggle(hasList && ROWS[op].indexOf('sender') !== -1);
174
- }
175
- $('#node-input-operation').on('change', showRows);
176
- showRows();
177
-
178
- $('#corbit-sms-load-senders').on('click', function (e) {
179
- e.preventDefault();
180
- const pick = $('#corbit-sms-sender-pick');
181
- const info = $('#corbit-sms-sender-info');
182
- const accountId = $('#node-input-account').val();
183
- if (!accountId || accountId === '_ADD_') {
184
- info.css('color', '#c00').text('Choose or add an account first.');
185
- return;
186
- }
187
- const account = RED.nodes.node(accountId) || {};
188
- const creds = account.credentials || {};
189
- info.css('color', '').text('Loading…');
190
- $.ajax({
191
- url: 'corbit-sms/senders',
192
- type: 'POST',
193
- contentType: 'application/json',
194
- data: JSON.stringify({
195
- account: accountId,
196
- apiToken: typeof creds.apiToken === 'string' ? creds.apiToken : '',
197
- baseUrl: account.baseUrl || '',
198
- }),
199
- })
200
- .done(function (r) {
201
- pick.empty().append($('<option>').val('').text('Choose a sender name…'));
202
- const senders = r.senders || [];
203
- senders.forEach(function (s) {
204
- pick.append($('<option>').val(s).text(s));
205
- });
206
- $('.corbit-sender-pick-row').show();
207
- info.css('color', '').text(senders.length ? senders.length + ' approved sender names' : 'No approved sender names on this account.');
208
- })
209
- .fail(function (x) {
210
- const r = x.responseJSON || {};
211
- info.css('color', '#c00').text((r.error || 'Failed') + (r.hint ? ' — ' + r.hint : ''));
212
- });
213
- });
214
- $('#corbit-sms-sender-pick').on('change', function () {
215
- const v = $(this).val();
216
- if (v) $('#node-input-sender').typedInput('type', 'str').typedInput('value', v);
217
- });
218
- },
219
- });
220
- })();
221
- </script>
222
-
223
- <script type="text/html" data-template-name="corbit-sms">
224
- <div class="form-row">
225
- <label for="node-input-account"><i class="fa fa-user"></i> Account</label>
226
- <input type="text" id="node-input-account" />
227
- </div>
228
- <div class="form-row">
229
- <label for="node-input-operation"><i class="fa fa-wrench"></i> Operation</label>
230
- <select id="node-input-operation" style="width:70%;">
231
- <option value="send">Send SMS</option>
232
- <option value="sendBulk">Send SMS to many numbers</option>
233
- <option value="getStatus">Get message status</option>
234
- <option value="getReport">Get delivery report</option>
235
- <option value="getBalance">Get balance</option>
236
- <option value="getSenderNames">Get sender names</option>
237
- </select>
238
- </div>
239
-
240
- <div class="form-row corbit-row-sender">
241
- <label for="node-input-sender"><i class="fa fa-id-badge"></i> Sender</label>
242
- <input type="text" id="node-input-sender" style="width:70%;" placeholder="e.g. MyBrand" />
243
- <input type="hidden" id="node-input-senderType" />
244
- </div>
245
- <div class="form-row corbit-row-sender">
246
- <label></label>
247
- <button type="button" class="red-ui-button" id="corbit-sms-load-senders"><i class="fa fa-list"></i> Load my sender names</button>
248
- <span id="corbit-sms-sender-info" style="margin-left:8px;"></span>
249
- </div>
250
- <div class="form-row corbit-row-sender corbit-sender-pick-row" style="display:none;">
251
- <label></label>
252
- <select id="corbit-sms-sender-pick" style="width:70%;"></select>
253
- </div>
254
- <div class="form-row corbit-row-to">
255
- <label for="node-input-to"><i class="fa fa-mobile"></i> To</label>
256
- <input type="text" id="node-input-to" style="width:70%;" />
257
- <input type="hidden" id="node-input-toType" />
258
- </div>
259
- <div class="form-row corbit-row-numbers">
260
- <label for="node-input-numbers"><i class="fa fa-users"></i> Numbers</label>
261
- <input type="text" id="node-input-numbers" style="width:70%;" />
262
- <input type="hidden" id="node-input-numbersType" />
263
- </div>
264
- <div class="form-row corbit-row-message">
265
- <label for="node-input-message"><i class="fa fa-comment"></i> Message</label>
266
- <input type="text" id="node-input-message" style="width:70%;" />
267
- <input type="hidden" id="node-input-messageType" />
268
- </div>
269
- <div class="form-row corbit-row-options">
270
- <label for="node-input-allowDuplicate"><i class="fa fa-clone"></i> Duplicates</label>
271
- <input type="checkbox" id="node-input-allowDuplicate" style="display:inline-block; width:auto; vertical-align:top;" />
272
- <label for="node-input-allowDuplicate" style="width:auto;">Allow the same text to the same number within an hour</label>
273
- </div>
274
- <div class="form-row corbit-row-options">
275
- <label for="node-input-idempotencyKey"><i class="fa fa-shield"></i> Idempotency</label>
276
- <input type="text" id="node-input-idempotencyKey" style="width:70%;" />
277
- <input type="hidden" id="node-input-idempotencyKeyType" />
278
- </div>
279
- <div class="form-row corbit-row-messageId">
280
- <label for="node-input-messageId"><i class="fa fa-hashtag"></i> Message ID</label>
281
- <input type="text" id="node-input-messageId" style="width:70%;" />
282
- <input type="hidden" id="node-input-messageIdType" />
283
- </div>
284
- <div class="form-row corbit-row-senderStatus">
285
- <label for="node-input-senderStatus"><i class="fa fa-filter"></i> Status</label>
286
- <select id="node-input-senderStatus" style="width:70%;">
287
- <option value="all">All</option>
288
- <option value="approved">Approved</option>
289
- <option value="pending">Pending</option>
290
- <option value="rejected">Rejected</option>
291
- <option value="expired">Expired</option>
292
- </select>
293
- </div>
294
- <div class="form-row">
295
- <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
296
- <input type="text" id="node-input-name" placeholder="Name" />
297
- </div>
298
- </script>
299
-
300
- <script type="text/markdown" data-help-name="corbit-sms">
301
- Sends SMS messages with [Corbit SMS](https://sms.corbit.sa) and checks their delivery, balance and sender names.
302
-
303
- ### Inputs
304
-
305
- By default the node reads these message properties. You can change each field to another property, a fixed value, a flow or global value, or an environment variable.
306
-
307
- : payload (string | number) : the text of the SMS, up to 1600 characters (Send and Send to many).
308
- : to (string) : the mobile number (Send). Use the international format, e.g. `966501234567`.
309
- : numbers (array | string) : the mobile numbers (Send to many), as an array or as text separated by commas or new lines.
310
- : messageId (string) : the ID returned by a send (Get message status, Get delivery report).
311
- : sender (string) : the sender name, used when the Sender field is empty.
312
- : allowDuplicate (boolean) : `true` sends even if the same text went to the same number in the last hour; `false` blocks it even if the box is ticked.
313
-
314
- ### Outputs
315
-
316
- : payload (object | array) : the result of the operation.
317
-
318
- - **Send SMS**: `{ messageId, status, to, senderName }`
319
- - **Send SMS to many numbers**: `{ messageId, status, recipients, numbers, senderName }`
320
- - **Get message status**: `{ messageId, status }`
321
- - **Get delivery report**: `{ messageId, jobId, recipients: [ ... ] }`, one entry per recipient
322
- - **Get balance**: `{ balance }`
323
- - **Get sender names**: an array of sender names with their status
324
-
325
- ### Errors
326
-
327
- When a call fails the node reports the error so a **Catch** node can handle it, and adds
328
- `msg.corbitError` with `message`, `hint` and, when Corbit SMS gives them, `code` and `statusCode`.
329
- Nothing is sent when a field is invalid.
330
-
331
- ### Details
332
-
333
- **Phone numbers.** Spaces, dashes, a leading `+` or `00` are removed. Saudi local numbers
334
- (`05XXXXXXXX`) become `9665XXXXXXXX`. Local numbers of other countries (starting with `0`)
335
- are rejected, so nothing goes to a wrong number.
336
-
337
- **Sender name.** Click **Load my sender names** to pick one of your approved names, or type it.
338
-
339
- **Duplicates.** Corbit SMS blocks the same text to the same number within one hour. Tick
340
- **Allow the same text…** (or set `msg.allowDuplicate = true`) for a resend on purpose, such as an OTP resend.
341
-
342
- **Safe retries.** Set **Idempotency** to a value that identifies the event behind the message, for
343
- example an order or request ID such as `msg.orderId`. If the same request is repeated with the same
344
- key, Corbit SMS returns the first result and does not send again. Don't use `msg._msgid` on its own:
345
- messages from a Split node, or sent to several nodes, share it, so only the first SMS would be sent.
346
-
347
- ### References
348
-
349
- - [Corbit SMS API documentation](https://api.corbit.sa/doc)
350
- - [GitHub repository and issues](https://github.com/agamalsa/node-red-corbit-sms)
351
- </script>
1
+ <!-- ===================================================== account (config) -->
2
+ <script type="text/javascript">
3
+ RED.nodes.registerType('corbit-sms-account', {
4
+ category: 'config',
5
+ paletteLabel: 'Corbit SMS account',
6
+ defaults: {
7
+ name: { value: '' },
8
+ baseUrl: {
9
+ value: 'https://api.corbit.sa',
10
+ required: true,
11
+ validate: RED.validators.regex(/^(https:\/\/[^\s/]+|http:\/\/(localhost|127\.0\.0\.1|\[::1\])([:/]|$))/i),
12
+ },
13
+ },
14
+ credentials: {
15
+ apiToken: { type: 'password', required: true },
16
+ },
17
+ label: function () {
18
+ return this.name || 'Corbit SMS account';
19
+ },
20
+ oneditprepare: function () {
21
+ const id = this.id;
22
+ $('#corbit-sms-test').on('click', function (e) {
23
+ e.preventDefault();
24
+ const out = $('#corbit-sms-test-result');
25
+ out.css('color', '').text('Testing…');
26
+ $.ajax({
27
+ url: 'corbit-sms/test',
28
+ type: 'POST',
29
+ contentType: 'application/json',
30
+ data: JSON.stringify({
31
+ account: id,
32
+ apiToken: $('#node-config-input-apiToken').val(),
33
+ baseUrl: $('#node-config-input-baseUrl').val(),
34
+ }),
35
+ })
36
+ .done(function (r) {
37
+ out.css('color', 'green').text('Connected. Balance: ' + r.balance);
38
+ })
39
+ .fail(function (x) {
40
+ const r = x.responseJSON || {};
41
+ out.css('color', '#c00').text((r.error || 'Failed') + (r.hint ? ' — ' + r.hint : ''));
42
+ });
43
+ });
44
+ },
45
+ });
46
+ </script>
47
+
48
+ <script type="text/html" data-template-name="corbit-sms-account">
49
+ <div class="form-row">
50
+ <label for="node-config-input-name"><i class="fa fa-tag"></i> Name</label>
51
+ <input type="text" id="node-config-input-name" placeholder="Corbit SMS account" />
52
+ </div>
53
+ <div class="form-row">
54
+ <label for="node-config-input-apiToken"><i class="fa fa-key"></i> API token</label>
55
+ <input type="password" id="node-config-input-apiToken" placeholder="From sms.corbit.sa" />
56
+ </div>
57
+ <div class="form-row">
58
+ <label for="node-config-input-baseUrl"><i class="fa fa-globe"></i> Base URL</label>
59
+ <input type="text" id="node-config-input-baseUrl" placeholder="https://api.corbit.sa" />
60
+ </div>
61
+ <div class="form-row">
62
+ <label></label>
63
+ <button type="button" class="red-ui-button" id="corbit-sms-test"><i class="fa fa-plug"></i> Test connection</button>
64
+ </div>
65
+ <div class="form-row">
66
+ <label></label>
67
+ <span id="corbit-sms-test-result" style="display:inline-block; width:70%;"></span>
68
+ </div>
69
+ <div class="form-tips">
70
+ Copy the API token from your account at <b>sms.corbit.sa</b>. Keep the base URL as
71
+ <code>https://api.corbit.sa</code> unless Corbit support gives you another address.
72
+ </div>
73
+ </script>
74
+
75
+ <script type="text/markdown" data-help-name="corbit-sms-account">
76
+ Holds the API token of your Corbit SMS account. Several Corbit SMS nodes can share one account.
77
+
78
+ ### Properties
79
+
80
+ - **API token**: the token from your account at [sms.corbit.sa](https://sms.corbit.sa). It is stored with the Node-RED credentials and is not exported with the flow.
81
+ - **Base URL**: keep `https://api.corbit.sa` unless Corbit support gives you another address.
82
+
83
+ **Test connection** reads your balance with the token you typed, before you deploy.
84
+ </script>
85
+
86
+ <!-- ================================================================ node -->
87
+ <script type="text/javascript">
88
+ (function () {
89
+ const OPERATIONS = {
90
+ send: 'Send SMS',
91
+ sendBulk: 'Send SMS to many numbers',
92
+ getStatus: 'Get message status',
93
+ getReport: 'Get delivery report',
94
+ getBalance: 'Get balance',
95
+ getSenderNames: 'Get sender names',
96
+ };
97
+ const ROWS = {
98
+ send: ['sender', 'to', 'message', 'options'],
99
+ sendBulk: ['sender', 'numbers', 'message', 'options'],
100
+ getStatus: ['messageId'],
101
+ getReport: ['messageId'],
102
+ getBalance: [],
103
+ getSenderNames: ['senderStatus'],
104
+ };
105
+ const ALL_ROWS = ['sender', 'to', 'numbers', 'message', 'options', 'messageId', 'senderStatus'];
106
+
107
+ RED.nodes.registerType('corbit-sms', {
108
+ category: 'network',
109
+ color: '#F6A26B',
110
+ defaults: {
111
+ name: { value: '' },
112
+ account: { value: '', type: 'corbit-sms-account', required: true },
113
+ operation: { value: 'send' },
114
+ sender: { value: '' },
115
+ senderType: { value: 'str' },
116
+ to: { value: 'to' },
117
+ toType: { value: 'msg' },
118
+ numbers: { value: 'numbers' },
119
+ numbersType: { value: 'msg' },
120
+ message: { value: 'payload' },
121
+ messageType: { value: 'msg' },
122
+ messageId: { value: 'messageId' },
123
+ messageIdType: { value: 'msg' },
124
+ idempotencyKey: { value: '' },
125
+ idempotencyKeyType: { value: 'none' },
126
+ allowDuplicate: { value: false },
127
+ senderStatus: { value: 'all' },
128
+ },
129
+ inputs: 1,
130
+ outputs: 1,
131
+ icon: 'corbit-sms.svg',
132
+ paletteLabel: 'Corbit SMS',
133
+ label: function () {
134
+ return this.name || OPERATIONS[this.operation] || 'Corbit SMS';
135
+ },
136
+ labelStyle: function () {
137
+ return this.name ? 'node_label_italic' : '';
138
+ },
139
+ outputLabels: function () {
140
+ return 'result in msg.payload';
141
+ },
142
+ oneditprepare: function () {
143
+ const node = this;
144
+ const anyTypes = ['msg', 'flow', 'global', 'str', 'env', 'jsonata'];
145
+ $('#node-input-sender').typedInput({ types: ['str', 'msg', 'flow', 'global', 'env'], typeField: '#node-input-senderType' });
146
+ $('#node-input-to').typedInput({ types: anyTypes, typeField: '#node-input-toType' });
147
+ $('#node-input-numbers').typedInput({ types: ['msg', 'flow', 'global', 'str', 'json', 'env', 'jsonata'], typeField: '#node-input-numbersType' });
148
+ $('#node-input-message').typedInput({ types: anyTypes, typeField: '#node-input-messageType' });
149
+ $('#node-input-messageId').typedInput({ types: ['msg', 'flow', 'global', 'str', 'env', 'jsonata'], typeField: '#node-input-messageIdType' });
150
+ $('#node-input-idempotencyKey').typedInput({
151
+ types: [{ value: 'none', label: 'none', hasValue: false }, 'msg', 'str', 'flow', 'global', 'env', 'jsonata'],
152
+ typeField: '#node-input-idempotencyKeyType',
153
+ });
154
+
155
+ // Switching a field from msg.<name> to a fixed value would keep the old
156
+ // property name as the value (the SMS would read "payload"), so clear it.
157
+ [['#node-input-to', 'to'], ['#node-input-numbers', 'numbers'], ['#node-input-message', 'payload'], ['#node-input-messageId', 'messageId']].forEach(function (f) {
158
+ let lastType = $(f[0]).typedInput('type');
159
+ $(f[0]).on('change', function (event, type) {
160
+ if (type && type !== lastType) {
161
+ if (type !== 'msg' && $(f[0]).typedInput('value') === f[1]) $(f[0]).typedInput('value', '');
162
+ lastType = type;
163
+ }
164
+ });
165
+ });
166
+
167
+ function showRows() {
168
+ const op = $('#node-input-operation').val();
169
+ ALL_ROWS.forEach(function (r) {
170
+ $('.corbit-row-' + r).not('.corbit-sender-pick-row').toggle(ROWS[op].indexOf(r) !== -1);
171
+ });
172
+ const hasList = $('#corbit-sms-sender-pick option').length > 0;
173
+ $('.corbit-sender-pick-row').toggle(hasList && ROWS[op].indexOf('sender') !== -1);
174
+ }
175
+ $('#node-input-operation').on('change', showRows);
176
+ showRows();
177
+
178
+ $('#corbit-sms-load-senders').on('click', function (e) {
179
+ e.preventDefault();
180
+ const pick = $('#corbit-sms-sender-pick');
181
+ const info = $('#corbit-sms-sender-info');
182
+ const accountId = $('#node-input-account').val();
183
+ if (!accountId || accountId === '_ADD_') {
184
+ info.css('color', '#c00').text('Choose or add an account first.');
185
+ return;
186
+ }
187
+ const account = RED.nodes.node(accountId) || {};
188
+ const creds = account.credentials || {};
189
+ info.css('color', '').text('Loading…');
190
+ $.ajax({
191
+ url: 'corbit-sms/senders',
192
+ type: 'POST',
193
+ contentType: 'application/json',
194
+ data: JSON.stringify({
195
+ account: accountId,
196
+ apiToken: typeof creds.apiToken === 'string' ? creds.apiToken : '',
197
+ baseUrl: account.baseUrl || '',
198
+ }),
199
+ })
200
+ .done(function (r) {
201
+ pick.empty().append($('<option>').val('').text('Choose a sender name…'));
202
+ const senders = r.senders || [];
203
+ senders.forEach(function (s) {
204
+ pick.append($('<option>').val(s).text(s));
205
+ });
206
+ $('.corbit-sender-pick-row').show();
207
+ info.css('color', '').text(senders.length ? senders.length + ' approved sender names' : 'No approved sender names on this account.');
208
+ })
209
+ .fail(function (x) {
210
+ const r = x.responseJSON || {};
211
+ info.css('color', '#c00').text((r.error || 'Failed') + (r.hint ? ' — ' + r.hint : ''));
212
+ });
213
+ });
214
+ $('#corbit-sms-sender-pick').on('change', function () {
215
+ const v = $(this).val();
216
+ if (v) $('#node-input-sender').typedInput('type', 'str').typedInput('value', v);
217
+ });
218
+ },
219
+ });
220
+ })();
221
+ </script>
222
+
223
+ <script type="text/html" data-template-name="corbit-sms">
224
+ <div class="form-row">
225
+ <label for="node-input-account"><i class="fa fa-user"></i> Account</label>
226
+ <input type="text" id="node-input-account" />
227
+ </div>
228
+ <div class="form-row">
229
+ <label for="node-input-operation"><i class="fa fa-wrench"></i> Operation</label>
230
+ <select id="node-input-operation" style="width:70%;">
231
+ <option value="send">Send SMS</option>
232
+ <option value="sendBulk">Send SMS to many numbers</option>
233
+ <option value="getStatus">Get message status</option>
234
+ <option value="getReport">Get delivery report</option>
235
+ <option value="getBalance">Get balance</option>
236
+ <option value="getSenderNames">Get sender names</option>
237
+ </select>
238
+ </div>
239
+
240
+ <div class="form-row corbit-row-sender">
241
+ <label for="node-input-sender"><i class="fa fa-id-badge"></i> Sender</label>
242
+ <input type="text" id="node-input-sender" style="width:70%;" placeholder="e.g. MyBrand" />
243
+ <input type="hidden" id="node-input-senderType" />
244
+ </div>
245
+ <div class="form-row corbit-row-sender">
246
+ <label></label>
247
+ <button type="button" class="red-ui-button" id="corbit-sms-load-senders"><i class="fa fa-list"></i> Load my sender names</button>
248
+ <span id="corbit-sms-sender-info" style="margin-left:8px;"></span>
249
+ </div>
250
+ <div class="form-row corbit-row-sender corbit-sender-pick-row" style="display:none;">
251
+ <label></label>
252
+ <select id="corbit-sms-sender-pick" style="width:70%;"></select>
253
+ </div>
254
+ <div class="form-row corbit-row-to">
255
+ <label for="node-input-to"><i class="fa fa-mobile"></i> To</label>
256
+ <input type="text" id="node-input-to" style="width:70%;" />
257
+ <input type="hidden" id="node-input-toType" />
258
+ </div>
259
+ <div class="form-row corbit-row-numbers">
260
+ <label for="node-input-numbers"><i class="fa fa-users"></i> Numbers</label>
261
+ <input type="text" id="node-input-numbers" style="width:70%;" />
262
+ <input type="hidden" id="node-input-numbersType" />
263
+ </div>
264
+ <div class="form-row corbit-row-message">
265
+ <label for="node-input-message"><i class="fa fa-comment"></i> Message</label>
266
+ <input type="text" id="node-input-message" style="width:70%;" />
267
+ <input type="hidden" id="node-input-messageType" />
268
+ </div>
269
+ <div class="form-row corbit-row-options">
270
+ <label for="node-input-allowDuplicate"><i class="fa fa-clone"></i> Duplicates</label>
271
+ <input type="checkbox" id="node-input-allowDuplicate" style="display:inline-block; width:auto; vertical-align:top;" />
272
+ <label for="node-input-allowDuplicate" style="width:auto;">Allow the same text to the same number within an hour</label>
273
+ </div>
274
+ <div class="form-row corbit-row-options">
275
+ <label for="node-input-idempotencyKey"><i class="fa fa-shield"></i> Idempotency</label>
276
+ <input type="text" id="node-input-idempotencyKey" style="width:70%;" />
277
+ <input type="hidden" id="node-input-idempotencyKeyType" />
278
+ </div>
279
+ <div class="form-row corbit-row-messageId">
280
+ <label for="node-input-messageId"><i class="fa fa-hashtag"></i> Message ID</label>
281
+ <input type="text" id="node-input-messageId" style="width:70%;" />
282
+ <input type="hidden" id="node-input-messageIdType" />
283
+ </div>
284
+ <div class="form-row corbit-row-senderStatus">
285
+ <label for="node-input-senderStatus"><i class="fa fa-filter"></i> Status</label>
286
+ <select id="node-input-senderStatus" style="width:70%;">
287
+ <option value="all">All</option>
288
+ <option value="approved">Approved</option>
289
+ <option value="pending">Pending</option>
290
+ <option value="rejected">Rejected</option>
291
+ <option value="expired">Expired</option>
292
+ </select>
293
+ </div>
294
+ <div class="form-row">
295
+ <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
296
+ <input type="text" id="node-input-name" placeholder="Name" />
297
+ </div>
298
+ </script>
299
+
300
+ <script type="text/markdown" data-help-name="corbit-sms">
301
+ Sends SMS messages with [Corbit SMS](https://sms.corbit.sa) and checks their delivery, balance and sender names.
302
+
303
+ ### Inputs
304
+
305
+ By default the node reads these message properties. You can change each field to another property, a fixed value, a flow or global value, or an environment variable.
306
+
307
+ : payload (string | number) : the text of the SMS, up to 1600 characters (Send and Send to many).
308
+ : to (string) : the mobile number (Send). Use the international format, e.g. `966501234567`.
309
+ : numbers (array | string) : the mobile numbers (Send to many), as an array or as text separated by commas or new lines.
310
+ : messageId (string) : the ID returned by a send (Get message status, Get delivery report).
311
+ : sender (string) : the sender name, used when the Sender field is empty.
312
+ : allowDuplicate (boolean) : `true` sends even if the same text went to the same number in the last hour; `false` blocks it even if the box is ticked.
313
+
314
+ ### Outputs
315
+
316
+ : payload (object | array) : the result of the operation.
317
+
318
+ - **Send SMS**: `{ messageId, status, to, senderName }`
319
+ - **Send SMS to many numbers**: `{ messageId, status, recipients, numbers, senderName }`
320
+ - **Get message status**: `{ messageId, status }`
321
+ - **Get delivery report**: `{ messageId, jobId, recipients: [ ... ] }`, one entry per recipient
322
+ - **Get balance**: `{ balance }`
323
+ - **Get sender names**: an array of sender names with their status
324
+
325
+ ### Errors
326
+
327
+ When a call fails the node reports the error so a **Catch** node can handle it, and adds
328
+ `msg.corbitError` with `message`, `hint` and, when Corbit SMS gives them, `code` and `statusCode`.
329
+ Nothing is sent when a field is invalid.
330
+
331
+ ### Details
332
+
333
+ **Phone numbers.** Spaces, dashes, a leading `+` or `00` are removed. Saudi local numbers
334
+ (`05XXXXXXXX`) become `9665XXXXXXXX`. Local numbers of other countries (starting with `0`)
335
+ are rejected, so nothing goes to a wrong number.
336
+
337
+ **Sender name.** Click **Load my sender names** to pick one of your approved names, or type it.
338
+
339
+ **Duplicates.** Corbit SMS blocks the same text to the same number within one hour. Tick
340
+ **Allow the same text…** (or set `msg.allowDuplicate = true`) for a resend on purpose, such as an OTP resend.
341
+
342
+ **Safe retries.** Set **Idempotency** to a value that identifies the event behind the message, for
343
+ example an order or request ID such as `msg.orderId`. If the same request is repeated with the same
344
+ key, Corbit SMS returns the first result and does not send again. Don't use `msg._msgid` on its own:
345
+ messages from a Split node, or sent to several nodes, share it, so only the first SMS would be sent.
346
+
347
+ ### References
348
+
349
+ - [Corbit SMS API documentation](https://api.corbit.sa/doc)
350
+ - [GitHub repository and issues](https://github.com/agamalsa/node-red-corbit-sms)
351
+ </script>