-
Notifications
You must be signed in to change notification settings - Fork 2
/
createSession.js
45 lines (35 loc) · 1.13 KB
/
createSession.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
import twilio from "twilio";
const AccessToken = twilio.jwt.AccessToken;
const ChatGrant = AccessToken.ChatGrant;
export default async function createSession(
accountSid,
apiKey,
apiSecret,
identity
) {
const client = twilio(apiKey, apiSecret, { accountSid });
// Grab the first conversations service. In the future you should be able to choose this.
const { sid: serviceSid } =
(await client.conversations.v1.services.list({ limit: 1 }))?.[0] ?? null;
// eslint-disable-next-line no-console
console.log("Using conversations service " + serviceSid);
const chatGrant = new ChatGrant({
serviceSid,
});
const token = new AccessToken(accountSid, apiKey, apiSecret, {
identity,
});
token.addGrant(chatGrant);
const incomingPhoneNumbers = await client.incomingPhoneNumbers.list();
const phoneNumbers = incomingPhoneNumbers
.filter((phoneNumber) => phoneNumber.capabilities.sms === true)
.map((phoneNumber) => ({
friendlyName: phoneNumber.friendlyName,
phoneNumber: phoneNumber.phoneNumber,
sid: phoneNumber.sid,
}));
return {
token: token.toJwt(),
phoneNumbers,
};
}