-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtriggers.ts
108 lines (85 loc) · 2.49 KB
/
triggers.ts
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
import { MemoryStorage, ConsoleAdapter } from 'botbuilder';
import { Topic, prettyConsole, consoleOnTurn, doTopic, startBestScoringChild } from '../src/topical';
interface FlightsStart {
destination: string;
}
class Flights extends Topic<FlightsStart> {
async getStartScore() {
if (this.text && this.text.includes('flight'))
return {
startArgs: {
destination: 'Paris'
},
score: .5
}
}
async onStart(args?: FlightsStart) {
await this.send(`Let's fly to ${ args ? args.destination : 'a city'}!`)
}
async onDispatch() {
if (this.text === 'end') {
await this.end();
return;
}
await this.send(`That's my one trick. Try 'end' to return to the travel bot.`);
}
}
Flights.register();
interface HotelsStart {
chain: string;
}
class Hotels extends Topic <HotelsStart> {
async getStartScore() {
if (this.text && this.text.includes('hotel'))
return {
startArgs: {
chain: 'Hyatt'
},
score: .6
}
}
async onStart(args?: HotelsStart) {
await this.send(`Let's stay at a ${ args ? args.chain : 'hotel'}!`)
}
async onDispatch() {
if (this.text === 'end') {
await this.end();
return;
}
await this.send(`That's my one trick. Try 'end' to return to the travel bot.`);
}
}
Hotels.register();
class Travel extends Topic {
async help() {
await this.send(`I can book flights and hotels.`);
}
async onStart() {
await this.send(`Welcome to the Travel bot!`)
await this.help();
await this.createChild('flights', Flights);
await this.createChild('hotels', Hotels);
console.log();
}
async onDispatch() {
if (!this.text)
return;
if (await this.dispatchToChild())
return;
if (!await startBestScoringChild(this))
await this.send(`I can't do that.`);
}
async onChildEnd() {
await this.send(`Welcome back to the Travel bot!`);
await this.help();
}
}
Travel.register();
// const wst = new WSTelemetry('ws://localhost:8080/server');
// Topic.telemetry = action => wst.send(action);
Topic.init(new MemoryStorage());
consoleOnTurn(
new ConsoleAdapter()
.use(prettyConsole),
context => doTopic(Travel, context)
);