36 lines
1.0 KiB
Python
36 lines
1.0 KiB
Python
from flask import Flask, request, abort, g
|
|
from telebot import TeleBot
|
|
from telebot.types import Update
|
|
|
|
from ..config import Config
|
|
|
|
|
|
def handle_updates():
|
|
if request.method == "GET":
|
|
abort(404) # safer to 404
|
|
if g.config.webhook.use_secret_token:
|
|
if request.headers.get("X-Telegram-Bot-Api-Secret-Token") != g.config.webhook.secret_token:
|
|
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(404) # safer to 404
|
|
|
|
|
|
def inject_g(**kwargs):
|
|
def inner():
|
|
for k, v in kwargs.items():
|
|
setattr(g, k, v)
|
|
return inner
|
|
|
|
|
|
def create_app(bot: TeleBot, config: Config):
|
|
app = Flask(__name__)
|
|
app.add_url_rule(config.webhook.url_path,
|
|
view_func=handle_updates,
|
|
methods=["GET", "POST"])
|
|
app.before_request(inject_g(bot=bot, config=config))
|
|
return app
|