-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathvoice.js
56 lines (47 loc) · 1.71 KB
/
voice.js
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
const { TwilioClientCommand } = require('@twilio/cli-core').baseCommands;
const Twilio = require('twilio');
const createToken = require('../../helpers/accessToken.js');
const globalFlags = require('../../helpers/globalFlags.js');
const { voiceFlags, validateTwimlAppSid, validatePushCredentialSid } = require('../../helpers/voiceGlobals.js');
class VoiceTokenGenerator extends TwilioClientCommand {
constructor(argv, config) {
super(argv, config);
this.showHeaders = true;
}
async run() {
await super.run();
const accessToken = createToken.call(this);
if (!validateTwimlAppSid(this.flags['voice-app-sid'])) {
this.logger.error(
'Invalid TwiML Application SID, must look like APxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
);
process.exit(1);
}
let pushCredentialSid = this.flags['push-credential-sid'];
if (pushCredentialSid && !validatePushCredentialSid(pushCredentialSid)) {
this.logger.error(
'Invalid Push Credential SID, must look like CRxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
);
process.exit(1);
}
let incomingAllow = (this.flags['allow-incoming'] == 'true');
let voiceGrant = new Twilio.jwt.AccessToken.VoiceGrant({
outgoingApplicationSid: this.flags['voice-app-sid'],
incomingAllow
});
if (pushCredentialSid) {
voiceGrant.pushCredentialSid = pushCredentialSid;
}
accessToken.addGrant(voiceGrant);
this.logger.info('Copy/paste this voice token into your test application:');
this.output({ jwt: accessToken.toJwt() }, undefined, {
showHeaders: false,
});
}
}
VoiceTokenGenerator.flags = Object.assign(
voiceFlags,
TwilioClientCommand.flags,
globalFlags,
);
module.exports = VoiceTokenGenerator;