-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsimple.js
62 lines (46 loc) · 1.45 KB
/
simple.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
const { MemoryStorage, ConsoleAdapter } = require('botbuilder');
const { Topic, prettyConsole, consoleOnTurn, doTopic, hasNumber } = require ('../lib/src/topical.js');
class Echo extends Topic {
async onStart(args) {
await this.send(`Welcome to the child topic! Say 'end child' to go back to the root.`);
}
async onDispatch() {
if (this.text === 'end child')
await this.end();
else
await this.send(`Child Topic: you said "${this.text}"`);
}
}
Echo.register();
class RootTopic extends Topic {
async help() {
await this.send(`Try "start child" or "time".`);
}
async onStart() {
await this.send(`Welcome to my root topic!`);
await this.help();
}
async onDispatch() {
if (!this.text)
return;
if (this.text.includes('time')) {
await this.send(`The current time is ${new Date().toLocaleTimeString()}`);
return;
}
if (await this.dispatchToChild())
return;
if (this.text === 'start child') {
return this.startChild(Echo);
}
await this.help();
}
async onChildEnd() {
await this.send(`Welcome back from the child topic!`);
await this.help();
}
}
RootTopic.register();
Topic.init(new MemoryStorage());
const adapter = new ConsoleAdapter()
.use(prettyConsole);
consoleOnTurn(adapter, context => doTopic(RootTopic, context));