1
0
mirror of https://gitlab.com/akasroua/covot synced 2025-12-24 16:11:55 +01:00

Migrate database to sqlite

This commit is contained in:
2020-11-17 14:16:44 +01:00
parent ea1733abdc
commit 89ac99858a
9 changed files with 66 additions and 26 deletions

5
app/__init__.py Normal file
View File

@@ -0,0 +1,5 @@
import logging
logging.basicConfig(
format="[%(levelname) 5s/%(asctime)s] %(name)s: %(message)s", level=logging.WARNING
)

35
app/bot.py Normal file
View File

@@ -0,0 +1,35 @@
from telethon import TelegramClient, events
from telethon.sessions import StringSession
from app.notifier import send_mail
from constants import API_HASH, API_ID, BOT_TOKEN, EXAMPLE, SESSION
from database.crud import create_database, search_database
bot = TelegramClient(StringSession(SESSION), API_ID, API_HASH).start(
bot_token=BOT_TOKEN
)
@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."
)
raise events.StopPropagation
@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):
user = search_database(id=event.raw_text)
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."
)
if __name__ == "__main__":
create_database(data=EXAMPLE)
bot.run_until_disconnected()

28
app/notifier.py Normal file
View File

@@ -0,0 +1,28 @@
from email.message import EmailMessage
from secrets import token_hex
from smtplib import SMTP_SSL
from constants import DOMAIN, PASSWORD, USERNAME
def initialize_smtp(domain, username, password):
server = SMTP_SSL(domain)
server.login(username, password)
return server
def format_message(sender, recipient):
body = "La clave es {}. Envíele esta clave al bot.".format(token_hex(4))
message = EmailMessage()
message["From"] = sender
message["To"] = recipient
message["Subject"] = "Clave para verificar su identidad"
message.set_content(body)
return message
def send_mail(recipient):
server = initialize_smtp(domain=DOMAIN, username=USERNAME, password=PASSWORD)
message = format_message(sender=USERNAME, recipient=recipient)
server.send_message(msg=message)
server.close()