-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsoprano.js
More file actions
83 lines (74 loc) · 2.72 KB
/
Copy pathsoprano.js
File metadata and controls
83 lines (74 loc) · 2.72 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
77
78
79
80
81
82
83
// <copyright file="soprano.js" company="Microsoft Corporation">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
'use strict';
const { ParsedResponse } = require('../models');
const DEFAULT_VOICE_LANGUAGE = 'en-US';
const VOICE_GENDER = 1;
const VOICE_LOOP = 2;
const manifest = {
id: 'soprano',
auth: { mode: 'oauth' },
responseMapping: {
ENROUTE: 'Continue',
ACCEPTED: 'Continue',
SUBMITTED: 'Continue',
SENT: 'Continue',
DELIVERED: 'Continue',
QUEUED: 'Continue',
FAILED: 'Fail',
REJECTED: 'Fail',
FILTERED: 'Fail',
BLOCKED: 'Block',
default: 'Fail',
},
};
function buildTextToVoice(message, locale) {
const renderedMessage = String(message || '');
const passcodeMatch = renderedMessage.match(/\d{6}/);
if (!passcodeMatch) {
throw new Error('voice message does not contain a six-digit passcode');
}
const passcodeIndex = passcodeMatch.index;
return {
beforePasswordText: renderedMessage.slice(0, passcodeIndex),
password: passcodeMatch[0],
afterPasswordText: renderedMessage.slice(passcodeIndex + passcodeMatch[0].length),
language: typeof locale === 'string' && locale.trim() ? locale : DEFAULT_VOICE_LANGUAGE,
gender: VOICE_GENDER,
loop: VOICE_LOOP,
};
}
function buildRequest({ channel, endpoint, dispatch, credential }) {
const headers = {
'Content-Type': 'application/json',
Accept: 'application/json',
Authorization: `Bearer ${credential.accessToken}`,
};
let destination = String(dispatch.destination || '');
while (destination.startsWith('+')) destination = destination.slice(1);
const body = {
destination,
messageTypes: [channel === 'voice' ? 'voice' : 'sms'],
correlationId: dispatch.correlationId || dispatch.messageId,
shutterMode: false,
};
if (channel === 'voice') {
body.voice = { text2voice: buildTextToVoice(dispatch.message, dispatch.locale) };
} else {
body.text = dispatch.message;
}
return { url: endpoint, method: 'POST', headers, body: JSON.stringify(body) };
}
function parseResponse({ httpStatus, ok, json }) {
const payload = (Array.isArray(json) ? json[0] : json) || {};
const value = payload.status ?? payload.state;
const status = typeof value === 'string' && value ? value.toUpperCase() : 'UNKNOWN';
return new ParsedResponse({
success: ok,
providerHttpStatus: httpStatus,
providerMessageId: (payload.id != null ? String(payload.id) : null) || payload.messageId || null,
providerStatusName: status,
});
}
module.exports = { manifest, buildRequest, parseResponse };