2020-10-31 14:20:46 +01:00
import logging
2020-10-27 14:37:00 +01:00
from telethon import TelegramClient , events
2020-10-27 14:53:44 +01:00
from telethon . sessions import StringSession
2020-10-31 14:20:46 +01:00
2020-11-16 12:03:39 +01:00
from constants import API_HASH , API_ID , BOT_TOKEN , SESSION
from database import search_database
2020-10-31 14:20:46 +01:00
from notifier import send_mail
logging . basicConfig (
format = " [ %(levelname) 5s / %(asctime)s ] %(name)s : %(message)s " , level = logging . WARNING
)
2020-10-27 01:05:56 +01:00
2020-10-27 14:53:44 +01:00
bot = TelegramClient ( StringSession ( SESSION ) , API_ID , API_HASH ) . start (
bot_token = BOT_TOKEN
)
2020-10-27 01:05:56 +01:00
2020-10-27 14:37:00 +01:00
@bot.on ( events . NewMessage ( pattern = " /start " ) )
async def start ( event ) :
await event . respond (
" Hola, muy buenas. Gracias por ponerte en contacto con UGR Tracing Bot. Para poder continuar con el proceso por favor responda a este mensaje con un identificador único (DNI o correo institucional). Muchas gracias. "
2020-10-27 01:05:56 +01:00
)
2020-10-27 14:37:00 +01:00
raise events . StopPropagation
2020-10-27 01:05:56 +01:00
2020-10-31 14:20:46 +01:00
@bot.on ( events . NewMessage ( pattern = r " ^[0-9] { 8,8}[A-Za-z] " ) )
@bot.on (
events . NewMessage ( pattern = r " (^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+ \ .[a-zA-Z0-9-.]+$) " )
)
async def identify ( event ) :
2020-11-16 12:03:39 +01:00
user = search_database ( id = event . raw_text )
2020-10-31 14:20:46 +01:00
send_mail ( recipient = user [ " correo_institucional " ] )
await event . respond (
" El usuario se ha identificado con éxito. Se ha mandado una clave alfanumérica a su correo institucional. Por favor envíe la clave para continuar con el proceso. "
)
2020-10-27 01:05:56 +01:00
if __name__ == " __main__ " :
2020-11-16 12:03:39 +01:00
bot . run_until_disconnected ( )