-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsoprano.js
More file actions
71 lines (65 loc) · 2.36 KB
/
Copy pathsoprano.js
File metadata and controls
71 lines (65 loc) · 2.36 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
// <copyright file="soprano.js" company="Microsoft Corporation">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
'use strict';
const { ParsedResponse, TextToVoice } = require('../models');
const manifest = {
id: 'soprano',
requiresTextToVoice: true,
auth: {
mode: 'apiKey',
keyVaultSecretName: 'soprano-api-key',
identityKeyVaultSecretName: 'soprano-api-id',
},
responseMapping: {
ENROUTE: 'Continue',
ACCEPTED: 'Continue',
SUBMITTED: 'Continue',
SENT: 'Continue',
DELIVERED: 'Continue',
QUEUED: 'Continue',
FAILED: 'Fail',
REJECTED: 'Fail',
FILTERED: 'Fail',
BLOCKED: 'Block',
default: 'Fail',
},
};
function buildRequest({ channel, endpoint, dispatch, credential }) {
let base = endpoint;
while (base.endsWith('/')) base = base.slice(0, -1);
const headers = {
'Content-Type': 'application/json',
Accept: 'application/json',
'X-MEMS-API-ID': credential.identity,
'X-MEMS-API-Key': credential.secret,
};
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') {
const voice = dispatch.textToVoice;
if (!(voice instanceof TextToVoice) || !voice.isComplete) throw new Error('incomplete voice context');
body.voice = { text2voice: voice };
} else {
body.text = dispatch.message;
}
return { url: `${base}/messages/omnimsg`, 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 };