forked from Azure/bot-trees
-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.js
125 lines (107 loc) · 3.85 KB
/
router.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
var path = require('path');
var express = require('express');
var builder = require('botbuilder');
var GraphDialog = require('bot-graph-dialog');
var config = require('./config');
var fs = require('fs');
var port = process.env.PORT || 3978;
var app = express();
var microsoft_app_id = config.get('MICROSOFT_APP_ID');
var microsoft_app_password = config.get('MICROSOFT_APP_PASSWORD');
var connector = new builder.ChatConnector({
appId: microsoft_app_id,
appPassword: microsoft_app_password,
});
var bot = new builder.UniversalBot(connector).set('storage', builder.MemoryBotStorage());
var intents = new builder.IntentDialog();
var scenariosPath = path.join(__dirname, 'bot', 'scenarios');
var handlersPath = path.join(__dirname, 'bot', 'handlers');
bot.dialog('/', intents);
intents.matches(/^(help|hi|hello)/i, [
function (session) {
session.send('Hi, how can I help you?');
}
]);
process.nextTick(async () => {
try {
var graphDialog = await GraphDialog.create({
bot,
scenario: 'router',
loadScenario,
loadHandler,
customTypeHandlers: getCustomTypeHandlers()
});
intents.onDefault(graphDialog.getDialog());
}
catch(err) {
console.error(`error loading dialog: ${err.message}`)
}
});
// this allows you to extend the json with more custom node types,
// by providing your implementation to processing each custom type.
// in the end of your implemention you should call the next callbacks
// to allow the framework to continue with the dialog.
// refer to the customTypeStepDemo node in the stomachPain.json scenario for an example.
function getCustomTypeHandlers() {
return [
{
name: 'myCustomType',
execute: (session, next, data) => {
console.log(`in custom node type handler: customTypeStepDemo, data: ${data.someData}`);
return next();
}
}
];
}
// this is the handler for loading scenarios from external datasource
// in this implementation we're just reading it from a file
// but it can come from any external datasource like a file, db, etc.
function loadScenario(scenario) {
return new Promise((resolve, reject) => {
console.log('loading scenario', scenario);
// implement loadScenario from external datasource.
// in this example we're loading from local file
var scenarioPath = path.join(scenariosPath, scenario + '.json');
return fs.readFile(scenarioPath, 'utf8', (err, content) => {
if (err) {
console.error("error loading json: " + scenarioPath);
return reject(err);
}
var scenarioObj = JSON.parse(content);
// simulating long load period
setTimeout(() => {
console.log('resolving scenario', scenarioPath);
resolve(scenarioObj);
}, Math.random() * 3000);
});
});
}
// this is the handler for loading handlers from external datasource
// in this implementation we're just reading it from a file
// but it can come from any external datasource like a file, db, etc.
//
// NOTE: handlers can also be embeded in the scenario json. See scenarios/botGames.json for an example.
function loadHandler(handler) {
return new Promise((resolve, reject) => {
console.log('loading handler', handler);
// implement loadHandler from external datasource.
// in this example we're loading from local file
var handlerPath = path.join(handlersPath, handler);
var handlerString = null;
return fs.readFile(handlerPath, 'utf8', (err, content) => {
if (err) {
console.error("error loading handler: " + handlerPath);
return reject(err);
}
// simulating long load period
setTimeout(() => {
console.log('resolving handler', handler);
resolve(content);
}, Math.random() * 3000);
});
});
}
app.post('/api/messages', connector.listen());
app.listen(port, () => {
console.log('listening on port %s', port);
});