mirror of
https://github.com/RYGhub/royalnet.git
synced 2024-11-23 19:44:20 +00:00
First commit
This commit is contained in:
commit
160c96f310
3 changed files with 105 additions and 0 deletions
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
config.ini
|
||||
.idea/
|
||||
__pycache__
|
54
db.py
Normal file
54
db.py
Normal file
|
@ -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"<Royal {self.username}>"
|
||||
|
||||
|
||||
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"<Telegram {self.id}>"
|
||||
|
||||
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)
|
48
telebot.py
Normal file
48
telebot.py
Normal file
|
@ -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()
|
Loading…
Reference in a new issue