-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerateDirectLineToken.js
More file actions
38 lines (29 loc) · 1.39 KB
/
generateDirectLineToken.js
File metadata and controls
38 lines (29 loc) · 1.39 KB
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
const fetch = require('node-fetch');
const { createUserID } = require('./createUserID');
const { DIRECT_LINE_URL = 'https://directline.botframework.com/' } = process.env;
module.exports = {
generateDirectLineToken: async (userID) => {
const { DIRECT_LINE_SECRET } = process.env;
userID || (userID = await createUserID());
console.log(`Generating Direct Line token using secret "${ DIRECT_LINE_SECRET.substr(0, 3) }...${ DIRECT_LINE_SECRET.substr(-3) }" and user ID "${ userID }"`);
const cres = await fetch(`${ DIRECT_LINE_URL }v3/directline/tokens/generate`, {
body: JSON.stringify({ User: { Id: userID } }),
headers: {
authorization: `Bearer ${ DIRECT_LINE_SECRET }`,
'Content-Type': 'application/json'
},
method: 'POST'
});
if (cres.status === 200) {
const json = await cres.json();
if ('error' in json) {
throw new Error(`Direct Line service responded ${ JSON.stringify(json.error) } while generating new token`);
} else {
const { conversationId: conversationID, ...otherJSON } = json;
return { ...otherJSON, conversationID, userID };
}
} else {
throw new Error(`Direct Line service returned ${ cres.status } while generating new token`);
}
}
};