Try to add webhook using flask

This commit is contained in:
2024-07-30 03:56:00 +03:00
parent e230d6f3dc
commit db3e02356d
4 changed files with 48 additions and 2 deletions
+27
View File
@@ -0,0 +1,27 @@
from flask import Flask, g
from telebot import TeleBot
from mybot import create_bot
from mybot.config import load_config
from mybot.database import get_engine
from mybot.i18n import I18N
from .bot import bp as bot_bp
def inject_bot(bot: TeleBot):
def inner():
g.bot = bot
return inner
def create_app():
config = load_config()
i18n = I18N(config.i18n)
engine = get_engine(config.database)
bot = create_bot(config, i18n, engine)
app = Flask(__name__)
app.register_blueprint(bot_bp, url_prefix=f"/{config.bot.token}")
app.before_request(inject_bot(bot))
return app
+16
View File
@@ -0,0 +1,16 @@
from flask import Blueprint, request, abort, g, Response
from telebot.types import Update
bp = Blueprint("bot", __name__)
@bp.route("/", methods=["POST"])
def handle_updates():
if request.headers.get("content-type") == "application/json":
json_string = request.get_data().decode("utf-8")
update = Update.de_json(json_string)
g.bot.process_new_updates([update])
return Response("", 200)
else:
abort(403)