from flask import Flask, Blueprint, request, abort, g from telebot import TeleBot from telebot.types import Update from ..config import Config bot_bp = Blueprint("bot", __name__) @bot_bp.route("/", methods=["GET", "POST"]) def handle_updates(): if request.method == "GET": abort(404) if request.headers.get("content-type") == "application/json": update = Update.de_json(request.get_json()) g.bot.process_new_updates([update]) return "" else: abort(403) def inject_g(bot: TeleBot, config: Config): def inner(): g.bot = bot g.config = config return inner def create_app(bot: TeleBot, config: Config): app = Flask(__name__) app.register_blueprint(bot_bp, url_prefix=f"{config.webhook.url_path}") app.before_request(inject_g(bot, config)) return app