Compare commits

...

3 Commits

Author SHA1 Message Date
4ac6be06cf Fix Dockerfile 2024-07-30 16:11:06 +03:00
a1bd24d3fc Fix webhook 2024-07-30 16:10:38 +03:00
238d144b6d Add pymysql in requirements.txt 2024-07-30 13:58:13 +03:00
8 changed files with 55 additions and 50 deletions

View File

@ -20,12 +20,11 @@ RUN --mount=type=cache,target=/root/.cache/pip \
pip install -r requirements.txt pip install -r requirements.txt
# copy i18n files # copy i18n files
COPY --chown=bot i18n.yaml i18n/i18n.yaml COPY --chown=bot i18n.yaml /i18n/i18n.yaml
# copy default configs # copy default configs
WORKDIR /app WORKDIR /app
COPY --chown=bot mybot mybot COPY --chown=bot mybot mybot
COPY --chown=bot webapp webapp
# preapre environment # preapre environment
ENV SS_TYPE=memory ENV SS_TYPE=memory

View File

@ -19,8 +19,8 @@ if config.config_file_name is not None:
target_metadata = Base.metadata target_metadata = Base.metadata
# set sqlalchemy.url since it can not be set in alembic.ini file # set sqlalchemy.url since it can not be set in alembic.ini file
app_config = AppConfig() app_config = AppConfig.from_env()
config.set_main_option("sqlalchemy.url", app_config.DB_URL) config.set_main_option("sqlalchemy.url", app_config.database.url)
def run_migrations_offline() -> None: def run_migrations_offline() -> None:

View File

@ -8,6 +8,7 @@ from .states import get_state_storage
from .handlers import register_handlers from .handlers import register_handlers
from .middlewares import setup_middlewares from .middlewares import setup_middlewares
from .filters import add_custom_filters from .filters import add_custom_filters
from .webhook import create_app
def create_bot(config: Config, i18n: I18N, engine): def create_bot(config: Config, i18n: I18N, engine):
@ -17,7 +18,8 @@ def create_bot(config: Config, i18n: I18N, engine):
skip_pending=config.bot.skip_pending, skip_pending=config.bot.skip_pending,
num_threads=config.bot.num_threads, num_threads=config.bot.num_threads,
use_class_middlewares=True, use_class_middlewares=True,
state_storage=state_storage) state_storage=state_storage,
threaded=False if config.use_webhook else True)
register_handlers(bot) register_handlers(bot)
setup_middlewares(bot, i18n, engine) setup_middlewares(bot, i18n, engine)
add_custom_filters(bot, config) add_custom_filters(bot, config)
@ -35,6 +37,11 @@ def main():
i18n = I18N(config.i18n) i18n = I18N(config.i18n)
engine = get_engine(config.database) engine = get_engine(config.database)
bot = create_bot(config, i18n, engine) bot = create_bot(config, i18n, engine)
if config.use_webhook:
app = create_app(bot, config)
return app
bot.infinity_polling( bot.infinity_polling(
timeout=config.bot.timeout, timeout=config.bot.timeout,
long_polling_timeout=config.bot.polling_timeout, long_polling_timeout=config.bot.polling_timeout,

View File

@ -23,13 +23,19 @@ class BotConfig:
@dataclass @dataclass
class WebhookConfig: class WebhookConfig:
url: str domain: str
url_path: str
max_connections: int max_connections: int
drop_pending_updates: bool drop_pending_updates: bool
@property
def url(self):
return f"https://{self.domain}/{self.url_path}"
@classmethod @classmethod
def from_env(cls): def from_env(cls):
return cls(os.getenv("WEBHOOK_URL"), return cls(os.getenv("WEBHOOK_DOMAIN"),
os.getenv("WEBHOOK_URL_PATH"),
int(os.getenv("WEBHOOK_MAX_CONNECTIONS", 40)), int(os.getenv("WEBHOOK_MAX_CONNECTIONS", 40)),
bool(int(os.getenv("WEBHOOK_DROP_PENDING", True)))) bool(int(os.getenv("WEBHOOK_DROP_PENDING", True))))

35
mybot/webhook/__init__.py Normal file
View File

@ -0,0 +1,35 @@
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

View File

@ -3,5 +3,6 @@ pyyaml
sqlalchemy sqlalchemy
alembic alembic
psycopg psycopg
pymysql
flask flask
gunicorn gunicorn

View File

@ -1,27 +0,0 @@
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

View File

@ -1,16 +0,0 @@
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)