Untitled
unknown
plain_text
2 years ago
1.9 kB
16
Indexable
// Gen token by id
// ex: https://twiliochatroomaccesstoken-4974.twil.io/test_token?identity=Tompei
exports.handler = function(context, event, callback) {
const AccessToken = require('twilio').jwt.AccessToken;
const VoiceGrant = AccessToken.VoiceGrant;
const twilioAccountSid = context.ACCOUNT_SID;
const twilioApiKey = context.API_KEY_SID;
const twilioApiSecret = context.API_SECRET;
const outgoingApplicationSid = context.APP_SID;
const pushCredentialSid = context.PUSH_CREDENTIAL_SID;
const identity = event.identity || 'alice';
const voiceGrant = new VoiceGrant({
outgoingApplicationSid: outgoingApplicationSid,
pushCredentialSid: pushCredentialSid
});
const token = new AccessToken(twilioAccountSid, twilioApiKey, twilioApiSecret, {ttl: 20157});
token.addGrant(voiceGrant);
token.identity = identity;
callback(null, token.toJwt());
};
// makeCall
const callerNumber = '1234567890';
const callerId = 'client:Tom';
exports.handler = function(context, event, callback) {
const twiml = new Twilio.twiml.VoiceResponse();
var to = (event.to) ? event.to : event.To;
if (!to) {
twiml.say('Congratulations! You have made your first call! Good bye.');
} else if (isNumber(to)) {
const dial = twiml.dial({callerId : callerNumber});
dial.number(to);
} else {
const dial = twiml.dial({callerId : callerId});
dial.client(to);
}
callback(null, twiml);
};
function isNumber(to) {
if(to.length == 1) {
if(!isNaN(to)) {
console.log("It is a 1 digit long number" + to);
return true;
}
} else if(String(to).charAt(0) == '+') {
number = to.substring(1);
if(!isNaN(number)) {
console.log("It is a number " + to);
return true;
};
} else {
if(!isNaN(to)) {
console.log("It is a number " + to);
return true;
}
}
console.log("not a number");
return false;
}Editor is loading...