getToken() { return new Promise((resolve, reject) => { var appName = this.botConfig.apiaiAccessToken; var deviceId = "server"; // Create a unique ID for the client on their current device var endpointId = "TwilioChat:" + this.botConfig.botIdentity + ":browser"; // Create a "grant" which enables a client to use IPM as a given user, // on a given device var ipmGrant = new IpMessagingGrant({ serviceSid: this.botConfig.serviceSid, endpointId: endpointId }); // Create an access token which we will sign and return to the client, // containing the grant we just created var token = new AccessToken( this.botConfig.accountSid, this.botConfig.signingKeySid, this.botConfig.signingKeySecret ); token.addGrant(ipmGrant); token.identity = this.botConfig.botIdentity; resolve(token.toJwt()); }); }
const generateTwilioJwt = (req, res) => { const token = new AccessToken( config.twilio.accountSid, config.twilio.apiKeySid, config.twilio.apiSecret ); token.identity = req.user.identity; token.addGrant(chatGrant); res.json({ twilioToken: token.toJwt() }) }
app.post('/token/:identity', (request, response) => { const identity = request.params.identity; const accessToken = new AccessToken(config.twilio.accountSid, config.twilio.apiKey, config.twilio.apiSecret); const chatGrant = new ChatGrant({ serviceSid: config.twilio.chatServiceSid, endpointId: `${identity}:browser` }); accessToken.addGrant(chatGrant); accessToken.identity = identity; response.set('Content-Type', 'application/json'); response.send(JSON.stringify({ token: accessToken.toJwt(), identity: identity })); })
res.json({ identity: identity, token: token.toJwt(), })