Template
Compare commits
19
Commits
d2c6b972e5
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3e97c318c7 | ||
|
|
08d50a3391 | ||
|
|
de24812cdc | ||
|
|
3e64441936 | ||
|
|
b2eac62bd2 | ||
|
|
6e8238243a | ||
|
|
3b12868201 | ||
|
|
a8f39d4630 | ||
|
|
b3a1ea1309 | ||
|
|
c309d4975e | ||
|
|
4ac6be06cf | ||
|
|
a1bd24d3fc | ||
|
|
238d144b6d | ||
|
|
db3e02356d | ||
|
|
e230d6f3dc | ||
|
|
f8c09d963e | ||
|
|
cd5845c180 | ||
|
|
f62e05fd06 | ||
|
|
3ec83e408c |
@@ -3,5 +3,6 @@ __pycache__/
|
|||||||
venv/
|
venv/
|
||||||
.venv/
|
.venv/
|
||||||
logs/
|
logs/
|
||||||
|
data/
|
||||||
*.env
|
*.env
|
||||||
*.db
|
*.db
|
||||||
|
|||||||
+13
-11
@@ -19,19 +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/
|
||||||
VOLUME i18n/
|
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"]
|
||||||
|
|||||||
+2
-4
@@ -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
|
||||||
|
|||||||
@@ -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
|
||||||
+3
-3
@@ -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,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 = load_config()
|
||||||
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:
|
||||||
|
|||||||
+16
-2
@@ -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,10 +18,18 @@ 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)
|
setup_middlewares(bot, i18n, engine)
|
||||||
add_custom_filters(bot, config)
|
add_custom_filters(bot, config)
|
||||||
|
bot.delete_webhook()
|
||||||
|
if config.use_webhook:
|
||||||
|
bot.set_webhook(config.webhook.url,
|
||||||
|
drop_pending_updates=config.webhook.drop_pending_updates,
|
||||||
|
max_connections=config.webhook.max_connections,
|
||||||
|
secret_token=config.webhook.secret_token,
|
||||||
|
certificate=config.webhook.cert_path)
|
||||||
return bot
|
return bot
|
||||||
|
|
||||||
|
|
||||||
@@ -30,6 +39,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,
|
||||||
|
|||||||
+46
-3
@@ -1,5 +1,7 @@
|
|||||||
import os
|
import os
|
||||||
|
import secrets
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
@@ -21,6 +23,39 @@ class BotConfig:
|
|||||||
os.getenv("BOT_PARSE_MODE", "html"))
|
os.getenv("BOT_PARSE_MODE", "html"))
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class WebhookConfig:
|
||||||
|
domain: Optional[str]
|
||||||
|
url_path: str
|
||||||
|
max_connections: int
|
||||||
|
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
|
||||||
|
def url(self):
|
||||||
|
return f"https://{self.domain}{self.url_path}"
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_env(cls):
|
||||||
|
return cls(os.getenv("WEBHOOK_DOMAIN"),
|
||||||
|
os.getenv("WEBHOOK_URL_PATH", "/"),
|
||||||
|
int(os.getenv("WEBHOOK_MAX_CONNECTIONS", 40)),
|
||||||
|
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
|
||||||
class I18NConfig:
|
class I18NConfig:
|
||||||
path: str
|
path: str
|
||||||
@@ -37,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):
|
||||||
@@ -54,10 +89,14 @@ class StateStorageConfig:
|
|||||||
@dataclass
|
@dataclass
|
||||||
class DatabaseConfig:
|
class DatabaseConfig:
|
||||||
url: str
|
url: str
|
||||||
|
pool_recycle: int
|
||||||
|
pool_pre_ping: bool
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_env(cls):
|
def from_env(cls):
|
||||||
return cls(os.getenv("DATABASE_URL", "sqlite:///bot.db"))
|
return cls(os.getenv("DB_URL", "sqlite:///bot.db"),
|
||||||
|
int(os.getenv("DB_POOL_RECYCLE", 3600)),
|
||||||
|
bool(int(os.getenv("DB_POOL_PRE_PING", True))))
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
@@ -66,7 +105,9 @@ class Config:
|
|||||||
i18n: I18NConfig
|
i18n: I18NConfig
|
||||||
states: StateStorageConfig
|
states: StateStorageConfig
|
||||||
database: DatabaseConfig
|
database: DatabaseConfig
|
||||||
|
webhook: WebhookConfig
|
||||||
|
|
||||||
|
use_webhook: bool
|
||||||
log_level: str
|
log_level: str
|
||||||
owner_id: int
|
owner_id: int
|
||||||
|
|
||||||
@@ -77,6 +118,8 @@ class Config:
|
|||||||
i18n=I18NConfig.from_env(),
|
i18n=I18NConfig.from_env(),
|
||||||
states=StateStorageConfig.from_env(),
|
states=StateStorageConfig.from_env(),
|
||||||
database=DatabaseConfig.from_env(),
|
database=DatabaseConfig.from_env(),
|
||||||
|
webhook=WebhookConfig.from_env(),
|
||||||
|
use_webhook=bool(int(os.getenv("USE_WEBHOOK", False))),
|
||||||
log_level=os.getenv("LOG_LEVEL", "INFO"),
|
log_level=os.getenv("LOG_LEVEL", "INFO"),
|
||||||
owner_id=int(os.getenv("OWNER_ID", 1)),
|
owner_id=int(os.getenv("OWNER_ID", 1)),
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -6,8 +6,8 @@ from ..config import DatabaseConfig
|
|||||||
|
|
||||||
def get_engine(config: DatabaseConfig):
|
def get_engine(config: DatabaseConfig):
|
||||||
engine = create_engine(config.url,
|
engine = create_engine(config.url,
|
||||||
pool_recycle=3600,
|
pool_recycle=config.pool_recycle,
|
||||||
pool_pre_ping=True)
|
pool_pre_ping=config.pool_pre_ping)
|
||||||
return engine
|
return engine
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -8,5 +8,8 @@ class User (Base):
|
|||||||
__tablename__ = "user"
|
__tablename__ = "user"
|
||||||
|
|
||||||
id: Mapped[int] = mapped_column(BIGINT, primary_key=True, unique=True, autoincrement=False)
|
id: Mapped[int] = mapped_column(BIGINT, primary_key=True, unique=True, autoincrement=False)
|
||||||
username: Mapped[int] = mapped_column(String(32), unique=True, nullable=True)
|
username: Mapped[str] = mapped_column(String(32), unique=True, nullable=True)
|
||||||
# additional fields go here
|
# additional fields go here
|
||||||
|
|
||||||
|
def __init__(self, id: int, username: str):
|
||||||
|
super().__init__(id=id, username=username)
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
# keyboards will be defined here
|
|
||||||
@@ -27,4 +27,3 @@ def create_logger(name: str,
|
|||||||
logger.addHandler(file_handler)
|
logger.addHandler(file_handler)
|
||||||
|
|
||||||
return logger
|
return logger
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
from telebot import TeleBot
|
from telebot import TeleBot
|
||||||
|
|
||||||
from .arguments import ExtraArguments
|
|
||||||
from ..i18n import I18N
|
from ..i18n import I18N
|
||||||
|
from .arguments import ArgumentsMiddleware
|
||||||
|
from .database import DatabaseMiddleware
|
||||||
|
|
||||||
|
|
||||||
def setup_middlewares(bot: TeleBot, i18n: I18N):
|
def setup_middlewares(bot: TeleBot, i18n: I18N, engine):
|
||||||
bot.setup_middleware(ExtraArguments(i18n))
|
bot.setup_middleware(ArgumentsMiddleware(i18n))
|
||||||
|
bot.setup_middleware(DatabaseMiddleware(engine))
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ from telebot.handler_backends import BaseMiddleware
|
|||||||
from telebot.types import Message, CallbackQuery
|
from telebot.types import Message, CallbackQuery
|
||||||
|
|
||||||
|
|
||||||
class ExtraArguments(BaseMiddleware):
|
class ArgumentsMiddleware (BaseMiddleware):
|
||||||
def __init__(self, i18n):
|
def __init__(self, i18n):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.i18n = i18n
|
self.i18n = i18n
|
||||||
|
|||||||
@@ -0,0 +1,29 @@
|
|||||||
|
from telebot.handler_backends import BaseMiddleware
|
||||||
|
from telebot.types import Message, CallbackQuery
|
||||||
|
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
|
||||||
|
from ..database.models import User
|
||||||
|
|
||||||
|
|
||||||
|
class DatabaseMiddleware (BaseMiddleware):
|
||||||
|
def __init__(self, engine):
|
||||||
|
super().__init__()
|
||||||
|
self.engine = engine
|
||||||
|
self.update_types = ["message", "callback_query"]
|
||||||
|
|
||||||
|
def pre_process(self, obj: [Message, CallbackQuery], data: dict):
|
||||||
|
session = Session(self.engine)
|
||||||
|
user = session.get(User, obj.from_user.id)
|
||||||
|
if user is None:
|
||||||
|
user = User(id=obj.from_user.id, username=obj.from_user.username)
|
||||||
|
session.add(user)
|
||||||
|
session.commit()
|
||||||
|
data["db"] = session
|
||||||
|
data["user"] = user
|
||||||
|
|
||||||
|
def post_process(self, message, data: dict, exception: BaseException):
|
||||||
|
if "db" in data:
|
||||||
|
session: Session = data["db"]
|
||||||
|
session.rollback()
|
||||||
|
session.close()
|
||||||
@@ -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
|
|
||||||
|
|||||||
@@ -0,0 +1,35 @@
|
|||||||
|
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
|
||||||
+4
-1
@@ -1,6 +1,9 @@
|
|||||||
pytelegrambotapi
|
pytelegrambotapi
|
||||||
environs
|
|
||||||
pyyaml
|
pyyaml
|
||||||
sqlalchemy
|
sqlalchemy
|
||||||
alembic
|
alembic
|
||||||
psycopg
|
psycopg
|
||||||
|
pymysql[rsa]
|
||||||
|
flask
|
||||||
|
gunicorn
|
||||||
|
redis
|
||||||
|
|||||||
Reference in New Issue
Block a user