-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathworkflow.py
228 lines (193 loc) · 6.54 KB
/
workflow.py
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
# -*- coding: utf-8 -*-
import io
import json
import pprint
from datetime import date
from celery.utils.log import get_task_logger
import api_client as api
import mongocon
logger = get_task_logger(__name__)
# carrega workflows
with io.open('conf/workflowsconfig.json', 'r', encoding='utf-8') as f:
workflows = json.load(f)
# constroi dicionario com a mensagem como chave
fluxos, mensagens = workflows['fluxos'], workflows['mensagens']
# menu principal de botoes
menu_principal = [a for (a, b) in sorted([x for x in fluxos.items() if x[1]['menu']],
key=lambda x: x[1].get('ordem'))]
# menu principal de botoes para subscritos
menu_subscritos = menu_principal[:]
menu_subscritos[-1] = 'Cancelar notificações'
def _format(text, args=list()):
return '\n\n'.join(text).format(*args)
# processadores
# @params: chat, db (Opcional)
# @return: resposta, botoes
def process_cardapio(chat, db=None):
escola, idade, data = chat['valores']
# busca cardapio
try:
cardapio = api.get_cardapio(escola['_id'], idade, data)
except:
# erro na API
return _format(mensagens['erro_api']['texto']), menu_principal
# processa resposta
if len(cardapio) > 0:
cardapio = cardapio.pop()
cardapio_str = ['{}:\n{}'.format(k, ', '.join(v)) for (k, v) in sorted(cardapio['cardapio'].items())]
else:
cardapio_str = ['Ops! Não encontramos nenhuma informação de cardápio para este dia.']
resposta = fluxos[chat['fluxo']]['resposta']
args = (escola['nome'], idade, _format(cardapio_str))
return _format(resposta, args), None
def process_notificacao(chat, db=None):
# configura subscricao
db.users.update_one(
{ '_id': chat['chat_id'] },
{ '$set': { 'subscription': chat['valores'] } }
)
return None, menu_subscritos
def process_cancelar_notificacao(chat, db=None):
# apaga subscricao
db.users.update_one(
{ '_id': chat['chat_id'] },
{ '$unset': { 'subscription': True } }
)
return None, menu_principal
# construtores de argumentos
# @params: chat, texto da mensagem
# @return: chat
def process_arguments(chat, texto):
logger.debug('in:process_arguments: {}'.format(texto))
logger.debug(pprint.pformat(chat))
_arg_corrente = chat['arg_corrente']
msg = mensagens.get(_arg_corrente, dict())
chat.update({
'resposta': _format(msg.get('texto')),
'botoes': msg.get('opcoes')
})
try:
_fn = getters[_arg_corrente]
logger.debug('dispatching to: {}'.format(_fn))
chat = _fn(chat, texto)
except KeyError:
if texto:
chat = _update_arg(chat, texto)
logger.debug('out:process_arguments')
logger.debug(pprint.pformat(chat))
return chat
def _update_arg(chat, texto):
# atualiza argumentos
arg_corrente = chat['arg_corrente']
logger.debug('updating arg: {} to {}'.format(arg_corrente, texto))
args = chat['argumentos']
valores = chat['valores']
valores[args.index(arg_corrente)] = texto
# define proximo argumento
chat.update({
# 'valores': valores,
'arg_corrente': args[valores.index(None)],
'resposta': None,
'sub_status': None
})
# quando nao houver mais argumentos a ser preenchidos,
# args[valores.index(None)] lancara ValueError
return chat
def _get_escola(chat, texto):
# procura pela escola
_arg_status = chat.get('sub_status', 0)
if not _arg_status or texto == 'Nenhuma das opções':
chat['sub_status'] = 1
else:
resposta, botoes = None, None
try:
escolas = api.find_escolas(texto)
except:
# erro na API
chat.update({
'resposta': _format(mensagens['erro_api']['texto']),
'botoes': menu_principal,
'status': 'erro'
})
return chat
if len(escolas) == 0:
msg = mensagens['escola_invalida']
chat['resposta'] = _format(msg['texto'])
else:
botoes = [c['nome'] for c in escolas]
if texto in botoes:
escola = escolas[botoes.index(texto)]
chat = _update_arg(chat, escola)
# obtem dados da escola para uso futuro
try:
escola = api.get_escola(escola['_id'])
chat['escola'] = escola
except:
# erro na API
chat.update({
'resposta': _format(mensagens['erro_api']['texto']),
'botoes': menu_principal,
'status': 'erro'
})
return chat
else:
msg = mensagens['escola_confirm']
chat.update({
'resposta': _format(msg['texto']),
'botoes': sorted(botoes) + ['Nenhuma das opções']
})
return chat
def _get_idade(chat, texto):
if texto:
chat = _update_arg(chat, texto)
else:
escola = chat['escola']
botoes = sorted([c for c in escola['idades']])
if len(botoes) == 1:
chat = _update_arg(chat, botoes[0])
else:
chat['botoes'] = botoes
return chat
def _get_data(chat, texto):
if texto:
_hoje = date.today().strftime('%Y%m%d')
data = int(_hoje) + 1*(texto=='Amanhã') - 1*(texto=='Ontem')
chat = _update_arg(chat, data)
return chat
def _get_data_cardapio(chat, texto):
return _get_data(chat, texto)
def _get_data_avaliacao(chat, texto):
return _get_data(chat, texto)
def _get_refeicao_preferida(chat, texto):
if texto:
chat = _update_arg(chat, texto)
else:
escola = chat['escola']
chat['botoes'] = sorted([c for c in escola['refeicoes']])
return chat
def _get_comentario_confirm(chat, texto):
_arg_status = chat.get('sub_status', 0)
if not _arg_status:
chat['sub_status'] = 1
elif texto == 'Sim':
msg = mensagens['comentario']
chat.update({
'resposta': _format(msg['texto']),
'botoes': None
})
else:
chat = _update_arg(chat, texto)
return chat
processors = {
name.replace('process_', ''): fn
for name, fn in locals().items()
if name.startswith('process_')
}
getters = {
name.replace('_get_', ''): fn
for name, fn in locals().items()
if name.startswith('_get_')
}
if __name__ == '__main__':
print(processors)
print(getters)