Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
780 B
2
Indexable
Never
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());
};