Compare commits

..
10 Commits
13 changed files with 76 additions and 45 deletions
+1
View File
@@ -3,5 +3,6 @@ __pycache__/
venv/ venv/
.venv/ .venv/
logs/ logs/
data/
*.env *.env
*.db *.db
+13 -12
View File
@@ -19,20 +19,21 @@ RUN --mount=type=cache,target=/root/.cache/pip \
--mount=type=bind,source=requirements.txt,target=requirements.txt \ --mount=type=bind,source=requirements.txt,target=requirements.txt \
pip install -r requirements.txt pip install -r requirements.txt
# copy i18n files # copy sources
COPY --chown=bot i18n.yaml /i18n/i18n.yaml COPY --chown=bot --chmod=774 docker-entrypoint.sh /app/
COPY --chown=bot migrations /app/migrations
COPY --chown=bot mybot /app/mybot
COPY --chown=bot i18n.yaml /app/
# copy default configs # prepare environment
WORKDIR /app ENV I18N_PATH=/data/i18n.yaml
COPY --chown=bot mybot mybot ENV DB_URL=sqlite:////data/bot.db
# preapre environment RUN mkdir -p /data
ENV SS_TYPE=memory RUN chown bot:bot /data
ENV I18N_PATH=/i18n/i18n.yaml VOLUME /data
# set user
USER bot USER bot
WORKDIR /app
CMD ["python3", "-m", "mybot"] ENTRYPOINT ["./docker-entrypoint.sh"]
VOLUME i18n/
+2 -4
View File
@@ -16,10 +16,8 @@ services:
- i18n:/i18n - i18n:/i18n
env_file: .env env_file: .env
environment: environment:
- SS_TYPE=memory # redis currently is broken - SS_TYPE=redis
- SS_REDIS_HOST=redis - SS_REDIS_HOST=redis
- SS_REDIS_PORT=6379
- SS_REDIS_PASSWORD=bot
redis: redis:
image: redis image: redis
@@ -27,4 +25,4 @@ services:
volumes: volumes:
- redis-config:/etc/redis - redis-config:/etc/redis
- redis-data:/data - redis-data:/data
command: redis-server --save 20 1 --loglevel warning --requirepass bot command: redis-server --save 20 1
+17
View File
@@ -0,0 +1,17 @@
#!/bin/sh
if [ ! -f ${I18N_PATH} ]; then
cp i18n.yaml ${I18N_PATH}
fi
alembic -c migrations/alembic.ini upgrade head
if [ $# -eq 0 ]; then
if [ -z "${USE_WEBHOOK}" ]; then
exec python3 -m mybot
else
exec gunicorn -b 0.0.0.0:8080 "mybot:main()"
fi
else
exec $1
fi
+2 -2
View File
@@ -5,7 +5,7 @@ from sqlalchemy import pool
from alembic import context from alembic import context
from mybot.config import Config as AppConfig from mybot.config import load_config
from mybot.database import Base from mybot.database import Base
import mybot.database.models # do not delete this import mybot.database.models # do not delete this
@@ -19,7 +19,7 @@ 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.from_env() app_config = load_config()
config.set_main_option("sqlalchemy.url", app_config.database.url) config.set_main_option("sqlalchemy.url", app_config.database.url)
+4 -2
View File
@@ -23,11 +23,13 @@ def create_bot(config: Config, i18n: I18N, engine):
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)
bot.delete_webhook()
if config.use_webhook: if config.use_webhook:
bot.delete_webhook()
bot.set_webhook(config.webhook.url, bot.set_webhook(config.webhook.url,
drop_pending_updates=config.webhook.drop_pending_updates, drop_pending_updates=config.webhook.drop_pending_updates,
max_connections=config.webhook.max_connections) max_connections=config.webhook.max_connections,
secret_token=config.webhook.secret_token,
certificate=config.webhook.cert_path)
return bot return bot
+22 -6
View File
@@ -1,5 +1,7 @@
import os import os
import secrets
from dataclasses import dataclass from dataclasses import dataclass
from typing import Optional
@dataclass @dataclass
@@ -23,21 +25,35 @@ class BotConfig:
@dataclass @dataclass
class WebhookConfig: class WebhookConfig:
domain: str domain: Optional[str]
url_path: str url_path: str
max_connections: int max_connections: int
drop_pending_updates: bool drop_pending_updates: bool
# secret token
use_secret_token: bool
secret_token: Optional[str]
# self-signed certificate
cert_path: Optional[str]
def __post_init__(self):
if self.use_secret_token and not self.secret_token:
self.secret_token = secrets.token_hex()
@property @property
def url(self): def url(self):
return f"https://{self.domain}/{self.url_path}" return f"https://{self.domain}{self.url_path}"
@classmethod @classmethod
def from_env(cls): def from_env(cls):
return cls(os.getenv("WEBHOOK_DOMAIN"), return cls(os.getenv("WEBHOOK_DOMAIN"),
os.getenv("WEBHOOK_URL_PATH"), 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))),
bool(int(os.getenv("WEBHOOK_USE_SECRET_TOKEN", True))),
os.getenv("WEBHOOK_SECRET_TOKEN"),
os.getenv("WEBHOOK_CERT_PATH"))
@dataclass @dataclass
@@ -56,10 +72,10 @@ class I18NConfig:
@dataclass @dataclass
class StateStorageConfig: class StateStorageConfig:
type: str type: str
redis_host: str redis_host: Optional[str]
redis_port: int redis_port: int
redis_db: int redis_db: int
redis_pass: str redis_pass: Optional[str]
@classmethod @classmethod
def from_env(cls): def from_env(cls):
-1
View File
@@ -1 +0,0 @@
# keyboards will be defined here
-1
View File
@@ -27,4 +27,3 @@ def create_logger(name: str,
logger.addHandler(file_handler) logger.addHandler(file_handler)
return logger return logger
-3
View File
@@ -12,6 +12,3 @@ def get_state_storage(config: StateStorageConfig):
else: else:
raise RuntimeWarning(f"Unknown state storage type: '{config.type}'") raise RuntimeWarning(f"Unknown state storage type: '{config.type}'")
return state_storage return state_storage
# states will be defined here
+13 -13
View File
@@ -1,35 +1,35 @@
from flask import Flask, Blueprint, request, abort, g from flask import Flask, request, abort, g
from telebot import TeleBot from telebot import TeleBot
from telebot.types import Update from telebot.types import Update
from ..config import Config from ..config import Config
bot_bp = Blueprint("bot", __name__)
@bot_bp.route("/", methods=["GET", "POST"])
def handle_updates(): def handle_updates():
if request.method == "GET": if request.method == "GET":
abort(404) 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": if request.headers.get("content-type") == "application/json":
update = Update.de_json(request.get_json()) update = Update.de_json(request.get_json())
g.bot.process_new_updates([update]) g.bot.process_new_updates([update])
return "" return ""
else: else:
abort(403) abort(404) # safer to 404
def inject_g(bot: TeleBot, config: Config): def inject_g(**kwargs):
def inner(): def inner():
g.bot = bot for k, v in kwargs.items():
g.config = config setattr(g, k, v)
return inner return inner
def create_app(bot: TeleBot, config: Config): def create_app(bot: TeleBot, config: Config):
app = Flask(__name__) app = Flask(__name__)
app.register_blueprint(bot_bp, url_prefix=f"{config.webhook.url_path}") app.add_url_rule(config.webhook.url_path,
app.before_request(inject_g(bot, config)) view_func=handle_updates,
methods=["GET", "POST"])
app.before_request(inject_g(bot=bot, config=config))
return app return app
+2 -1
View File
@@ -3,6 +3,7 @@ pyyaml
sqlalchemy sqlalchemy
alembic alembic
psycopg psycopg
pymysql pymysql[rsa]
flask flask
gunicorn gunicorn
redis