From f3338c2fe9f3dea2376357828f3da8891a67a689 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Wed, 4 Oct 2017 23:29:02 +0200 Subject: [PATCH] Add Steam linking --- db.py | 18 ++++++++++++++++++ steam.py | 39 +++++++++++++++++++++++++++++++++++++++ telebot.py | 2 ++ 3 files changed, 59 insertions(+) create mode 100644 steam.py diff --git a/db.py b/db.py index 5a26b53c..ce940a7d 100644 --- a/db.py +++ b/db.py @@ -49,6 +49,24 @@ class Telegram(Base): return self.first_name +class Steam(Base): + __tablename__ = "steam" + + royal_id = Column(Integer, ForeignKey("royals.id")) + royal = relationship("Royal") + + steam_id = Column(String, primary_key=True) + + def __repr__(self): + return f"" + + def __str__(self): + if self.steam_name is not None: + return self.steam_name + else: + return self.steam_id + + # 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/steam.py b/steam.py new file mode 100644 index 00000000..2bc41273 --- /dev/null +++ b/steam.py @@ -0,0 +1,39 @@ +from flask import Flask +from flask import session as flask_session +from flask_openid import OpenID +from db import session, Royal, Steam +import re + +# Init the config reader +import configparser +config = configparser.ConfigParser() +config.read("config.ini") + +# Init Flask and Flask_OpenID +app = Flask(__name__) +app.secret_key = config["Steam"]["secret_key"] +oid = OpenID(app) + +@app.route("/login/") +@oid.loginhandler +def page_steam_login(royal_id): + flask_session["royal_id"] = royal_id + return oid.try_login("http://steamcommunity.com/openid") + + +@oid.after_login +def page_after_login(response): + steam_id = re.search("https?://steamcommunity\.com/openid/id/(.+)", response.identity_url).group(1) + db_steam = session.query(Steam).filter(Steam.steam_id == steam_id).first() + if db_steam is None: + db_steam = Steam(royal_id=flask_session["royal_id"], + steam_id=steam_id) + session.add(db_steam) + session.commit() + return "Account Steam collegato con successo!" + else: + return "Il tuo account Steam è già collegato." + + +if __name__ == "__main__": + app.run(host="0.0.0.0", port=config["Steam"]["flask_port"]) \ No newline at end of file diff --git a/telebot.py b/telebot.py index 7f7766b1..b4c81f2e 100644 --- a/telebot.py +++ b/telebot.py @@ -14,6 +14,7 @@ logging.basicConfig(level=logging.DEBUG, format="[%(levelname)s] %(asctime)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() @@ -44,5 +45,6 @@ def message_sync(bot: Bot, update: Update): 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