-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemp
427 lines (341 loc) · 16.4 KB
/
temp
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
BOT DIALOGS
function getParticipationList(people) {
return " **Participantes** :\n\n"
+ people.map(p => "* " + p.name).join('\n\n');
}
dialog.matches("None", [
async (session, args, next) => {
session.endDialog("Acho que não entendi... vc pode pedir ajuda com 'help', 'ajuda', 'socorro' etc para obter a lista de comandos disponíveis");
}
]);
dialog.matches("Help", [
async (session, args, next) => {
let msg = "Atualmente eu posso apenas registrar momentos de vida kung fu para o diário. \n\n "
+ "Você pode pedir algo como: \n\n"
+ `* _"Registrar um momento com o André"_ \n\n`
+ `* _"Informar sobre um almoço com o Cris"_ \n\n`
+ `* _"Gostaria de registrar uma refeição com o Cláudio, a Alice e o Iuri"_ \n\n`
+ `* _"Lançar um jantar com o Si Fu, Pereira, Pedro Oliveira e a Rubia"_ \n\n\n\n`
+ `Lembrando que se vc estiver preso em algum ponto pode usar o comando [cancelar] que eu recomeço a conversa `;
session.endDialog(msg);
}
]);
dialog.matches("Greetings", [
async (session, args, next) => {
session.endDialog("Saudações!");
}
]);
dialog.matches("Thanks", [
async (session, args, next) => {
session.endDialog("Disponha!");
}
]);
dialog.matches("RegisterMoment", [
async (session, args, next) => {
const title_entity = builder.EntityRecognizer.findEntity(args.entities, "title");
const names_entities = builder.EntityRecognizer.findAllEntities(args.entities, "person_name");
let moment = { title: "", people: [], fund_value: 0 };
moment.title = title_entity ? title_entity.entity : "Provimento de Vida Kung Fu";
session.dialogData.moment = moment;
session.beginDialog("/findParticipants", { moment, names: names_entities.map(n => n.entity).join(",") });
},
(session, results, next) => {
if(results.response.moment) {
session.dialogData.moment = results.response.moment;
}
const moment = session.dialogData.moment;
if(results.response.cancel) {
session.endDialog("Ok, cancelando essa operação então");
return;
}
session.beginDialog("/confirmMoment", { moment });
}, async (session, results, next) => {
if(results.response.cancel) {
session.endDialog("Ok, cancelando essa operação então");
return;
}
if(results.response.moment
&& results.response.moment.dirty) {
session.replaceDialog("/confirmMoment", {
moment: results.response.moment
});
return;
}
const moment = results.response.moment || session.dialogData.moment;
try {
session.sendTyping();
await new sql.Request(pool)
.input('participants', sql.VarChar(sql.MAX),
moment.people.map(p => p.person_id).join(',')
)
.input('fund_value', sql.Decimal(10, 2),
moment.fund_value
)
.input('title', sql.VarChar(300),
moment.title
)
.execute(`RegisterMoment`);
session.endDialog("Evento registrado!");
} catch(error) {
session.endDialog("Ocorreu um erro ao registrar o evento: " + error.message);
return;
}
}
]);
bot.dialog("/confirmMoment", [(session, args, temp) => {
if(args.cancel_all) {
session.endDialogWithResult({
response: { cancel: true }
});
return;
}
const moment = args.moment;
session.dialogData.moment = moment;
let msg = "Estou com os seguintes dados: \n\n"
+ ` **Título** : ${moment.title} \n\n`;
if(moment.people.length > 0) {
msg += getParticipationList(moment.people);
}
if(moment.fund_value > 0) {
msg += `\n\n **Valor para o fundo** : ${moment.fund_value} `;
}
session.send(msg);
const fund_value_options = moment.fund_value > 0 ?
"Alterar valor para o fundo"
: "Adicionar valor para o fundo";
builder.Prompts.choice(session,
"Posso confirmar? ",
`Sim|Alterar|${fund_value_options}|Cancelar`,
{ listStyle: builder.ListStyle.button });
}, (session, results, next) => {
let response = results.response;
if(response.index === 0) {
session.dialogData.moment.dirty = false;
session.endDialogWithResult({
response: session.dialogData.moment
});
return;
}
session.dialogData.moment.dirty = true;
if(response.index === 1) {
session.replaceDialog("/askChanges", { moment: session.dialogData.moment } );
return;
}
if(response.index === 2) {
session.replaceDialog("/askFundValue", {
moment: session.dialogData.moment
});
return;
}
session.endDialogWithResult({
response: { cancel: true }
});
}]);
bot.dialog("/askChanges", [(session, args) => {
session.dialogData.moment = args.moment;
builder.Prompts.choice(session,
"O que deseja alterar?",
"Título|Adicionar participante|Remover participante|Cancelar alterações|Cancelar lançamento",
{ listStyle: builder.ListStyle.button });
}, (session, results, next) => {
let response = results.response;
if(response.index === 0) {
session.replaceDialog("/changeTitle", {
moment: session.dialogData.moment
});
return;
}
if(response.index === 1) {
session.replaceDialog("/addParticipant", {
moment: session.dialogData.moment,
});
return;
}
if(response.index === 2) {
session.replaceDialog("/removeParticipant", {
moment: session.dialogData.moment
});
return;
}
if(response.index === 3) {
session.replaceDialog("/confirmMoment", {
moment: session.dialogData.moment
});
return;
}
session.replaceDialog("/confirmMoment", {
cancel_all: true
});
}]
);
bot.dialog("/addParticipant", [(session, args) => {
const moment = args.moment;
session.dialogData.moment = moment;
session.replaceDialog("/askNameAndSearchParticipant",{
moment
});
}]);
bot.dialog("/removeParticipant", [(session, args) => {
const moment = args.moment;
session.dialogData.moment = moment;
builder.Prompts.choice(session,
"Quem deseja remover? ",
moment.people.map(p => p.name),
{ listStyle: builder.ListStyle.button });
}, (session, results, next) => {
const name = results.response.entity;
const moment = session.dialogData.moment;
moment.people = moment.people
.filter(p => p.name != name);
session.replaceDialog("/changeParticipants", { moment });
}]);
bot.dialog("/changeParticipants", [(session, args) => {
session.dialogData.moment = args.moment;
const moment = session.dialogData.moment;
let msg = "Então a lista ficou: \n\n";
if(moment.people.length > 0) {
msg += getParticipationList(moment.people);
}
builder.Prompts.choice(session,
"O que deseja?",
`Adicionar participantes|Remover participante|Continuar lançamento`,
{ listStyle: builder.ListStyle.button });
}, (session, results, next) => {
let response = results.response;
const moment = session.dialogData.moment;
if(response.index === 0) {
session.replaceDialog("/addParticipant", moment);
return;
}
if(response.index === 1) {
session.replaceDialog("/removeParticipant", { moment });
return;
}
session.replaceDialog("/confirmMoment", {
moment: moment
});
}]);
bot.dialog("/changeTitle", [(session, args) => {
session.dialogData.moment = args.moment;
builder.Prompts.text(session, "Poderia informar o título então?");
}, (session, results, next) => {
const moment = session.dialogData.moment;
moment.title = results.response;
session.replaceDialog("/confirmMoment", {
moment: moment
});
}]);
bot.dialog("/askFundValue", [(session, args) => {
session.dialogData.moment = args.moment;
builder.Prompts.number(session, "Poderia informar o valor que será destinado para o fundo?");
}, (session, results, next) => {
const moment = session.dialogData.moment;
moment.fund_value = results.response;
session.replaceDialog("/confirmMoment", {
moment: moment
});
}]);
bot.dialog("/askNameAndSearchParticipant", [(session, args) => {
session.dialogData.moment = args.moment;
builder.Prompts.text(session, "Poderia informar o nome então?");
}, (session, results, next) => {
session.replaceDialog("/findParticipants", {
names: results.response,
moment: session.dialogData.moment
});
}]);
bot.dialog("/findParticipants", [async (session, args) => {
if(args) {
if(args.moment) {
session.dialogData.moment = args.moment;
}
if(args.names) {
session.dialogData.names = args.names;
}
}
let names = session.dialogData.names;
if(!names || names.length == 0) {
//session.replaceDialog("/askNameAndSearchParticipant", { moment: session.dialogData.moment });
//return;
}
session.dialogData.query = [];
if(names != null && names.length > 0) {
try {
session.sendTyping();
const result = await new sql.Request(pool)
.input('names', sql.VarChar(sql.MAX), names)
.execute(`GetPeopleByNameForBot`);
session.dialogData.query = result.recordset;
} catch(error) {
session.endDialogWithResult({
response: {
error: "Ocorreu um erro ao obter os participantes: " + error.message
}
});
return;
}
}
const not_founds = session.dialogData.query.filter(f => !f.found);
const people = session.dialogData.query.filter(f => f.found && f.total == 1);
const many_options = session.dialogData.query.filter(f => f.found && f.total > 1);
if(!session.dialogData.moment.people) {
session.dialogData.moment.people = [];
}
session.dialogData.moment.people = session.dialogData.
moment.people.concat(people.filter(p => {
return !session.dialogData.moment.people.find(p2 => p2.person_id == p.person_id);
}));
if(not_founds.length > 0) {
const msg = not_founds.length == 1?
"não encontrei a seguinte pessoa: " + not_founds[0].name
: "não encontrei as seguintes pessoas: " + not_founds.map(n => n.name).join(",");
session.send(msg);
//session.replaceDialog("/confirmMoment", {
// not_founds, moment: session.dialogData.moment
//});
}
if(many_options.length > 0) {
session.replaceDialog("/choosePersonInList", {
many_options, moment: session.dialogData.moment
});
return;
}
session.endDialogWithResult({
response: { moment: session.dialogData.moment }
});
}]);
bot.dialog("/choosePersonInList", [(session, args) => {
session.dialogData.moment = args.moment;
session.dialogData.current_options = args.many_options.shift();
session.dialogData.many_options = args.many_options;
if(!session.dialogData.current_options) {
session.endDialogWithResult({
response: { moment: session.dialogData.moment }
});
return;
}
let options = JSON.parse(session.dialogData.current_options.options);
options.push({id: -1, name: "Ignorar esse nome"});
session.dialogData.options = options;
builder.Prompts.choice(session,
`O nome '${session.dialogData.current_options.name}' possui algumas opções, qual seria?`,
options.map(p => p.name),
{ listStyle: builder.ListStyle.button });
}, (session, result, next) => {
const moment = session.dialogData.moment;
const many_options = session.dialogData.many_options;
const options = session.dialogData.options;
if(result.response.index === options.length) {
session.replaceDialog("/choosePersonInList", {
moment, many_options
});
return;
}
moment.people.push(session.dialogData.options[result.response.index]);
session.replaceDialog("/choosePersonInList", {
moment, many_options
});
}]);
bot.dialog("/lookForNameOrCreate", [(session, args) => {
console.log("lookForNameOrCreate");
}]);