-
Notifications
You must be signed in to change notification settings - Fork 8
/
customContext.ts
69 lines (52 loc) · 1.68 KB
/
customContext.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
import { TurnContext, MemoryStorage, ConsoleAdapter } from 'botbuilder';
import { Topic, prettyConsole, WSTelemetry, consoleOnTurn, doTopic, PromptArgs, Prompt, hasText } from '../src/topical';
class CustomContext extends TurnContext {
foo = "hey"
}
class Child extends Topic<any, any, any, CustomContext> {
async onStart() {
await this.send(this.context.foo);
await this.end();
}
}
Child.register();
class PromptForText extends Prompt<string, PromptArgs, CustomContext> {
validator = hasText;
prompter = async () => {
await this.send(this.context.foo);
await this.send(this.state.args!.prompt!);
}
}
PromptForText.register();
class Root extends Topic<any, any, any, CustomContext> {
async onStart() {
await this.startChild(Child);
}
async onDispatch() {
if (!this.text)
return;
if (await this.dispatchToChild())
return;
await this.send(`That's all I've got.`)
}
async onChildEnd(child: Topic) {
if (child instanceof Child) {
await this.send(this.context.foo);
await this.startChild(PromptForText, {
prompt: 'Wassup?',
});
} else if (child instanceof PromptForText) {
await this.send(`You said ${child.return!.result.value}`);
} else
throw "mystery child topic";
}
}
Root.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(Root, new CustomContext(context))
);