-
Notifications
You must be signed in to change notification settings - Fork 130
Expand file tree
/
Copy pathcreateDirectLine.js
More file actions
97 lines (81 loc) · 2.94 KB
/
createDirectLine.js
File metadata and controls
97 lines (81 loc) · 2.94 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { userId as DEFAULT_USER_ID } from '../constants.json';
import { DirectLine, DirectLineStreaming } from '../../dist/directline';
const {
DIRECT_LINE_SECRET,
STREAMING_EXTENSIONS_DOMAIN = 'https://dljstestbot.azurewebsites.net/.bot/v3/directline'
} = process.env;
const DEFAULT_DOMAIN = 'https://directline.botframework.com/v3/directline';
async function fetchDirectLineToken() {
const res = await fetch('https://dljstestbot.azurewebsites.net/token/directline');
if (res.ok) {
return await res.json();
} else {
throw new Error(`Server returned ${ res.status } while fetching Direct Line token`);
}
}
async function fetchDirectLineStreamingExtensionsToken() {
const res = await fetch(`https://dljstestbot.azurewebsites.net/token/directlinease`);
if (res.ok) {
return await res.json();
} else {
throw new Error(`Server returned ${ res.status } while fetching Direct Line token`);
}
}
async function generateDirectLineToken(domain = DEFAULT_DOMAIN) {
let res;
res = await fetch(`${ domain }/tokens/generate`, {
body: JSON.stringify({ User: { Id: DEFAULT_USER_ID } }),
headers: {
authorization: `Bearer ${ DIRECT_LINE_SECRET }`,
'Content-Type': 'application/json'
},
method: 'POST'
});
if (res.status === 200) {
const json = await res.json();
if ('error' in json) {
throw new Error(`Direct Line service responded with ${ JSON.stringify(json.error) } while generating a new token`);
} else {
return json;
}
} else {
throw new Error(`Direct Line service returned ${ res.status } while generating a new token`);
}
}
export async function forREST({ token } = {}, mergeOptions = {}) {
let options = { webSocket: false };
if (token && DIRECT_LINE_SECRET) {
options = { ...options, token: (await generateDirectLineToken()).token };
} else if (token) {
// Probably via PR validation on Travis, or run by a contributing developer.
// We still want to let the developer to test majority of stuff without deploying their own bot server.
options = { ...options, token: (await fetchDirectLineToken()).token };
} else if (DIRECT_LINE_SECRET) {
options = { ...options, secret: DIRECT_LINE_SECRET };
} else {
return console.warn('Tests using secret are skipped because DIRECT_LINE_SECRET environment variable is not defined.');
}
return new DirectLine({ ...options, ...mergeOptions });
}
export async function forStreamingExtensions(mergeOptions = {}) {
const { conversationId, token } = DIRECT_LINE_SECRET ?
await generateDirectLineToken(STREAMING_EXTENSIONS_DOMAIN)
:
await fetchDirectLineStreamingExtensionsToken();
return new DirectLineStreaming({
conversationId,
domain: STREAMING_EXTENSIONS_DOMAIN,
token,
webSocket: true,
...mergeOptions
});
}
export async function forWebSocket({ token } = {}, mergeOptions = {}) {
return await forREST(
{ token },
{
webSocket: true,
...mergeOptions
}
);
}