-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathinfobip.js
More file actions
68 lines (59 loc) · 2.29 KB
/
Copy pathinfobip.js
File metadata and controls
68 lines (59 loc) · 2.29 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
// <copyright file="infobip.js" company="Microsoft Corporation">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
'use strict';
const { ParsedResponse } = require('../models');
// Voice integration is unverified; confirm the request format before production use.
const manifest = {
id: 'infobip',
auth: { mode: 'apiKey', keyVaultSecretName: 'infobip-api-key' },
responseMapping: {
ACCEPTED: 'Continue',
PENDING: 'Continue',
DELIVERED: 'Continue',
REJECTED: 'Fail',
EXPIRED: 'Fail',
UNDELIVERABLE: 'Fail',
default: 'Fail',
},
};
function buildRequest({ channel, endpoint, dispatch, credential, env }) {
const base = endpoint;
const senderId = env.EPP_PROVIDER_ACCOUNT_NAME || 'Verify';
const headers = {
Authorization: `App ${credential.secret}`,
'Content-Type': 'application/json',
Accept: 'application/json',
};
if (channel === 'voice') {
const body = {
messages: [{
from: senderId,
destinations: [{ to: dispatch.destination, messageId: dispatch.correlationId || dispatch.messageId }],
text: dispatch.message,
language: dispatch.locale || 'en',
voice: { name: 'Joanna', gender: 'female' },
}],
};
return { url: `${base}/tts/3/advanced`, method: 'POST', headers, body: JSON.stringify(body) };
}
const body = {
messages: [{
sender: senderId,
destinations: [{ to: dispatch.destination, messageId: dispatch.correlationId || dispatch.messageId }],
content: { text: dispatch.message },
}],
};
return { url: `${base}/sms/3/messages`, method: 'POST', headers, body: JSON.stringify(body) };
}
function parseResponse({ httpStatus, ok, json }) {
const firstMessage = json && json.messages && json.messages[0];
const status = (firstMessage && firstMessage.status) || {};
return new ParsedResponse({
success: ok,
providerHttpStatus: httpStatus,
providerMessageId: (firstMessage && firstMessage.messageId) || null,
providerStatusName: (status.groupName || status.name || '').toUpperCase() || null,
});
}
module.exports = { manifest, buildRequest, parseResponse };