Twilio developer evangelist here.
There seems to be something wrong with your token generation as you get an error when trying to instantiate the Chat client.
The code you're using to generate the token is mostly right, though a little out of date. We can use a ChatGrant
now, rather than the IpMessagingGrant
and there's no need to include an endpointId
any more. I would update your function to:
exports.handler = function(context, event, callback) {
let identity = event.identity;
let AccessToken = Twilio.jwt.AccessToken;
let ChatGrant = AccessToken.ChatGrant;
let chatGrant = new ChatGrant({
serviceSid: context.CHAT_SERVICE_SID,
pushCredentialSid: context.CHAT_PUSH_CREDENTIAL_SID
});
const accessToken = new AccessToken(
context.ACCOUNT_SID,
context.CHAT_TWILIO_API_KEY,
context.CHAT_TWILIO_API_SECRET
);
accessToken.addGrant(chatGrant);
accessToken.identity = identity;
callback(null, { token: accessToken.toJwt() });
};
Also, I would confirm that you have correctly set the following environment variables in the function configuration: CHAT_SERVICE_SID
, CHAT_PUSH_CREDENTIAL_SID
, ACCOUNT_SID
, CHAT_TWILIO_API_KEY
and CHAT_TWILIO_API_SECRET
.
If any of those are missing or incorrect then your token will be invalid.
Let me know how that goes.