From 160c96f310e950765ad1192c70a8f59fca2c6bf4 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Wed, 4 Oct 2017 18:51:40 +0200 Subject: [PATCH] First commit --- .gitignore | 3 +++ db.py | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ telebot.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 105 insertions(+) create mode 100644 .gitignore create mode 100644 db.py create mode 100644 telebot.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..e5e290c3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +config.ini +.idea/ +__pycache__ \ No newline at end of file diff --git a/db.py b/db.py new file mode 100644 index 00000000..5a26b53c --- /dev/null +++ b/db.py @@ -0,0 +1,54 @@ +from sqlalchemy.ext.declarative import declarative_base +from sqlalchemy.orm import sessionmaker, relationship +from sqlalchemy import Column, BigInteger, Integer, String, Numeric, DateTime, ForeignKey, Float, create_engine + +# Init the config reader +import configparser +config = configparser.ConfigParser() +config.read("config.ini") + +# Init the sqlalchemy engine +engine = create_engine(config["Database"]["database_uri"]) +Base = declarative_base(bind=engine) +Session = sessionmaker(bind=engine) + +# Create a new default session +session = Session() + + +class Royal(Base): + __tablename__ = "royals" + + id = Column(Integer, primary_key=True) + username = Column(String, unique=True, nullable=False) + + def __repr__(self): + return f"" + + +class Telegram(Base): + __tablename__ = "telegram" + + royal_id = Column(Integer, ForeignKey("royals.id")) + royal = relationship("Royal") + + telegram_id = Column(BigInteger, primary_key=True) + first_name = Column(String, nullable=False) + last_name = Column(String) + username = Column(String) + + def __repr__(self): + return f"" + + def __str__(self): + if self.username is not None: + return f"@{self.username}" + elif self.last_name is not None: + return f"{self.first_name} {self.last_name}" + else: + return self.first_name + + +# If run as script, create all the tables in the db +if __name__ == "__main__": + Base.metadata.create_all(bind=engine) \ No newline at end of file diff --git a/telebot.py b/telebot.py new file mode 100644 index 00000000..7f7766b1 --- /dev/null +++ b/telebot.py @@ -0,0 +1,48 @@ +from telegram import Bot, Update +from telegram.ext import Updater, CommandHandler +import logging +from db import session, Royal, Telegram + +# Init the config reader +import configparser +config = configparser.ConfigParser() +config.read("config.ini") + +# Init the logger +logging.basicConfig(level=logging.DEBUG, format="[%(levelname)s] %(asctime)s | %(message)s") + +# Init the Telegram Bot +updater = Updater(token=config["Telegram"]["bot_token"]) + +def message_sync(bot: Bot, update: Update): + tg_user = update.message.from_user + db_user = session.query(Telegram).filter(Telegram.telegram_id == tg_user.id).first() + if db_user is None: + # Find the royals table record matching the command argument + try: + tg_royal = update.message.text.split(" ", 1)[1] + except IndexError: + bot.send_message(update.message.chat.id, "⚠️ Non hai specificato nessun username!") + return + db_royal = session.query(Royal).filter(Royal.username == tg_royal).first() + if db_royal is None: + bot.send_message(update.message.chat.id, "⚠️ L'username che hai specificato non è valido!") + return + # Create the new user and link it to the royals table + db_user = Telegram(royal_id=db_royal.id, + telegram_id=tg_user.id, + first_name=tg_user.first_name, + last_name=tg_user.last_name, + username=tg_user.username) + session.add(db_user) + session.commit() + else: + # Update user data + db_user.first_name = tg_user.first_name + db_user.last_name = tg_user.last_name + db_user.username = tg_user.username + session.commit() + bot.send_message(update.message.chat.id, "✅ Sincronizzazione completata!") + +updater.dispatcher.add_handler(CommandHandler("sync", message_sync)) +updater.start_polling() \ No newline at end of file