-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatbotpoc2.js
93 lines (88 loc) · 2.45 KB
/
chatbotpoc2.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
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
const http = require('http');
const url = require('url');
const fs = require('fs');
const server = http.createServer((req, res) => {
const { pathname } = url.parse(req.url, true);
if (pathname === '/') {
const indexHTML = `
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Chatbot POC</title>
<script src="https://cdn.botframework.com/botframework-webchat/latest/webchat.js"></script>
<style>
#webchat {
height: 100%;
width: 100%;
}
html, body {
height: 100%;
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<div id="webchat"></div>
<script>
(async function() {
const res = await fetch('https://directline.botframework.com/v3/directline/tokens/generate', {
method: 'POST',
headers: {
Authorization: 'PUT TOKEN HERE',
'Content-Type': 'application/json'
}
});
const { token } = await res.json();
window.WebChat.renderWebChat({
directLine: window.WebChat.createDirectLine({
token
}),
userID: 'USER_ID',
username: 'User',
locale: 'en-US',
botAvatarInitials: 'CB',
userAvatarInitials: 'WC'
}, document.getElementById('webchat'));
})();
</script>
</body>
</html>
`;
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(indexHTML);
} else if (pathname === '/api/messages') {
let body = '';
req.on('data', chunk => {
body += chunk.toString();
});
req.on('end', () => {
const options = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: 'PUT TOKEN HERE'
},
body
};
fetch('PUT AZURE CLOUD URL HERE', options)
.then(res => res.json())
.then(data => {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(data));
})
.catch(err => {
console.error(err);
res.writeHead(500, { 'Content-Type': 'text/plain' });
res.end('Server Error');
});
});
} else {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('404 Not Found');
}
});
server.listen(8080, () => {
console.log('Server running at http://localhost:8080/');
});