forked from CoreDumped-ETSISI/2020-etsisi-telegram-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
380 lines (314 loc) · 14.9 KB
/
bot.py
File metadata and controls
380 lines (314 loc) · 14.9 KB
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import io
import json
import telegram
import datetime
import time
import traceback
import re
import requests
import logger
from logger import get_logger
from data_loader import DataLoader
import sys
from telegram.ext import Updater, CommandHandler, MessageHandler, BaseFilter, Filters
from random import normalvariate
from telegram.error import (TelegramError, Unauthorized, BadRequest,
TimedOut, ChatMigrated, NetworkError)
from etsisi_web_scraper import news_json_scraper, events_json_scraper, avisos_json_scraper
from selenium import webdriver
reload(sys)
sys.setdefaultencoding('utf8')
def error_callback(bot, update, error):
try:
raise error
except Unauthorized:
logger.exception("remove update.message.chat_id from conversation list")
except BadRequest as e:
if update.message.chat_id < 0 and e == "Message can't be deleted": # This pre-check is necessary if we do not want to spam the logs with "BadRequest: Message can't be deleted" as this bot has no power to remove user messages in private chats.
logger.exception("handle malformed requests - read more below!")
except TimedOut:
logger.exception("handle slow connection problems")
except NetworkError:
logger.exception("handle other connection problems")
except ChatMigrated as e:
logger.exception("the chat_id of a group has changed, use " + e.new_chat_id + " instead")
except TelegramError:
logger.exception("There is some error with Telegram")
def is_admin(user_id):
if user_id in settings.admin_ids:
return True
return False
weekdays = ["lunes", "martes", "miércoles", "jueves", "viernes", "sábado", "domingo"]
codedays = {
"L": 0,
"M": 1,
"X": 2,
"J": 3,
"V": 4,
"S": 5,
"D": 6,
"LUNES": 0,
"MARTES": 1,
"MIÉRCOLES": 2,
"MIERCOLES": 2,
"JUEVES": 3,
"VIERNES": 4,
"SÁBADO": 5,
"SABADO": 6,
"DOMINGO": 7,
}
def get_schedule():
with io.open('data/horarios.json', 'r', encoding='utf8') as data_file:
return json.load(data_file)
def get_teachers():
with io.open('data/profesores.json', 'r', encoding='utf8') as data_file:
return json.load(data_file)
def get_chat_ids():
with io.open('data/chat_ids.json', 'r', encoding='utf8') as data_file:
return json.load(data_file)
def load_settings():
global settings
global last_function_calls
global schedule_list
global teacher_list
global chat_ids_list
settings = DataLoader()
schedule_list = get_schedule()
teacher_list = get_teachers()
chat_ids_list = get_chat_ids()
last_function_calls = {}
def delete_message(bot, update):
bot.deleteMessage(update.message.chat_id, update.message.message_id)
def is_call_available(name, chat_id, cooldown):
global last_function_calls
now = datetime.datetime.now()
cooldown_time = datetime.datetime.now() - datetime.timedelta(minutes=cooldown)
if name in last_function_calls.keys():
if chat_id in last_function_calls[name].keys():
if last_function_calls[name][chat_id] > cooldown_time:
last_function_calls[name][chat_id] = now
return False
else:
last_function_calls[name][chat_id] = now
return True
else:
last_function_calls[name][chat_id] = now
return True
else:
last_function_calls[name] = {chat_id: now}
return True
def reset_call(name, chat_id):
global last_function_calls
reset_date = datetime.datetime.now() - datetime.timedelta(days=1)
last_function_calls[name][chat_id] = reset_date
def help_command(bot, update):
if is_call_available("help_command", update.message.chat_id, 180):
log_message(update)
bot.sendMessage(update.message.chat_id, settings.help_string, parse_mode=telegram.ParseMode.HTML)
def log_message(update):
try:
username = update.message.from_user.username
except:
username = "desconocido"
try:
text = update.message.text
except:
text = "algo"
logger.info("He recibido: \"" + text + "\" de " + username + " [ID: " + str(
update.message.chat_id) + "]")
def human_texting(string):
wait_time = len(string) * normalvariate(0.1, 0.05)
if wait_time > 8:
wait_time = 8
time.sleep(wait_time)
def reload_data(bot, update):
if is_admin(update.message.from_user.id):
logger.info("Reloading settings")
load_settings()
delete_message(bot, update)
def news_command(bot, update):
if is_call_available("news_command", update.message.chat_id, 180):
log_message(update)
logger.info("Getting news")
text = "Estas son las últimas noticias que aparecen en la web: \n"
news_list = news_json_scraper()
for idx, new in enumerate(news_list):
text = text + str(idx) + ") " + news_list[new]["a-link"] + "\n"
bot.sendMessage(chat_id=update.message.chat.id, text=text, parse_mode=telegram.ParseMode.HTML)
def events_command(bot, update):
if is_call_available("events", update.message.chat_id, 180):
log_message(update)
logger.info("Getting news")
text = "Estas son los últimos eventos que aparecen en la web: \n"
events_list = events_json_scraper()
for idx, new in enumerate(events_list):
text = text + str(idx) + ") " + events_list[new]["a-link"] + "\n"
bot.sendMessage(chat_id=update.message.chat.id, text=text, parse_mode=telegram.ParseMode.HTML)
def notifications_command(bot, update):
if is_call_available("help", update.message.chat_id, 180):
log_message(update)
logger.info("Getting news")
text = "Estas son los últimos avisos que aparecen en la web: \n"
notifications_list = avisos_json_scraper()
for idx, new in enumerate(notifications_list):
text = text + str(idx) + ") " + notifications_list[new]["a-link"] + "\n"
bot.sendMessage(chat_id=update.message.chat.id, text=text, parse_mode=telegram.ParseMode.HTML)
else:
delete_message(bot, update)
def calendar_command(bot, update):
if is_call_available("calendario", update.message.chat_id, 180):
log_message(update)
bot.sendMessage(update.message.chat_id, settings.calendar_string, parse_mode=telegram.ParseMode.HTML)
def menu_command(bot, update):
if is_call_available("cafe", update.message.chat_id, 180):
text = ""
log_message(update)
try:
data = requests.get("https://cafe.kolhos.chichasov.es/today").json()
if "message" in data: # Error, maybe it isn't open today?
text = "Ups, algo ha salido mal:\n" + data["message"]
else:
text = (
"Hoy de primer plato hay:\n" +
'\n'.join(['- ' + p for p in data["primer"]]) +
"\n\nSegundo plato:\n" +
'\n'.join(['- ' + p for p in data["segundo"]])
)
except:
text = (
"El servidor no está disponible ahora, vuelve a intentarlo más tarde.\n\nEl menú semanal se puede ver manualmente aquí:\n" +
"https://www.etsisi.upm.es/escuela/servicios/cafeteria"
)
bot.sendMessage(update.message.chat_id, text=text)
def busy_rooms_command(bot, update):
if is_call_available("busy_rooms", update.message.chat_id, 180):
log_message(update)
logger.info("Getting busy rooms")
options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--window-size=1033,558')
driver = webdriver.Chrome(executable_path='resources/chromedriver_linux', chrome_options=options)
driver.get(settings.url_busy_rooms)
driver.get_screenshot_as_file('resources/ocupacion_salas_biblioteca.png')
driver.close()
text = 'La ocupación de las salas de trabajo en grupo de la Biblioteca Universitaria Campus Sur es la ' \
'siguiente: '
bot.sendMessage(chat_id=update.message.chat.id, text=text, parse_mode=telegram.ParseMode.HTML)
bot.send_photo(chat_id=update.message.chat_id, photo=open('resources/ocupacion_salas_biblioteca.png', 'rb'))
def schedule_command(bot, update, args): # Add arguments for checking other's group schedule
global chat_ids_list
global schedule_list
def schedule_parser(schedule):
parsed_schedule = [""]
schedule_keys = sorted(schedule, key=lambda s: int(s.split(":")[0]))
for hour in schedule_keys:
parsed_schedule.append("A las %sh -> %s" % (hour, schedule[hour]))
return "\n".join(parsed_schedule)
if is_call_available(update.message.text, update.message.chat_id, 60):
log_message(update)
try:
group = ""
day_index = datetime.datetime.today().weekday()
if args:
args = [str(x.upper()) for x in args]
if re.match(r"G[TM][1-4]{2}", args[0]): # First argument is a group identificator
group = args[0]
if len(args) > 1: # If a second paramenter exists
if re.match(r"[LMXJV]", args[1]) or args[1].decode(
'utf-8') in codedays.keys(): # Two inputs: group and daycode
day_index = codedays[str(args[1].decode('utf-8'))]
else:
if not re.match(r"[LMXJV]", args[0]) or not args[0].decode(
'utf-8') in codedays.keys(): # Two inputs: group and daycode
if update.message.chat_id < 0:
bot.send_message(chat_id=update.message.chat_id,
text="Día de la semana inválido. Debes introducir martes/M, miércoles/X, jueves/J, viernes/V")
return
else:
bot.send_message(chat_id=update.message.chat_id,
text="Debes especificar un grupo. <i>Por ejemplo: /horario gt11</i>",
parse_mode=telegram.ParseMode.HTML)
return
day_index = codedays[str(args[0].decode('utf-8'))]
if update.message.chat_id < 0: # ID's below 0 are groups.
group = update.message.chat.title.replace(" ETSISI", "") # get group from chat title
if group == u"GX3" or group == u"GX4":
bot.send_message(chat_id=update.message.chat_id,
text="Esta función no está definida para este grupo.")
elif day_index in range(1, 5):
text = schedule_parser(schedule_list[group][str(day_index)])
text = "Horario del " + weekdays[day_index] + " para " + group + ":" + text
bot.send_message(chat_id=update.message.chat_id, text=text)
elif day_index == datetime.datetime.today().weekday(): # Check if user input is from 'today'
bot.send_message(chat_id=update.message.chat_id,
text="Hoy " + weekdays[day_index] + " no hay clases.")
else:
bot.send_message(chat_id=update.message.chat_id,
text="El " + weekdays[day_index] + " no hay clases.")
except:
tb = traceback.format_exc()
bot.send_message(chat_id=update.message.chat_id,
text="No he podido procesar tu solicitud de horario.\n\nERROR:\n" + str(
tb) + "\n\nPor favor, reenvía este error a @nestoroa.")
else:
delete_message(bot, update)
def teacher_command(bot, update, args): # Add arguments for checking other's group schedule
global chat_ids_list
global teacher_list
def teacher_parser(teacher):
parsed_teachers = [""]
# schedule_keys = sorted(schedule, key=lambda s: int(s.split(":")[0]))
for k, v in zip(teacher.keys(), teacher.values()):
parsed_teachers.append("<b>%s</b> -> %s" % (k, v))
return "\n".join(parsed_teachers)
if is_call_available("teacher", update.message.chat_id, 180):
log_message(update)
try:
group = ""
if args:
group = args[0].upper()
if update.message.chat_id < 0: # ID's below 0 are groups.
group = update.message.chat.title.replace(" ETSISI", "") # get group from chat title
if group is not "":
text = teacher_parser(teacher_list[group])
text = "Lista de profesores para el primer semestre de " + group + ":\n" + text
else:
text = "No me has dicho a qué grupo te refieres."
bot.send_message(chat_id=update.message.chat_id, text=text, parse_mode=telegram.ParseMode.HTML)
except:
tb = traceback.format_exc()
bot.send_message(chat_id=update.message.chat_id,
text="No he podido procesar tu solicitud de profesores.\n\nERROR:\n" + str(
tb) + "\n\nPor favor, reenvía este error a @nestoroa.")
else:
delete_message(bot, update)
if __name__ == "__main__":
print("ETSISI Bot: Starting...")
logger = get_logger("bot_starter", True)
load_settings()
try:
logger.info("Conectando con la API de Telegram.")
updater = Updater(settings.telegram_token)
dispatcher = updater.dispatcher
dispatcher.add_handler(CommandHandler('help', help_command))
dispatcher.add_handler(CommandHandler('reload', reload_data))
dispatcher.add_handler(CommandHandler('noticias', news_command))
dispatcher.add_handler(CommandHandler('eventos', events_command))
dispatcher.add_handler(CommandHandler('avisos', notifications_command))
dispatcher.add_handler(CommandHandler('calendario', calendar_command))
dispatcher.add_handler(CommandHandler('menu', menu_command))
dispatcher.add_handler(CommandHandler('horario', schedule_command, pass_args=True))
dispatcher.add_handler(CommandHandler('profesores', teacher_command, pass_args=True))
dispatcher.add_handler(CommandHandler('salasbiblioteca', busy_rooms_command))
dispatcher.add_handler(MessageHandler(Filters.status_update.new_chat_members, delete_message))
dispatcher.add_handler(MessageHandler(Filters.status_update.new_chat_title, delete_message))
dispatcher.add_handler(MessageHandler(Filters.status_update.left_chat_member, delete_message))
dispatcher.add_error_handler(error_callback)
except Exception as ex:
logger.exception("Error al conectar con la API de Telegram.")
quit()
updater.start_polling()
logger.info("ETSISI Bot: Estoy escuchando.")
updater.idle()