This repository has been archived by the owner on Sep 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conversation.py
268 lines (225 loc) · 11 KB
/
conversation.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
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
import time
from sqlalchemy.orm import sessionmaker
from telegram import ParseMode, ChatAction, InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import ConversationHandler, CommandHandler, MessageHandler, Filters, run_async
import admins
import crawlers
import dao
import db
import messages
import users
Session = sessionmaker(bind=db.gen_engine(db.get_database_url()))
def login():
@users.restricted
@users.registered
@run_async
def iniciar(bot, update):
telegram_id = update['message']['chat']['id']
bot.sendChatAction(chat_id=telegram_id, action=ChatAction.TYPING)
bot.send_message(chat_id=telegram_id, text=messages.user_login())
return 'user'
def get_user(bot, update, user_data):
text = update['message']['text']
user_data['user'] = text
return test_user(bot, update, user_data)
def test_user(bot, update, user_data):
telegram_id = update['message']['chat']['id']
bot.sendChatAction(chat_id=telegram_id, action=ChatAction.TYPING)
soup = crawlers.get_login(user_data['user'], "pass")
for index in soup.find_all('script'):
if str(index.get_text().lstrip()).split("'")[1] == "Erro":
if "Usuário" in str(index.get_text().lstrip()).split("'")[3]:
bot.send_message(chat_id=telegram_id, text=messages.user_invalido_login())
users.usage(telegram_id, "Login_user", time.strftime("%d/%m/%Y %H:%M:%S", time.localtime()))
return 'user'
elif "Senha" in str(index.get_text().lstrip()).split("'")[3]:
bot.send_message(chat_id=telegram_id, text=messages.pass_login())
return 'senha'
def get_senha(bot, update, user_data):
text = update['message']['text']
user_data['senha'] = text
return test_senha(bot, update, user_data)
def test_senha(bot, update, user_data):
telegram_id = update['message']['chat']['id']
bot.sendChatAction(chat_id=telegram_id, action=ChatAction.TYPING)
first_name = update['message']['chat']['first_name']
logado, chave, curso = crawlers.get_login_completo(user_data['user'], user_data['senha'])
if not logado:
bot.send_message(chat_id=telegram_id, text=messages.pass_invalido_login())
users.usage(telegram_id, "Login_pass", time.strftime("%d/%m/%Y %H:%M:%S", time.localtime()))
return 'senha'
else:
logar(bot, update, [telegram_id, user_data['user'], user_data['senha'], chave, curso, first_name])
user_data.clear()
return -1
def logar(bot, update, args):
session = Session()
user = session.query(db.User).filter_by(telegram_id=args[0]).first()
user.sapu_username = args[1]
user.sapu_password = args[2]
user.chave = args[3]
user.curso = args[4]
session.commit()
user = session.query(db.User).filter_by(telegram_id=args[0]).first()
notas_resumo, notas_detalhe = crawlers.get_notas(user, bot)
frequencia = crawlers.get_frequencia(user, bot)
dao.set_notas(user, notas_resumo, notas_detalhe, bot)
dao.set_frequencia(user, frequencia)
session.close()
bot.send_message(chat_id=args[0], text=messages.valid_login(args[5]), parse_mode=ParseMode.HTML)
users.menu(bot, update, [])
users.usage(args[0], "Login", time.strftime("%d/%m/%Y %H:%M:%S", time.localtime()))
return
def cancelar(bot, update, user_data):
telegram_id = update['message']['chat']['id']
bot.sendChatAction(chat_id=telegram_id, action=ChatAction.TYPING)
bot.send_message(chat_id=telegram_id, text=messages.cancelar_login())
user_data.clear()
return -1
return ConversationHandler(
entry_points=[CommandHandler('login', iniciar)],
states={
'user': [MessageHandler(Filters.text, get_user, pass_user_data=True)],
'senha': [MessageHandler(Filters.text, get_senha, pass_user_data=True)],
},
fallbacks=[CommandHandler('cancelar', cancelar, pass_user_data=True)]
)
def sugerir():
@users.restricted
@users.registered
@run_async
def iniciar(bot, update):
telegram_id = update['message']['chat']['id']
bot.sendChatAction(chat_id=telegram_id, action=ChatAction.TYPING)
bot.send_message(chat_id=telegram_id, text=messages.conversation_sugestao())
return 'sugestao'
def get_sugestao(bot, update, user_data):
text = update['message']['text']
user_data['sugestao'] = text
return test_sugestao(bot, update, user_data)
def test_sugestao(bot, update, user_data):
telegram_id = update['message']['chat']['id']
bot.sendChatAction(chat_id=telegram_id, action=ChatAction.TYPING)
if len(user_data['sugestao']) < 10:
bot.send_message(chat_id=telegram_id, text=messages.conversation_sugestao_invalida())
return 'sugestao'
else:
session = Session()
sugestao = db.Sugestoes(telegram_id, user_data['sugestao'], time.strftime("%d/%m/%Y %H:%M:%S", time.localtime()))
session.add(sugestao)
bot.send_message(chat_id=telegram_id, text=messages.sugestao(update['message']['chat']['first_name']), parse_mode=ParseMode.HTML)
session.commit()
session.close()
users.usage(telegram_id, "Sugestão", time.strftime("%d/%m/%Y %H:%M:%S", time.localtime()))
return -1
def cancelar(bot, update, user_data):
telegram_id = update['message']['chat']['id']
bot.sendChatAction(chat_id=telegram_id, action=ChatAction.TYPING)
bot.send_message(chat_id=telegram_id, text=messages.conversation_cancelar())
user_data.clear()
return -1
return ConversationHandler(
entry_points=[CommandHandler('sugerir', iniciar)],
states={
'sugestao': [MessageHandler(Filters.text, get_sugestao, pass_user_data=True)],
},
fallbacks=[CommandHandler('cancelar', cancelar, pass_user_data=True)]
)
def poll():
def formata(user_data):
titulo = user_data['titulo']
questao = user_data['questao']
msg = ""
tam = len(user_data) - 1
for i in range(1, tam):
msg += str(i) + " - " + user_data['{}'.format(i)] + "\n"
return """
<b>{}</b>
{}
{}""".format(titulo, questao, msg)
def top(user_data):
titulo = user_data['titulo']
questao = user_data['questao']
return """
<b>{}</b>
{}
Seu voto é totalmente anônimo e sua opinião é muito importante para que possamos melhorar cada vez mais.""".format(titulo, questao)
@admins.restricted
def criar_votacao(bot, update):
telegram_id = update['message']['chat']['id']
bot.sendChatAction(chat_id=telegram_id, action=ChatAction.TYPING)
bot.send_message(chat_id=telegram_id, text=messages.poll_titulo(update['message']['chat']['first_name']))
return 'titulo'
def get_titulo(bot, update, user_data):
user_data['titulo'] = update['message']['text']
telegram_id = update['message']['chat']['id']
bot.sendChatAction(chat_id=telegram_id, action=ChatAction.TYPING)
bot.send_message(chat_id=telegram_id, text=messages.poll_pergunta())
return 'questao'
def get_questao(bot, update, user_data):
user_data['questao'] = update['message']['text']
telegram_id = update['message']['chat']['id']
bot.sendChatAction(chat_id=telegram_id, action=ChatAction.TYPING)
bot.send_message(chat_id=telegram_id, text=messages.poll_primeira_pergunta())
return 'resposta'
def get_resposta(bot, update, user_data):
if str(update['message']['text']).lower() != "finalizar":
index = len(user_data) - 1
user_data['{}'.format(index)] = update['message']['text']
telegram_id = update['message']['chat']['id']
bot.sendChatAction(chat_id=telegram_id, action=ChatAction.TYPING)
if index < 2:
bot.send_message(chat_id=telegram_id, text=messages.poll_segunda_pergunta())
else:
bot.send_message(chat_id=telegram_id, text=messages.poll_outra_pergunta())
return 'resposta'
else:
telegram_id = update['message']['chat']['id']
bot.sendChatAction(chat_id=telegram_id, action=ChatAction.TYPING)
bot.send_message(chat_id=telegram_id,
text=messages.poll_finalizar(formata(user_data)), parse_mode=ParseMode.HTML)
return 'enviar'
def enviar(bot, update, user_data):
if str(update['message']['text']).lower() in ["sim", "s", "y", "enviar"]:
session = Session()
poll_db = db.Poll(user_data['titulo'], user_data['questao'])
session.add(poll_db)
session.commit()
keyboard = []
tam = len(user_data) - 1
for i in range(1, tam):
options_poll_db = db.OptionsPoll(poll_db.id, user_data['{}'.format(i)])
session.add(options_poll_db)
session.commit()
keyboard.append([InlineKeyboardButton('{}'.format(user_data['{}'.format(i)]), callback_data='poll | {} | {}'.format(poll_db.id, options_poll_db.id))])
options_poll_db = db.OptionsPoll(poll_db.id, 'Não sou capaz de opinar')
session.add(options_poll_db)
session.commit()
keyboard.append([InlineKeyboardButton('Não sou capaz de opinar', callback_data='poll | {} | {}'.format(poll_db.id, options_poll_db.id))])
reply_markup = InlineKeyboardMarkup(keyboard)
for user in session.query(db.User):
bot.send_message(chat_id=user.telegram_id, text=top(user_data), reply_markup=reply_markup, parse_mode=ParseMode.HTML)
user_data.clear()
session.close()
return -1
else:
telegram_id = update['message']['chat']['id']
bot.sendChatAction(chat_id=telegram_id, action=ChatAction.TYPING)
bot.send_message(chat_id=telegram_id, text=messages.poll_finalizar_dnv())
return 'enviar'
def cancelar(bot, update, user_data):
telegram_id = update['message']['chat']['id']
bot.sendChatAction(chat_id=telegram_id, action=ChatAction.TYPING)
bot.send_message(chat_id=telegram_id, text=messages.poll_cancelar(update['message']['chat']['first_name']))
user_data.clear()
return -1
return ConversationHandler(
entry_points=[CommandHandler('poll', criar_votacao)],
states={
'titulo': [MessageHandler(Filters.text, get_titulo, pass_user_data=True)],
'questao': [MessageHandler(Filters.text, get_questao, pass_user_data=True)],
'resposta': [MessageHandler(Filters.text, get_resposta, pass_user_data=True)],
'enviar': [MessageHandler(Filters.text, enviar, pass_user_data=True)],
},
fallbacks=[CommandHandler('cancelar', cancelar, pass_user_data=True)]
)