Repository navigation
Expand file tree
/
Copy pathinfobip.js
More file actions
76 lines (70 loc) · 2.99 KB
/
Copy pathinfobip.js
File metadata and controls
76 lines (70 loc) · 2.99 KB
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
'use strict';
const { OUTCOME, ProviderResult, classifyFailure } = require('../providerResult');
const SUCCESS = new Set(['ACCEPTED', 'PENDING', 'DELIVERED']);
const REJECTED = new Set(['REJECTED', 'EXPIRED', 'UNDELIVERABLE']);
const provider = Object.freeze({
name: 'infobip',
authenticationMode: 'oauth',
credentialSpec: Object.freeze({ mode: 'oauth' }),
createRequest({ channel, endpoint, delivery, credential }) {
const messageId = delivery.correlationId || delivery.messageId;
const headers = {
Authorization: ['Bearer', credential.accessToken].join(' '),
'Content-Type': 'application/json',
Accept: 'application/json',
};
if (channel === 'voice') {
return {
url: endpoint,
method: 'POST',
headers,
body: JSON.stringify({
messages: [{
destinations: [{ to: delivery.phoneNumber, messageId }],
text: delivery.message,
language: delivery.locale || 'en',
voice: { name: 'Joanna', gender: 'female' },
}],
}),
};
}
return {
url: endpoint,
method: 'POST',
headers,
body: JSON.stringify({
messages: [{
destinations: [{ to: delivery.phoneNumber, messageId }],
content: { text: delivery.message },
}],
}),
};
},
interpretResponse(response) {
const message = Array.isArray(response.json?.messages) ? response.json.messages[0] : null;
const statusObject = message && typeof message.status === 'object' && !Array.isArray(message.status)
? message.status : null;
const rawStatus = typeof statusObject?.groupName === 'string'
? statusObject.groupName : typeof statusObject?.name === 'string' ? statusObject.name : null;
const status = rawStatus?.toUpperCase() || null;
const recognized = SUCCESS.has(status) || REJECTED.has(status);
const mappedOutcome = SUCCESS.has(status) ? OUTCOME.CONTINUE : OUTCOME.FAIL;
const outcome = response.ok ? mappedOutcome : OUTCOME.FAIL;
return new ProviderResult({
outcome,
statusRecognized: recognized,
providerHttpStatus: response.providerHttpStatus,
providerMessageId: typeof message?.messageId === 'string' ? message.messageId : null,
providerStatusName: status,
providerStatusDescription: typeof statusObject?.description === 'string'
? statusObject.description : null,
failureReason: classifyFailure({
providerHttpStatus: response.providerHttpStatus,
outcome,
statusRecognized: recognized,
validJson: response.validJson,
}),
});
},
});
module.exports = provider;