1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-30 15:04:18 +00:00
royalnet/telebot.py

52 lines
2.1 KiB
Python
Raw Normal View History

2017-10-04 16:51:40 +00:00
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"])
2017-10-04 21:29:02 +00:00
2017-10-04 16:51:40 +00:00
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()
2017-10-05 18:21:47 +00:00
bot.send_message(update.message.chat.id, f"✅ Sincronizzazione Telegram completata!\n"
f"Sincronizza il tuo account Steam qui:\n"
f"http://79.26.47.209:1234/login/{db_user.royal_id}")
2017-10-04 16:51:40 +00:00
2017-10-04 21:29:02 +00:00
2017-10-04 16:51:40 +00:00
updater.dispatcher.add_handler(CommandHandler("sync", message_sync))
updater.start_polling()