-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsinch.js
More file actions
67 lines (58 loc) · 2.47 KB
/
Copy pathsinch.js
File metadata and controls
67 lines (58 loc) · 2.47 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
// <copyright file="sinch.js" company="Microsoft Corporation">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
'use strict';
const { ParsedResponse } = require('../models');
// A batch identifier indicates acceptance, not final delivery; delivery status arrives by callback.
const manifest = {
id: 'sinch',
auth: { mode: 'apiKey', keyVaultSecretName: 'sinch-api-token' },
responseMapping: {
Dispatched: 'Continue',
Delivered: 'Continue',
Queued: 'Continue',
Failed: 'Fail',
Rejected: 'Fail',
default: 'Fail',
},
};
function buildRequest({ channel, endpoint, dispatch, credential, env }) {
const headers = {
Authorization: `Bearer ${credential.secret}`,
'Content-Type': 'application/json',
Accept: 'application/json',
};
if (channel === 'voice') {
// Voice uses a separate host; verify that its authentication accepts the configured credential.
const voiceBase = env.SINCH_VOICE_ENDPOINT || 'https://calling.api.sinch.com';
const body = {
method: 'ttsCallout',
ttsCallout: {
destination: { type: 'number', endpoint: dispatch.destination },
text: dispatch.message,
locale: dispatch.locale || 'en-US',
custom: dispatch.correlationId || dispatch.messageId,
},
};
return { url: `${voiceBase}/calling/v1/callouts`, method: 'POST', headers, body: JSON.stringify(body) };
}
const smsBase = endpoint;
const servicePlanId = env.SINCH_SERVICE_PLAN_ID || '';
const body = {
from: env.EPP_PROVIDER_ACCOUNT_NAME || 'Verify',
to: [dispatch.destination],
body: dispatch.message,
client_reference: dispatch.correlationId || dispatch.messageId,
};
return { url: `${smsBase}/xms/v1/${servicePlanId}/batches`, method: 'POST', headers, body: JSON.stringify(body) };
}
function parseResponse({ httpStatus, ok, json }) {
const messageOrCallId = (json && (json.id || json.callId || json._links && json._links.self)) || null;
return new ParsedResponse({
success: ok,
providerHttpStatus: httpStatus,
providerMessageId: typeof messageOrCallId === 'string' ? messageOrCallId : (messageOrCallId && messageOrCallId.href) || null,
providerStatusName: ok ? 'Dispatched' : (json && (json.text || json.status)) || null,
});
}
module.exports = { manifest, buildRequest, parseResponse };