-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
executable file
·421 lines (413 loc) · 20.8 KB
/
index.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
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
const MaAPI = require('./app/chatbot_api');
const db = require('./app/utils/DB_helper');
const { createIssue } = require('./app/utils/send_issue');
const DF = require('./app/utils/dialogFlow');
const { sendWarningCSV } = require('./app/utils/admin_menu/warn_admin');
const dialogs = require('./app/utils/dialogs');
const attach = require('./app/utils/attach');
const flow = require('./app/utils/flow');
const help = require('./app/utils/helper');
const timers = require('./app/utils/timers');
const labels = require('./app/utils/labels');
const { sendTestNotification } = require('./app/utils/notificationTest');
module.exports = async function App(context) {
try {
try {
await context.setState({ sessionUser: { ...await context.getUserProfile() } });
} catch (error) {
console.log('error', error);
const { id } = context.session.user;
await context.setState({
sessionUser: {
id,
name: '',
firstName: '',
lastName: '',
profilePic: '',
},
});
}
if (!context.state.dialog || context.state.dialog === '' || (context.event.postback && context.event.postback.payload === 'greetings')) { // because of the message that comes from the comment private-reply
await context.setState({ dialog: 'greetings' });
}
await context.setState({ chatbotData: await MaAPI.getChatbotData(context.event.rawEvent.recipient.id) });
await context.setState({ politicianData: context.state.chatbotData });
// console.log('context.state.chatbotData', context.state.chatbotData);
if (context.state.matricula === true) {
await MaAPI.postRecipient(context.state.chatbotData.user_id, await help.buildRecipientObj(context));
}
db.upsertUser(context.session.user.id, context.state.sessionUser.name);
// MaAPI.getRecipient(context.state.chatbotData.user_id, context.session.user.id);
await timers.deleteTimers(context.session.user.id);
if (context.event.isPostback) {
await context.setState({ lastPBpayload: context.event.postback.payload });
if (context.state.lastPBpayload === 'teste') {
// await context.setState({ dialog: 'adminMenu' });
// await context.setState({ dialog: 'sendFirst' });
} else {
await context.setState({ dialog: context.state.lastPBpayload });
await MaAPI.logFlowChange(context.session.user.id, context.state.chatbotData.user_id,
context.event.postback.payload, context.event.postback.title);
}
} else if (context.event.isQuickReply) {
await context.setState({ lastQRpayload: context.event.quickReply.payload });
if (context.state.lastQRpayload.slice(0, 4) === 'poll') { // user answered poll that came from timer
await context.setState({ answer: context.state.lastQRpayload.replace('poll', '') });
await MaAPI.postPollAnswer(context.session.user.id, context.state.answer, 'dialog');
await MaAPI.logAnsweredPoll(context.session.user.id, context.state.chatbotData.user_id, context.state.answer);
await context.sendText('Agradecemos sua resposta.');
await context.setState({ answer: '', dialog: 'mainMenu' });
} else if (context.state.lastQRpayload === 'adminMenu') {
if (await db.checkUserAdmin(context.session.user.id)) {
await context.setState({ dialog: 'adminMenu' });
} else {
await context.sendText(flow.adminMenu.notAdmin); await context.setState({ dialog: 'greetings' });
}
} else {
await context.setState({ dialog: context.state.lastQRpayload });
await MaAPI.logFlowChange(context.session.user.id, context.state.chatbotData.user_id,
context.state.lastQRpayload, context.state.lastQRpayload);
}
} else if (context.event.isText) {
await context.setState({ whatWasTyped: context.event.message.text });
console.log('context.event.message.text', context.event.message.text);
console.log('process.env.ADMIN_KEYWORD', process.env.ADMIN_KEYWORD);
if (context.state.whatWasTyped === process.env.ADMIN_KEYWORD) {
if (await db.checkUserAdmin(context.session.user.id)) {
await context.setState({ dialog: 'adminMenu' });
} else {
await context.sendText(flow.adminMenu.notAdmin); await context.setState({ dialog: 'greetings' });
}
} else if (context.state.whatWasTyped === process.env.MAIL_TEST) {
if (await db.checkUserAdmin(context.session.user.id)) {
await dialogs.mailTest(context);
}
} else if (context.state.dialog === 'jaSouAluna') {
await context.sendImage(flow.jaSouAluna.gif1);
await dialogs.handleCPF(context);
} else if (context.state.dialog === 'verTurma' || context.state.dialog === 'alunosTurmaCSV') {
if (await db.checkUserAdmin(context.session.user.id)) {
await context.setState({ searchTurma: await db.getTurmaID(context.state.whatWasTyped), dialog: 'alunosTurmaCSV' });
} else {
await context.sendText(flow.adminMenu.notAdmin); await context.setState({ dialog: 'greetings' });
}
} else if (['CPFNotFound', 'invalidCPF', 'validCPF'].includes(context.state.dialog)) {
await dialogs.handleCPF(context);
} else if (context.state.whatWasTyped.toLowerCase() === process.env.RESET && process.env.ENV !== 'prod') {
if (context.state.gotAluna && context.state.gotAluna.turma) {
await labels.unlinkUserToLabelByName(context.session.user.id, context.state.gotAluna.turma, context.state.chatbotData.fb_access_token);
}
await context.setState({ recipient: await MaAPI.getRecipient(context.state.chatbotData.user_id, context.session.user.id) });
if (context.state.recipient && context.state.recipient.extra_fields.labels) {
context.state.recipient.extra_fields.labels.forEach(async (element) => {
await MaAPI.deleteRecipientLabel(context.state.chatbotData.user_id, context.session.user.id, element.name);
});
}
await context.setState({ matricula: '', gotAluna: '' });
} else if (context.state.dialog === 'graficoMedia') {
await context.setState({ dialog: 'graficoMediaEnd' });
} else if (['graficoZipEnd', 'graficoZip'].includes(context.state.dialog)) {
await context.setState({ dialog: 'graficoZipEnd' });
} else if (context.state.dialog === 'mudarTurma') {
await dialogs.adminAlunaCPF(context, 'mudarAskTurma');
} else if (context.state.dialog === 'removerAluna') {
await dialogs.adminAlunaCPF(context, 'removerAlunaConfirma');
} else if (context.state.dialog === 'mudarAskTurma') {
await dialogs.mudarAskTurma(context, context.state.chatbotData.fb_access_token);
} else if (context.state.dialog === 'simularAskCPF') {
await context.setState({ simuladorCPF: await help.getCPFValid(context.state.whatWasTyped) });
if (context.state.simuladorCPF) {
await context.setState({ simuladorAluna: await db.getAlunaFromCPF(context.state.simuladorCPF) });
if (!context.state.simuladorAluna) {
await context.sendText(`Aluna com CPF ${context.state.simuladorCPF} não encontrada`);
} else if (context.state.simuladorAluna && !context.state.simuladorAluna.turma_id) {
await context.sendText(`Aluna ${context.state.simuladorAluna.nome_completo} não está em nenhuma turma. Ela não pode receber as notificações.`);
await context.setState({ dialog: 'adminMenu' });
} else {
await context.setState({ dialog: 'simularNotificacao' });
}
} else {
await context.sendText('CPF inválido.');
}
} else if (['sendFeedback', 'sendFeedbackConfirm'].includes(context.state.dialog)) {
await context.setState({ dialog: 'sendFeedbackConfirm' });
} else {
await DF.dialogFlow(context);
}
} else if (context.event.isFile && context.event.file && context.event.file.url) {
await dialogs.checkReceivedFile(context);
}
switch (context.state.dialog) {
case 'greetings':
if (context.state.matricula === true) {
await dialogs.sendMainMenu(context);
} else {
if (context.state.sessionUser.firstName) {
await context.sendText(flow.greetings.text1.replace('<first_name>', context.state.sessionUser.firstName));
} else {
await context.sendText(flow.greetings.text1extra);
}
await context.sendText(flow.greetings.text2);
await context.sendText(flow.greetings.text3, await attach.getQR(flow.greetings));
}
break;
case 'mainMenu':
await dialogs.sendMainMenu(context);
break;
case 'sobreElas':
await context.sendText(flow.sobreElas.text1);
await context.sendText(flow.sobreElas.text2);
await context.sendText(flow.sobreElas.text3);
await context.sendImage(flow.sobreElas.gif1);
if (context.state.matricula === true) {
await dialogs.sendMainMenu(context);
} else {
await context.sendText(flow.greetings.text3, await attach.getQR(flow.sobreElas));
}
break;
case 'trocarTurma':
await context.sendText(flow.trocarTurma.text1);
await context.sendText(flow.trocarTurma.text2, await attach.getQR(flow.trocarTurma));
break;
case 'queroTrocar':
await context.sendText(flow.trocarTurma.text3);
await dialogs.warnAlunaTroca(context.state.gotAluna);
await dialogs.sendMainMenu(context);
break;
case 'queroSerAluna':
await context.sendText(flow.queroSerAluna.text1);
await attach.sendCards(context, flow.queroSerAluna.cards, 'Ver Mais');
await context.sendText(flow.queroSerAluna.text2);
await context.sendText(flow.queroSerAluna.text3);
await context.sendText(flow.greetings.text3, await attach.getQR(flow.queroSerAluna));
break;
case 'jaSouAluna':
await context.sendText(flow.jaSouAluna.text1);
await context.sendText(flow.jaSouAluna.text2);
await context.sendText(flow.jaSouAluna.text3);
break;
case 'validCPF':
if (context.state.gotAluna) {
await context.sendText(flow.jaSouAluna.validCPF.replace('<name>', context.state.gotAluna.nome_completo).replace('<turma>', context.state.gotAluna.turma.replace('turma', '')),
await attach.getQR(flow.jaSouAluna));
} else {
await context.sendText('Não achei sua turma');
}
break;
case 'invalidCPF':
await context.sendText(flow.invalidCPF.text1);
await context.sendText(flow.invalidCPF.text2, await attach.getQR(flow.invalidCPF));
break;
case 'CPFNotFound':
await context.sendText(flow.CPFNotFound.text1, await attach.getQR(flow.CPFNotFound));
break;
case 'confirmaMatricula':
await db.linkUserToCPF(context.session.user.id, context.state.cpf);
await MaAPI.postRecipient(context.state.chatbotData.user_id, await help.buildRecipientObj(context));
await MaAPI.postRecipientLabel(context.state.chatbotData.user_id, context.session.user.id, context.state.gotAluna.turma);
await labels.linkUserToLabelByName(context.session.user.id, context.state.gotAluna.turma, context.state.chatbotData.fb_access_token, true);
await context.setState({ matricula: true, agendaData: await dialogs.getAgenda(await db.getTurmaFromID(context.state.gotAluna.turma_id)) });
await context.sendText(flow.confirmaMatricula.text1);
await context.sendText(await dialogs.buildAgendaMsg(context.state.agendaData), await attach.getQR(flow.confirmaMatricula));
break;
case 'afterConfirma':
await context.sendText(flow.confirmaMatricula.after1);
await dialogs.sendMainMenu(context);
break;
case 'erradoMatricula':
await db.linkUserToCPF(context.session.user.id, '');
await context.sendText(flow.erradoMatricula.text1, await attach.getQR(flow.erradoMatricula));
break;
case 'talkToElas':
await context.sendText(flow.talkToElas.text1);
await context.sendText(flow.talkToElas.text2);
await context.sendText(flow.talkToElas.text3);
break;
case 'falarDonna':
await context.sendText('Escreva suas dúvidas que eu vou tentar responder.');
await timers.createFalarDonnaTimer(context.session.user.id, context);
break;
case 'Atividade2':
await context.setState({ gotAluna: await db.getAlunaFromFBID(context.session.user.id) });
await context.setState({ ourTurma: await db.getTurmaFromID(context.state.gotAluna.turma_id) });
await context.setState({ mod1Date: context.state.ourTurma.horario_modulo1 });
await context.sendText(flow.Atividade2.text1.replace('[MOD1_15DIAS]', await help.formatDiasMod(context.state.mod1Date, -15)));
await attach.sendAtividade2Cards(context, await help.replaceVarOnCards(flow.Atividade2.cards, '[DISC_LINK]', await db.getDISCFromID(context.state.gotAluna.turma_id)), context.state.cpf);
await context.sendText(flow.Atividade2.text2.replace('[MOD1_2DIAS]', await help.formatDiasMod(context.state.mod1Date, -2)), await attach.getQR(flow.Atividade2));
await context.setState({ spreadsheet: '', ourTurma: '', mod1Date: '' });
break;
case 'mail6pt2':
await context.sendText(flow.mail6pt2.text1, await attach.getQR(flow.mail6pt2));
break;
case 'mail6pt3':
await context.sendText(flow.mail6pt3.text1.replace('<LINK_ANEXO>', await help.getTinyUrl(process.env.ANEXO_MAIL06)), await attach.getQR(flow.mail6pt3));
break;
case 'fim':
await context.sendText('fim');
break;
case 'createIssueDirect':
await createIssue(context);
break;
// adminMenu -----------------------------------------------------------------------------
case 'adminMenu':
await context.sendText(flow.adminMenu.firstMenu.txt1, await attach.getQR(flow.adminMenu.firstMenu));
break;
case 'inserirAlunas':
await context.sendText(flow.adminMenu.inserirAlunas.txt1);
await context.sendText(await help.getTinyUrl(process.env.CSV_ALUNA_EXAMPLE_LINK));
await context.sendText(flow.adminMenu.inserirAlunas.txt2, await attach.getQR(flow.adminMenu.inserirAlunas));
break;
case 'createAlunos': {
const result = await dialogs.receiveCSVAluno(context.state.csvLines, context.state.chatbotData.user_id, context.state.chatbotData.fb_access_token);
if (result) {
await dialogs.sendFeedbackMsgs(context, result.errors, flow.adminMenu.feedback.aluna);
} else {
await context.sendText(flow.adminMenu.inserirAlunas.invalidFile, await attach.getQR(flow.adminMenu.inserirAlunas));
}
await context.setState({ csvLines: '' });
}
break;
case 'inserirAvaliadores':
await context.sendText(flow.adminMenu.inserirAvaliadores.txt1);
await context.sendText(await help.getTinyUrl(process.env.CSV_AVALI_EXAMPLE_LINK));
await context.sendText(flow.adminMenu.inserirAvaliadores.txt2);
await context.sendText(flow.adminMenu.inserirAvaliadores.txt3);
await context.sendText(flow.adminMenu.inserirAvaliadores.txt4, await attach.getQR(flow.adminMenu.inserirAvaliadores));
break;
case 'createAvaliadores': {
const result = await dialogs.receiveCSVAvaliadores(context.state.csvLines);
if (result) {
await dialogs.sendFeedbackMsgs(context, result.errors, flow.adminMenu.feedback.indicado);
} else {
await context.sendText(flow.adminMenu.inserirAvaliadores.invalidFile, await attach.getQR(flow.adminMenu.inserirAvaliadores));
}
await context.setState({ csvLines: '' });
}
break;
case 'verTurma':
await context.sendText(flow.adminMenu.verTurma.txt1, await attach.getQR(flow.adminMenu.verTurma));
break;
case 'avisoResposta':
await context.sendText(flow.adminMenu.avisoResposta.txt1, await attach.getQR(flow.adminMenu.verTurma));
await context.typing(3000);
await sendWarningCSV(true);
break;
case 'alunosTurmaCSV':
if (context.state.searchTurma) {
for (let i = 0; i < Object.keys(flow.adminCSV).length; i++) {
const element = Object.keys(flow.adminCSV)[i];
await context.setState({ dialog: element });
await dialogs.sendCSV(context, context.state.searchTurma);
}
// await dialogs.sendStatus(context, context.state.searchTurma, 'status.csv');
// await dialogs.sendStatusIndicado(context, context.state.searchTurma, 'status_indicado.csv');
await context.sendText(flow.adminMenu.verTurma.txt2, await attach.getQR(flow.adminMenu.verTurma));
await context.setState({ dialog: 'alunosTurmaCSV' });
} else {
await context.sendText(flow.adminMenu.verTurma.noTurma, await attach.getQR(flow.adminMenu.verTurma));
}
break;
case 'mudarTurma':
await context.sendText(flow.adminMenu.mudarTurma.txt1, await attach.getQR(flow.adminMenu.mudarTurma));
break;
case 'mudarAskTurma': {
let text = flow.adminMenu.mudarTurma.txt2;
if (context.state.adminAlunaFound.turma) text += flow.adminMenu.mudarTurma.txt3.replace('<NOME>', context.state.adminAlunaFound.nome_completo.trim()).replace('<TURMA>', context.state.adminAlunaFound.turma);
await context.sendText(text, await attach.getQR(flow.adminMenu.verTurma));
} break;
// case 'updateTurma': {
// const feedback = await updateTurmas();
// await context.setState({ csvLines: feedback && feedback.results ? feedback.results.length : 0 });
// if (feedback && feedback.results && feedback.results.length > 0) await context.sendText(feedback.results.join('\n'), await attach.getQR(flow.adminMenu.atualizarTurma));
// if (feedback && feedback.errors && feedback.errors.length > 0) await dialogs.sendFeedbackMsgs(context, feedback.errors, '', flow.adminMenu.atualizarTurma);
// } break;
case 'removerAluna':
await context.sendText(flow.adminMenu.removerAluna.txt1, await attach.getQR(flow.adminMenu.removerAluna));
break;
case 'removerAlunaConfirma':
if (context.state.adminAlunaFound.turma) {
let text = flow.adminMenu.removerAlunaConfirma.txt1;
text += flow.adminMenu.removerAlunaConfirma.txt2.replace('<NOME>', context.state.adminAlunaFound.nome_completo.trim()).replace('<TURMA>', context.state.adminAlunaFound.turma);
await context.sendText(text, await attach.getQR(flow.adminMenu.removerAlunaConfirma));
} else {
await context.sendText(flow.adminMenu.removerAlunaConfirma.noTurma.replace('<NOME>', context.state.adminAlunaFound.nome_completo.trim()), await attach.getQR(flow.adminMenu.firstMenu));
}
break;
case 'removerAlunaFim':
await dialogs.removerAluna(context);
break;
case 'graficos':
await context.sendText(flow.adminMenu.graficos.txt1, await attach.getQR(flow.adminMenu.graficos));
break;
case 'graficoMedia':
await context.sendText(flow.adminMenu.graficos.txt2, await attach.getQR(flow.adminMenu.verTurma));
break;
case 'graficoMediaEnd':
await dialogs.graficoMediaEnd(context);
break;
case 'graficoZip':
await context.sendText(flow.adminMenu.graficos.txt3, await attach.getQR(flow.adminMenu.verTurma));
break;
case 'graficoZipEnd':
await dialogs.graficoZipEnd(context);
break;
case 'sendFeedback':
await context.sendText(flow.adminMenu.sendFeedback.askTurma);
break;
case 'sendFeedbackConfirm':
await dialogs.sendFeedbackConfirm(context);
break;
case 'sendFeedbackFim':
await dialogs.sendFeedbackFim(context.state.feedbackTurmaID, await db.getTurmaInCompany(context.state.feedbackTurmaID));
await context.sendText(flow.adminMenu.sendFeedback.fim);
await context.sendText(flow.adminMenu.firstMenu.txt1, await attach.getQR(flow.adminMenu.firstMenu));
break;
case 'simularNotificacao':
await context.sendText(flow.adminMenu.simularNotificacao.intro, await attach.getQR(flow.adminMenu.simularNotificacao));
break;
case 'simularAskCPF':
await context.sendText(flow.adminMenu.simularNotificacao.askCPF, await attach.getQR(flow.adminMenu.verTurma));
break;
case 'simularAll':
await sendTestNotification(context.state.simuladorAluna.cpf);
await context.sendText('Aguarde', await attach.getQR(flow.adminMenu.verTurma));
break;
case 'simularTrilha':
await sendTestNotification('41734249811', false, false, [3, 10, 12, 15, 16]);
await context.sendText('Aguarde', await attach.getQR(flow.adminMenu.verTurma));
break;
case 'simularIndicado':
await sendTestNotification(context.state.simuladorAluna.cpf, false, true);
await context.sendText('Aguarde', await attach.getQR(flow.adminMenu.verTurma));
break;
case 'simular24H':
await sendTestNotification(context.state.simuladorAluna.cpf, 15);
await context.sendText('Aguarde', await attach.getQR(flow.adminMenu.verTurma));
break;
case 'simular1H':
await sendTestNotification(context.state.simuladorAluna.cpf, 16);
await context.sendText('Aguarde', await attach.getQR(flow.adminMenu.verTurma));
break;
case 'notificationOn':
await MaAPI.updateBlacklistMA(context.session.user.id, 1);
await MaAPI.logNotification(context.session.user.id, context.state.chatbotData.user_id, 3);
await context.sendText(flow.notifications.on);
break;
case 'notificationOff':
await MaAPI.updateBlacklistMA(context.session.user.id, 0);
await MaAPI.logNotification(context.session.user.id, context.state.chatbotData.user_id, 4);
await context.sendText(flow.notifications.off);
break;
} // end switch case
} catch (error) {
await context.sendText('Ops. Tive um erro interno. Tente novamente.'); // warning user
const date = new Date();
const errorMsg = `Parece que aconteceu um erro as ${date.toLocaleTimeString('pt-BR')} de ${date.getDate()}/${date.getMonth() + 1} com ${context.state.sessionUser.name} => \n${error.stack}`;
console.log('errorMsg', errorMsg);
await help.Sentry.configureScope(async (scope) => { // sending to sentry
scope.setUser({ username: context.state.sessionUser.firstName });
scope.setExtra('state', context.state);
throw error;
});
} // catch
}; // handler function