-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdemo.ts
91 lines (75 loc) · 3.12 KB
/
demo.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
import { question } from 'readline-sync';
import crypto from 'crypto';
import Flow, { NewFlowStateParams } from '../src/core/Flow';
import Machine from '../src/core/Machine';
import ClientMemoryRepository from '../src/repositories/ClientMemory.repository';
import SendMessageConsoleGateway from '../src/gateways/SendMessageConsole.gateway';
import labelEqualsTextOrNumber from '../src/utils/matchFunctions/labelEqualsTextOrNumber';
const clientRepository = new ClientMemoryRepository();
const sendMessageGateway = new SendMessageConsoleGateway()
const flow = new Flow({
defaultStateId: "start",
defaultMatchFunction: labelEqualsTextOrNumber
});
const newFlowStateParams: NewFlowStateParams = {
id: "start",
message: "Olá, aqui é o plínio.",
catchMessage: "Desculpa, não te entendi",
createStateCallback: (newState) => {
const educado = newState.branch("Olá")
.state({
id: "educado",
message: "Que bom saber que você é um cliente educado",
catchMessage: "Eu não te entendi meu bom amigo",
matchFunction: labelEqualsTextOrNumber
});
educado.branch("Sou mesmo!")
.state({
id: "soumemo",
message: "Ish, que arrogância",
catchMessage: "Eu não te entendi arrogante.",
matchFunction: labelEqualsTextOrNumber
});
educado.branch("Será!?")
.state({
id: "sera",
message: "Aí vc me complica compadre",
catchMessage: "Não te entendi compadre",
matchFunction: labelEqualsTextOrNumber
});
const chato = newState.branch("Hum")
.state({
id: "chato",
message: "Que difícil hein, vc é um cara chato, que pena",
catchMessage: "Fala de novo",
matchFunction: labelEqualsTextOrNumber
});
chato.branch("Sou mesmo!")
.state({
id: "souchatomemo",
message: "Ish, que arrogância",
catchMessage: "Eu não te entendi arrogante.",
matchFunction: labelEqualsTextOrNumber
});
chato.branch("Não sou não!")
.state({
id: "naosouchato",
message: "Vamos descobrir",
catchMessage: "Eu não te entendi inoscente.",
matchFunction: labelEqualsTextOrNumber
});
}
}
flow.createState(newFlowStateParams);
const machine = new Machine(flow, clientRepository, sendMessageGateway);
(async () => {
const username = question("Whats your name? ");
const id = crypto.randomUUID().slice(0, 5);
while (true) {
const messageFromUser = question(`${username}: `);
await machine.handleMessage({
id,
text: messageFromUser
});
}
})()