Template
Compare commits
24
Commits
6ffae054c0
..
markup
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d3ed6e4006 | ||
|
|
a8f39d4630 | ||
|
|
b3a1ea1309 | ||
|
|
c309d4975e | ||
|
|
4ac6be06cf | ||
|
|
a1bd24d3fc | ||
|
|
238d144b6d | ||
|
|
db3e02356d | ||
|
|
e230d6f3dc | ||
|
|
f8c09d963e | ||
|
|
cd5845c180 | ||
|
|
f62e05fd06 | ||
|
|
3ec83e408c | ||
|
|
d2c6b972e5 | ||
|
|
e09347a03f | ||
|
|
fbe174e4fb | ||
|
|
273c0bf2da | ||
|
|
7b0aac673f | ||
|
|
028c1ac91d | ||
|
|
028d51af18 | ||
|
|
1a0341b3dd | ||
|
|
e1fdbcb60c | ||
|
|
c1d13f208a | ||
|
|
2f6e3b6c15 |
@@ -3,4 +3,6 @@ __pycache__/
|
|||||||
venv/
|
venv/
|
||||||
.venv/
|
.venv/
|
||||||
logs/
|
logs/
|
||||||
|
data/
|
||||||
*.env
|
*.env
|
||||||
|
*.db
|
||||||
|
|||||||
+39
@@ -0,0 +1,39 @@
|
|||||||
|
ARG PYTHON_VERSION=3.10
|
||||||
|
FROM python:${PYTHON_VERSION}-slim
|
||||||
|
|
||||||
|
# avoid .pyc and buffering stdout/stderr
|
||||||
|
ENV PYTHONDONTWRITEBYTECODE=1
|
||||||
|
ENV PYTHONUNBUFFERED=1
|
||||||
|
|
||||||
|
# add non-root user
|
||||||
|
ARG UID=1000
|
||||||
|
RUN adduser \
|
||||||
|
--disabled-password \
|
||||||
|
--no-create-home \
|
||||||
|
--uid "${UID}" \
|
||||||
|
--home "/app" \
|
||||||
|
bot
|
||||||
|
|
||||||
|
# install requirements
|
||||||
|
RUN --mount=type=cache,target=/root/.cache/pip \
|
||||||
|
--mount=type=bind,source=requirements.txt,target=requirements.txt \
|
||||||
|
pip install -r requirements.txt
|
||||||
|
|
||||||
|
# copy sources
|
||||||
|
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/
|
||||||
|
|
||||||
|
# prepare environment
|
||||||
|
ENV I18N_PATH=/data/i18n.yaml
|
||||||
|
ENV DB_URL=sqlite:////data/bot.db
|
||||||
|
|
||||||
|
RUN mkdir -p /data
|
||||||
|
RUN chown bot:bot /data
|
||||||
|
VOLUME /data
|
||||||
|
|
||||||
|
USER bot
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
ENTRYPOINT ["./docker-entrypoint.sh"]
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
version: "3.0"
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
i18n: {}
|
||||||
|
redis-config: {}
|
||||||
|
redis-data: {}
|
||||||
|
|
||||||
|
services:
|
||||||
|
bot:
|
||||||
|
build:
|
||||||
|
dockerfile: Dockerfile
|
||||||
|
context: .
|
||||||
|
depends_on: [redis]
|
||||||
|
restart: unless-stopped
|
||||||
|
volumes:
|
||||||
|
- i18n:/i18n
|
||||||
|
env_file: .env
|
||||||
|
environment:
|
||||||
|
- SS_TYPE=memory # redis currently is broken
|
||||||
|
- SS_REDIS_HOST=redis
|
||||||
|
- SS_REDIS_PORT=6379
|
||||||
|
- SS_REDIS_PASSWORD=bot
|
||||||
|
|
||||||
|
redis:
|
||||||
|
image: redis
|
||||||
|
restart: unless-stopped
|
||||||
|
volumes:
|
||||||
|
- redis-config:/etc/redis
|
||||||
|
- redis-data:/data
|
||||||
|
command: redis-server --save 20 1 --loglevel warning --requirepass bot
|
||||||
@@ -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
|
||||||
@@ -1,3 +1,7 @@
|
|||||||
en:
|
en:
|
||||||
start: "Hello, {message.from_user.full_name}"
|
start: "Hello, {message.from_user.full_name}"
|
||||||
help: ""
|
help: "Just simple bot, which greets the user. Use /start"
|
||||||
|
|
||||||
|
ru:
|
||||||
|
start: "Привет, {message.from_user.full_name}"
|
||||||
|
help: "Это простой бот, который приветствует пользователя. Используй /start"
|
||||||
|
|||||||
@@ -0,0 +1,117 @@
|
|||||||
|
# A generic, single database configuration.
|
||||||
|
|
||||||
|
[alembic]
|
||||||
|
# path to migration scripts
|
||||||
|
# Use forward slashes (/) also on windows to provide an os agnostic path
|
||||||
|
script_location = migrations
|
||||||
|
|
||||||
|
# template used to generate migration file names; The default value is %%(rev)s_%%(slug)s
|
||||||
|
# Uncomment the line below if you want the files to be prepended with date and time
|
||||||
|
# see https://alembic.sqlalchemy.org/en/latest/tutorial.html#editing-the-ini-file
|
||||||
|
# for all available tokens
|
||||||
|
# file_template = %%(year)d_%%(month).2d_%%(day).2d_%%(hour).2d%%(minute).2d-%%(rev)s_%%(slug)s
|
||||||
|
|
||||||
|
# sys.path path, will be prepended to sys.path if present.
|
||||||
|
# defaults to the current working directory.
|
||||||
|
prepend_sys_path = .
|
||||||
|
|
||||||
|
# timezone to use when rendering the date within the migration file
|
||||||
|
# as well as the filename.
|
||||||
|
# If specified, requires the python>=3.9 or backports.zoneinfo library.
|
||||||
|
# Any required deps can installed by adding `alembic[tz]` to the pip requirements
|
||||||
|
# string value is passed to ZoneInfo()
|
||||||
|
# leave blank for localtime
|
||||||
|
# timezone =
|
||||||
|
|
||||||
|
# max length of characters to apply to the "slug" field
|
||||||
|
# truncate_slug_length = 40
|
||||||
|
|
||||||
|
# set to 'true' to run the environment during
|
||||||
|
# the 'revision' command, regardless of autogenerate
|
||||||
|
# revision_environment = false
|
||||||
|
|
||||||
|
# set to 'true' to allow .pyc and .pyo files without
|
||||||
|
# a source .py file to be detected as revisions in the
|
||||||
|
# versions/ directory
|
||||||
|
# sourceless = false
|
||||||
|
|
||||||
|
# version location specification; This defaults
|
||||||
|
# to migrations/versions. When using multiple version
|
||||||
|
# directories, initial revisions must be specified with --version-path.
|
||||||
|
# The path separator used here should be the separator specified by "version_path_separator" below.
|
||||||
|
# version_locations = %(here)s/bar:%(here)s/bat:migrations/versions
|
||||||
|
|
||||||
|
# version path separator; As mentioned above, this is the character used to split
|
||||||
|
# version_locations. The default within new alembic.ini files is "os", which uses os.pathsep.
|
||||||
|
# If this key is omitted entirely, it falls back to the legacy behavior of splitting on spaces and/or commas.
|
||||||
|
# Valid values for version_path_separator are:
|
||||||
|
#
|
||||||
|
# version_path_separator = :
|
||||||
|
# version_path_separator = ;
|
||||||
|
# version_path_separator = space
|
||||||
|
version_path_separator = os # Use os.pathsep. Default configuration used for new projects.
|
||||||
|
|
||||||
|
# set to 'true' to search source files recursively
|
||||||
|
# in each "version_locations" directory
|
||||||
|
# new in Alembic version 1.10
|
||||||
|
# recursive_version_locations = false
|
||||||
|
|
||||||
|
# the output encoding used when revision files
|
||||||
|
# are written from script.py.mako
|
||||||
|
# output_encoding = utf-8
|
||||||
|
|
||||||
|
# ignore this since it will be loaded in migrations/env.py
|
||||||
|
sqlalchemy.url = driver://user:pass@localhost/dbname
|
||||||
|
|
||||||
|
|
||||||
|
[post_write_hooks]
|
||||||
|
# post_write_hooks defines scripts or Python functions that are run
|
||||||
|
# on newly generated revision scripts. See the documentation for further
|
||||||
|
# detail and examples
|
||||||
|
|
||||||
|
# format using "black" - use the console_scripts runner, against the "black" entrypoint
|
||||||
|
# hooks = black
|
||||||
|
# black.type = console_scripts
|
||||||
|
# black.entrypoint = black
|
||||||
|
# black.options = -l 79 REVISION_SCRIPT_FILENAME
|
||||||
|
|
||||||
|
# lint with attempts to fix using "ruff" - use the exec runner, execute a binary
|
||||||
|
# hooks = ruff
|
||||||
|
# ruff.type = exec
|
||||||
|
# ruff.executable = %(here)s/.venv/bin/ruff
|
||||||
|
# ruff.options = --fix REVISION_SCRIPT_FILENAME
|
||||||
|
|
||||||
|
# Logging configuration
|
||||||
|
[loggers]
|
||||||
|
keys = root,sqlalchemy,alembic
|
||||||
|
|
||||||
|
[handlers]
|
||||||
|
keys = console
|
||||||
|
|
||||||
|
[formatters]
|
||||||
|
keys = generic
|
||||||
|
|
||||||
|
[logger_root]
|
||||||
|
level = WARN
|
||||||
|
handlers = console
|
||||||
|
qualname =
|
||||||
|
|
||||||
|
[logger_sqlalchemy]
|
||||||
|
level = WARN
|
||||||
|
handlers =
|
||||||
|
qualname = sqlalchemy.engine
|
||||||
|
|
||||||
|
[logger_alembic]
|
||||||
|
level = INFO
|
||||||
|
handlers =
|
||||||
|
qualname = alembic
|
||||||
|
|
||||||
|
[handler_console]
|
||||||
|
class = StreamHandler
|
||||||
|
args = (sys.stderr,)
|
||||||
|
level = NOTSET
|
||||||
|
formatter = generic
|
||||||
|
|
||||||
|
[formatter_generic]
|
||||||
|
format = %(levelname)-5.5s [%(name)s] %(message)s
|
||||||
|
datefmt = %H:%M:%S
|
||||||
@@ -0,0 +1,75 @@
|
|||||||
|
from logging.config import fileConfig
|
||||||
|
|
||||||
|
from sqlalchemy import engine_from_config
|
||||||
|
from sqlalchemy import pool
|
||||||
|
|
||||||
|
from alembic import context
|
||||||
|
|
||||||
|
from mybot.config import Config as AppConfig
|
||||||
|
from mybot.database import Base
|
||||||
|
import mybot.database.models # do not delete this
|
||||||
|
|
||||||
|
|
||||||
|
config = context.config
|
||||||
|
|
||||||
|
if config.config_file_name is not None:
|
||||||
|
fileConfig(config.config_file_name)
|
||||||
|
|
||||||
|
# add your model's MetaData object here
|
||||||
|
target_metadata = Base.metadata
|
||||||
|
|
||||||
|
# set sqlalchemy.url since it can not be set in alembic.ini file
|
||||||
|
app_config = AppConfig.from_env()
|
||||||
|
config.set_main_option("sqlalchemy.url", app_config.database.url)
|
||||||
|
|
||||||
|
|
||||||
|
def run_migrations_offline() -> None:
|
||||||
|
"""Run migrations in 'offline' mode.
|
||||||
|
|
||||||
|
This configures the context with just a URL
|
||||||
|
and not an Engine, though an Engine is acceptable
|
||||||
|
here as well. By skipping the Engine creation
|
||||||
|
we don't even need a DBAPI to be available.
|
||||||
|
|
||||||
|
Calls to context.execute() here emit the given string to the
|
||||||
|
script output.
|
||||||
|
|
||||||
|
"""
|
||||||
|
url = config.get_main_option("sqlalchemy.url")
|
||||||
|
context.configure(
|
||||||
|
url=url,
|
||||||
|
target_metadata=target_metadata,
|
||||||
|
literal_binds=True,
|
||||||
|
dialect_opts={"paramstyle": "named"},
|
||||||
|
)
|
||||||
|
|
||||||
|
with context.begin_transaction():
|
||||||
|
context.run_migrations()
|
||||||
|
|
||||||
|
|
||||||
|
def run_migrations_online() -> None:
|
||||||
|
"""Run migrations in 'online' mode.
|
||||||
|
|
||||||
|
In this scenario we need to create an Engine
|
||||||
|
and associate a connection with the context.
|
||||||
|
|
||||||
|
"""
|
||||||
|
connectable = engine_from_config(
|
||||||
|
config.get_section(config.config_ini_section, {}),
|
||||||
|
prefix="sqlalchemy.",
|
||||||
|
poolclass=pool.NullPool,
|
||||||
|
)
|
||||||
|
|
||||||
|
with connectable.connect() as connection:
|
||||||
|
context.configure(
|
||||||
|
connection=connection, target_metadata=target_metadata
|
||||||
|
)
|
||||||
|
|
||||||
|
with context.begin_transaction():
|
||||||
|
context.run_migrations()
|
||||||
|
|
||||||
|
|
||||||
|
if context.is_offline_mode():
|
||||||
|
run_migrations_offline()
|
||||||
|
else:
|
||||||
|
run_migrations_online()
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
"""${message}
|
||||||
|
|
||||||
|
Revision ID: ${up_revision}
|
||||||
|
Revises: ${down_revision | comma,n}
|
||||||
|
Create Date: ${create_date}
|
||||||
|
|
||||||
|
"""
|
||||||
|
from typing import Sequence, Union
|
||||||
|
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
${imports if imports else ""}
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision: str = ${repr(up_revision)}
|
||||||
|
down_revision: Union[str, None] = ${repr(down_revision)}
|
||||||
|
branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)}
|
||||||
|
depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)}
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade() -> None:
|
||||||
|
${upgrades if upgrades else "pass"}
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade() -> None:
|
||||||
|
${downgrades if downgrades else "pass"}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
"""Add User model
|
||||||
|
|
||||||
|
Revision ID: ef447fba99de
|
||||||
|
Revises:
|
||||||
|
Create Date: 2024-07-26 01:43:16.748576
|
||||||
|
|
||||||
|
"""
|
||||||
|
from typing import Sequence, Union
|
||||||
|
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision: str = 'ef447fba99de'
|
||||||
|
down_revision: Union[str, None] = None
|
||||||
|
branch_labels: Union[str, Sequence[str], None] = None
|
||||||
|
depends_on: Union[str, Sequence[str], None] = None
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade() -> None:
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
op.create_table('user',
|
||||||
|
sa.Column('id', sa.BIGINT(), autoincrement=False, nullable=False),
|
||||||
|
sa.Column('username', sa.String(length=32), nullable=True),
|
||||||
|
sa.PrimaryKeyConstraint('id'),
|
||||||
|
sa.UniqueConstraint('id'),
|
||||||
|
sa.UniqueConstraint('username')
|
||||||
|
)
|
||||||
|
# ### end Alembic commands ###
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade() -> None:
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
op.drop_table('user')
|
||||||
|
# ### end Alembic commands ###
|
||||||
+46
-7
@@ -1,12 +1,51 @@
|
|||||||
from .config import Config
|
from telebot import TeleBot
|
||||||
|
|
||||||
|
from .config import Config, load_config
|
||||||
from .logger import create_logger
|
from .logger import create_logger
|
||||||
from .bot import create_bot
|
|
||||||
from .i18n import I18N
|
from .i18n import I18N
|
||||||
|
from .database import get_engine
|
||||||
|
from .states import get_state_storage
|
||||||
|
from .handlers import register_handlers
|
||||||
|
from .middlewares import setup_middlewares
|
||||||
|
from .filters import add_custom_filters
|
||||||
|
from .markup import setup_markup
|
||||||
|
from .webhook import create_app
|
||||||
|
|
||||||
|
|
||||||
|
def create_bot(config: Config, i18n: I18N, engine):
|
||||||
|
state_storage = get_state_storage(config.states)
|
||||||
|
bot = TeleBot(config.bot.token,
|
||||||
|
parse_mode=config.bot.parse_mode,
|
||||||
|
skip_pending=config.bot.skip_pending,
|
||||||
|
num_threads=config.bot.num_threads,
|
||||||
|
use_class_middlewares=True,
|
||||||
|
state_storage=state_storage,
|
||||||
|
threaded=False if config.use_webhook else True)
|
||||||
|
register_handlers(bot)
|
||||||
|
markup = setup_markup(bot, i18n)
|
||||||
|
setup_middlewares(bot, i18n, engine, markup)
|
||||||
|
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)
|
||||||
|
return bot
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
config = Config()
|
config = load_config()
|
||||||
logger = create_logger("mybot", config.LOG_LEVEL)
|
# logger = create_logger("mybot", config.log_level)
|
||||||
i18n = I18N(logger, config.I18N_PATH)
|
i18n = I18N(config.i18n)
|
||||||
bot = create_bot(config, logger, i18n)
|
engine = get_engine(config.database)
|
||||||
bot.infinity_polling(config.TIMEOUT, config.DROP_PENDING, config.POLLING_TIMEOUT)
|
bot = create_bot(config, i18n, engine)
|
||||||
|
|
||||||
|
if config.use_webhook:
|
||||||
|
app = create_app(bot, config)
|
||||||
|
return app
|
||||||
|
|
||||||
|
bot.infinity_polling(
|
||||||
|
timeout=config.bot.timeout,
|
||||||
|
long_polling_timeout=config.bot.polling_timeout,
|
||||||
|
skip_pending=config.bot.skip_pending
|
||||||
|
)
|
||||||
|
|||||||
@@ -1,32 +0,0 @@
|
|||||||
from telebot import TeleBot
|
|
||||||
from telebot.storage import StateMemoryStorage, StateRedisStorage
|
|
||||||
|
|
||||||
from .handlers import register_handlers
|
|
||||||
from .middlewares import setup_middlewares
|
|
||||||
|
|
||||||
|
|
||||||
def create_bot(config, logger, i18n):
|
|
||||||
if config.SS_TYPE == "memory":
|
|
||||||
state_storage = StateMemoryStorage()
|
|
||||||
elif config.SS_TYPE == "redis":
|
|
||||||
state_storage = StateRedisStorage(config.SS_REDIS_HOST,
|
|
||||||
config.SS_REDIS_PORT,
|
|
||||||
config.SS_REDIS_DB,
|
|
||||||
config.SS_REDIS_PASS)
|
|
||||||
else:
|
|
||||||
raise RuntimeWarning(f"Unknown state storage type: '{config.SS_TYPE}'")
|
|
||||||
|
|
||||||
bot = TeleBot(config.BOT_TOKEN,
|
|
||||||
parse_mode=config.PARSE_MODE,
|
|
||||||
skip_pending=config.DROP_PENDING,
|
|
||||||
num_threads=config.NUM_THREADS,
|
|
||||||
use_class_middlewares=True,
|
|
||||||
state_storage=state_storage)
|
|
||||||
|
|
||||||
logger.debug("Setting up middlewares")
|
|
||||||
setup_middlewares(bot, logger, i18n)
|
|
||||||
|
|
||||||
logger.debug("Registering handlers")
|
|
||||||
register_handlers(bot)
|
|
||||||
|
|
||||||
return bot
|
|
||||||
+105
-22
@@ -1,30 +1,113 @@
|
|||||||
import os
|
import os
|
||||||
|
from dataclasses import dataclass
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class BotConfig:
|
||||||
|
token: str
|
||||||
|
skip_pending: bool
|
||||||
|
timeout: int
|
||||||
|
polling_timeout: int
|
||||||
|
num_threads: int
|
||||||
|
parse_mode: str
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_env(cls):
|
||||||
|
return cls(os.getenv("BOT_TOKEN"),
|
||||||
|
bool(int(os.getenv("BOT_SKIP_PENDING", True))),
|
||||||
|
int(os.getenv("BOT_TIMEOUT", 20)),
|
||||||
|
int(os.getenv("BOT_POLLING_TIMEOUT", 20)),
|
||||||
|
int(os.getenv("BOT_NUM_THREADS", 2)),
|
||||||
|
os.getenv("BOT_PARSE_MODE", "html"))
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class WebhookConfig:
|
||||||
|
domain: str
|
||||||
|
url_path: str
|
||||||
|
max_connections: int
|
||||||
|
drop_pending_updates: bool
|
||||||
|
|
||||||
|
@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))))
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class I18NConfig:
|
||||||
|
path: str
|
||||||
|
lang: str
|
||||||
|
fallback_lang: str
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_env(cls):
|
||||||
|
return cls(os.getenv("I18N_PATH", "i18n.yaml"),
|
||||||
|
os.getenv("I18N_LANG", "en"),
|
||||||
|
os.getenv("I18N_FALLBACK_LANG", "en"))
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class StateStorageConfig:
|
||||||
|
type: str
|
||||||
|
redis_host: str
|
||||||
|
redis_port: int
|
||||||
|
redis_db: int
|
||||||
|
redis_pass: str
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_env(cls):
|
||||||
|
return cls(os.getenv("SS_TYPE", "memory"),
|
||||||
|
os.getenv("SS_REDIS_HOST"),
|
||||||
|
int(os.getenv("SS_REDIS_PORT", 6379)),
|
||||||
|
int(os.getenv("SS_REDIS_DB", 0)),
|
||||||
|
os.getenv("SS_REDIS_PASS"))
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class DatabaseConfig:
|
||||||
|
url: str
|
||||||
|
pool_recycle: int
|
||||||
|
pool_pre_ping: bool
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_env(cls):
|
||||||
|
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
|
||||||
class Config:
|
class Config:
|
||||||
# bot setup
|
bot: BotConfig
|
||||||
BOT_TOKEN: str = os.getenv("BOT_TOKEN")
|
i18n: I18NConfig
|
||||||
OWNER_ID: int = int(os.getenv("OWNER_ID", 1))
|
states: StateStorageConfig
|
||||||
LOG_LEVEL: str = os.getenv("LOG_LEVEL", "INFO")
|
database: DatabaseConfig
|
||||||
|
webhook: WebhookConfig
|
||||||
|
|
||||||
# bot behaviour
|
use_webhook: bool
|
||||||
DROP_PENDING: bool = bool(int(os.getenv("DROP_PENDING", True)))
|
log_level: str
|
||||||
TIMEOUT: int = int(os.getenv("TIMEOUT", 20))
|
owner_id: int
|
||||||
POLLING_TIMEOUT: int = int(os.getenv("POLLING_TIMEOUT", 20))
|
|
||||||
NUM_THREADS: int = int(os.getenv("NUM_THREADS", 2))
|
|
||||||
PARSE_MODE: str = os.getenv("PARSE_MODE", "html")
|
|
||||||
|
|
||||||
# i18n
|
@classmethod
|
||||||
I18N_PATH: str = os.getenv("I18N_PATH", "i18n.yaml")
|
def from_env(cls):
|
||||||
I18N_LANG: str = os.getenv("I18N_LANG", "en")
|
return cls(
|
||||||
|
bot=BotConfig.from_env(),
|
||||||
|
i18n=I18NConfig.from_env(),
|
||||||
|
states=StateStorageConfig.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"),
|
||||||
|
owner_id=int(os.getenv("OWNER_ID", 1)),
|
||||||
|
)
|
||||||
|
|
||||||
# state storage
|
|
||||||
SS_TYPE: str = os.getenv("SS_TYPE", "memory").lower()
|
|
||||||
SS_REDIS_HOST: str = os.getenv("SS_REDIS_HOST")
|
|
||||||
SS_REDIS_PORT: int = int(os.getenv("SS_REDIS_PORT"), 6379)
|
|
||||||
SS_REDIS_DB: int = int(os.getenv("SS_REDIS_DB", 0))
|
|
||||||
SS_REDIS_PASS: str = os.getenv("SS_REDIS_PASS")
|
|
||||||
|
|
||||||
def __init__(self):
|
def load_config() -> Config:
|
||||||
if not self.BOT_TOKEN:
|
return Config.from_env()
|
||||||
raise RuntimeError("Missing BOT_TOKEN")
|
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
from sqlalchemy import create_engine
|
||||||
|
from sqlalchemy.orm import DeclarativeBase
|
||||||
|
|
||||||
|
from ..config import DatabaseConfig
|
||||||
|
|
||||||
|
|
||||||
|
def get_engine(config: DatabaseConfig):
|
||||||
|
engine = create_engine(config.url,
|
||||||
|
pool_recycle=config.pool_recycle,
|
||||||
|
pool_pre_ping=config.pool_pre_ping)
|
||||||
|
return engine
|
||||||
|
|
||||||
|
|
||||||
|
class Base (DeclarativeBase):
|
||||||
|
pass
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
from sqlalchemy import BIGINT, String
|
||||||
|
from sqlalchemy.orm import Mapped, mapped_column
|
||||||
|
|
||||||
|
from . import Base
|
||||||
|
|
||||||
|
|
||||||
|
class User (Base):
|
||||||
|
__tablename__ = "user"
|
||||||
|
|
||||||
|
id: Mapped[int] = mapped_column(BIGINT, primary_key=True, unique=True, autoincrement=False)
|
||||||
|
username: Mapped[str] = mapped_column(String(32), unique=True, nullable=True)
|
||||||
|
# additional fields go here
|
||||||
|
|
||||||
|
def __init__(self, id: int, username: str):
|
||||||
|
super().__init__(id=id, username=username)
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
from telebot import TeleBot
|
||||||
|
from telebot.custom_filters import StateFilter
|
||||||
|
|
||||||
|
from .isowner import IsOwnerFilter
|
||||||
|
from ..config import Config
|
||||||
|
|
||||||
|
|
||||||
|
def add_custom_filters(bot: TeleBot, config: Config):
|
||||||
|
bot.add_custom_filter(StateFilter(bot))
|
||||||
|
bot.add_custom_filter(IsOwnerFilter(config.owner_id))
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
from telebot.custom_filters import SimpleCustomFilter
|
||||||
|
from telebot.types import Message
|
||||||
|
|
||||||
|
|
||||||
|
class IsOwnerFilter (SimpleCustomFilter):
|
||||||
|
key = "is_owner"
|
||||||
|
|
||||||
|
def __init__(self, owner_id: int):
|
||||||
|
self.owner_id: int = owner_id
|
||||||
|
|
||||||
|
def check(self, message: Message):
|
||||||
|
return message.from_user.id == self.owner_id
|
||||||
@@ -1,9 +1,13 @@
|
|||||||
from telebot import TeleBot
|
from telebot import TeleBot
|
||||||
from telebot.types import Message
|
from telebot.types import Message, CallbackQuery
|
||||||
|
|
||||||
|
|
||||||
def start(message: Message, bot: TeleBot, t, **kwargs):
|
def start(message: Message, bot: TeleBot, t, m, **kwargs):
|
||||||
bot.send_message(message.chat.id, t("start"))
|
bot.send_message(message.chat.id, t("start"), reply_markup=m("start"))
|
||||||
|
|
||||||
|
|
||||||
|
def start_call(call: CallbackQuery, bot: TeleBot, t, m, **kwargs):
|
||||||
|
bot.send_message(call.message.chat.id, t("start"), reply_markup=m("start"))
|
||||||
|
|
||||||
|
|
||||||
def help_(message, bot, t, **kwargs):
|
def help_(message, bot, t, **kwargs):
|
||||||
@@ -13,3 +17,5 @@ def help_(message, bot, t, **kwargs):
|
|||||||
def register_handlers(bot: TeleBot):
|
def register_handlers(bot: TeleBot):
|
||||||
bot.register_message_handler(start, commands=["start"], pass_bot=True)
|
bot.register_message_handler(start, commands=["start"], pass_bot=True)
|
||||||
bot.register_message_handler(help_, commands=["help"], pass_bot=True)
|
bot.register_message_handler(help_, commands=["help"], pass_bot=True)
|
||||||
|
|
||||||
|
bot.register_callback_query_handler(start_call, lambda call: call.data == "start")
|
||||||
|
|||||||
+19
-8
@@ -1,14 +1,15 @@
|
|||||||
import logging
|
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
from yaml import safe_load
|
from yaml import safe_load
|
||||||
|
|
||||||
|
from .config import I18NConfig
|
||||||
|
|
||||||
|
|
||||||
class I18N:
|
class I18N:
|
||||||
def __init__(self, logger: logging.Logger, path="i18n.yaml", fallback_lang="en"):
|
def __init__(self, config: I18NConfig):
|
||||||
self.logger = logger
|
self._path = config.path
|
||||||
self._path = path
|
self._fallback_lang = config.fallback_lang
|
||||||
self._fallback_lang = fallback_lang
|
self._lang = config.lang or self.fallback_lang
|
||||||
self._dict = dict()
|
self._dict = dict()
|
||||||
self.load()
|
self.load()
|
||||||
|
|
||||||
@@ -20,6 +21,16 @@ class I18N:
|
|||||||
def fallback_lang(self):
|
def fallback_lang(self):
|
||||||
return self._fallback_lang
|
return self._fallback_lang
|
||||||
|
|
||||||
|
def set_lang(self, lang: Optional[str] = None):
|
||||||
|
if lang in self._dict:
|
||||||
|
self._lang = lang
|
||||||
|
else:
|
||||||
|
self._lang = self.fallback_lang
|
||||||
|
|
||||||
|
@property
|
||||||
|
def lang(self):
|
||||||
|
return self._lang
|
||||||
|
|
||||||
def load(self):
|
def load(self):
|
||||||
self._dict.clear()
|
self._dict.clear()
|
||||||
with open(self._path) as f:
|
with open(self._path) as f:
|
||||||
@@ -28,14 +39,14 @@ class I18N:
|
|||||||
raise RuntimeError("I18N file doesn't contain fallback language section")
|
raise RuntimeError("I18N file doesn't contain fallback language section")
|
||||||
|
|
||||||
def get(self, phrase: str, lang: Optional[str] = None):
|
def get(self, phrase: str, lang: Optional[str] = None):
|
||||||
lang = lang or self.fallback_lang
|
lang = lang or self.lang
|
||||||
if lang not in self._dict:
|
if lang not in self._dict:
|
||||||
self.logger.warning(f"Language '{lang}' not found in i18n, using fallback")
|
# self.logger.warning(f"Language '{lang}' not found in i18n, using fallback")
|
||||||
lang = self.fallback_lang
|
lang = self.fallback_lang
|
||||||
lang_dict = self._dict.get(lang)
|
lang_dict = self._dict.get(lang)
|
||||||
result = lang_dict.get(phrase)
|
result = lang_dict.get(phrase)
|
||||||
if result is None:
|
if result is None:
|
||||||
self.logger.error(f"Phrase '{phrase}' not found in language '{lang}'")
|
# self.logger.error(f"Phrase '{phrase}' not found in language '{lang}'")
|
||||||
result = f"<Phrase '{phrase}' not found in language '{lang}'>"
|
result = f"<Phrase '{phrase}' not found in language '{lang}'>"
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
# keyboards will be defined here
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
from telebot import TeleBot
|
||||||
|
|
||||||
|
from ..i18n import I18N
|
||||||
|
from .base import MarkupManager
|
||||||
|
from .simple import SimpleMarkup
|
||||||
|
|
||||||
|
|
||||||
|
def setup_markup(bot: TeleBot, i18n: I18N):
|
||||||
|
markup_mg = MarkupManager(bot, i18n)
|
||||||
|
markup_mg.register_prototype(SimpleMarkup)
|
||||||
|
return markup_mg
|
||||||
@@ -0,0 +1,55 @@
|
|||||||
|
import abc
|
||||||
|
from typing import Optional, Type
|
||||||
|
|
||||||
|
from telebot import TeleBot
|
||||||
|
from telebot.types import CallbackQuery, InlineKeyboardMarkup
|
||||||
|
|
||||||
|
from ..i18n import I18N
|
||||||
|
|
||||||
|
|
||||||
|
class Markup (metaclass=abc.ABCMeta):
|
||||||
|
tag: str
|
||||||
|
|
||||||
|
def __init__(self, bot: TeleBot, i18n: I18N):
|
||||||
|
self.bot = bot
|
||||||
|
self.t = i18n
|
||||||
|
|
||||||
|
def __call__(self, *args, **kwargs):
|
||||||
|
return self.build(*args, **kwargs)
|
||||||
|
|
||||||
|
def check(self, call: CallbackQuery) -> bool:
|
||||||
|
return call.data == self.tag
|
||||||
|
|
||||||
|
@abc.abstractmethod
|
||||||
|
def build(self, *args, **kwargs) -> Optional[InlineKeyboardMarkup]:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class DummyMarkup (Markup):
|
||||||
|
tag = "__dummy"
|
||||||
|
|
||||||
|
def check(self, call: CallbackQuery) -> bool:
|
||||||
|
return True
|
||||||
|
|
||||||
|
def build(self, *args, **kwargs) -> Optional[InlineKeyboardMarkup]:
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
class MarkupManager:
|
||||||
|
def __init__(self, bot: TeleBot, i18n: I18N):
|
||||||
|
self.bot = bot
|
||||||
|
self.i18n = i18n
|
||||||
|
self._prototypes: list[Markup] = []
|
||||||
|
self._dummy = DummyMarkup(self.bot, self.i18n)
|
||||||
|
|
||||||
|
def register_prototype(self, markup_proto_class: Type[Markup]):
|
||||||
|
self._prototypes.append(markup_proto_class(self.bot, self.i18n))
|
||||||
|
|
||||||
|
def __call__(self, tag: str, *args, **kwargs):
|
||||||
|
return self.get(tag).build(*args, **kwargs)
|
||||||
|
|
||||||
|
def get(self, tag: str) -> Optional[Markup]:
|
||||||
|
for mp in self._prototypes:
|
||||||
|
if mp.tag == tag:
|
||||||
|
return mp
|
||||||
|
return None
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
from telebot.types import InlineKeyboardMarkup, InlineKeyboardButton
|
||||||
|
|
||||||
|
from .base import Markup
|
||||||
|
|
||||||
|
|
||||||
|
class SimpleMarkup (Markup):
|
||||||
|
tag = "start"
|
||||||
|
|
||||||
|
def build(self, *args, **kwargs) -> Optional[InlineKeyboardMarkup]:
|
||||||
|
return (InlineKeyboardMarkup()
|
||||||
|
.add(InlineKeyboardButton("start", callback_data="start"))
|
||||||
|
.add(InlineKeyboardButton("help", callback_data="help"))
|
||||||
|
)
|
||||||
@@ -1,9 +1,10 @@
|
|||||||
import logging
|
|
||||||
|
|
||||||
from telebot import TeleBot
|
from telebot import TeleBot
|
||||||
|
|
||||||
from .arguments import ExtraArguments
|
from ..i18n import I18N
|
||||||
|
from .arguments import ArgumentsMiddleware
|
||||||
|
from .database import DatabaseMiddleware
|
||||||
|
|
||||||
|
|
||||||
def setup_middlewares(bot: TeleBot, logger: logging.Logger, i18n):
|
def setup_middlewares(bot: TeleBot, i18n: I18N, engine, markup):
|
||||||
bot.setup_middleware(ExtraArguments(logger, i18n))
|
bot.setup_middleware(ArgumentsMiddleware(i18n, markup))
|
||||||
|
bot.setup_middleware(DatabaseMiddleware(engine))
|
||||||
|
|||||||
@@ -1,22 +1,26 @@
|
|||||||
import logging
|
|
||||||
|
|
||||||
from telebot.handler_backends import BaseMiddleware
|
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, logger: logging.Logger, i18n):
|
def __init__(self, i18n, markup):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.logger = logger
|
|
||||||
self.i18n = i18n
|
self.i18n = i18n
|
||||||
|
self.markup = markup
|
||||||
self.update_types = ["message", "callback_query"]
|
self.update_types = ["message", "callback_query"]
|
||||||
|
|
||||||
def pre_process(self, obj, data: dict):
|
def pre_process(self, obj, data: dict):
|
||||||
data["logger"] = self.logger
|
|
||||||
if isinstance(obj, Message):
|
if isinstance(obj, Message):
|
||||||
data["t"] = self.i18n.customized_call(message=obj)
|
self.pre_process_message(obj, data)
|
||||||
elif isinstance(obj, CallbackQuery):
|
elif isinstance(obj, CallbackQuery):
|
||||||
data["t"] = self.i18n.customized_call(callback=obj)
|
self.pre_process_callback(obj, data)
|
||||||
|
data["m"] = self.markup
|
||||||
|
|
||||||
|
def pre_process_message(self, message: Message, data: dict):
|
||||||
|
data["t"] = self.i18n.customized_call(message=message)
|
||||||
|
|
||||||
|
def pre_process_callback(self, call: CallbackQuery, data: dict):
|
||||||
|
data["t"] = self.i18n.customized_call(callback=call)
|
||||||
|
|
||||||
def post_process(self, message, data: dict, exception: BaseException):
|
def post_process(self, message, data: dict, exception: BaseException):
|
||||||
pass
|
pass
|
||||||
|
|||||||
@@ -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()
|
||||||
@@ -1 +1,17 @@
|
|||||||
|
from telebot.storage import StateMemoryStorage, StateRedisStorage
|
||||||
|
|
||||||
|
from .config import StateStorageConfig
|
||||||
|
|
||||||
|
|
||||||
|
def get_state_storage(config: StateStorageConfig):
|
||||||
|
if config.type == "memory":
|
||||||
|
state_storage = StateMemoryStorage()
|
||||||
|
elif config.type == "redis":
|
||||||
|
state_storage = StateRedisStorage(config.redis_host, config.redis_port,
|
||||||
|
config.redis_db, config.redis_pass)
|
||||||
|
else:
|
||||||
|
raise RuntimeWarning(f"Unknown state storage type: '{config.type}'")
|
||||||
|
return state_storage
|
||||||
|
|
||||||
|
|
||||||
# states will be defined here
|
# states will be defined here
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -1,2 +1,8 @@
|
|||||||
pytelegrambotapi
|
pytelegrambotapi
|
||||||
pyyaml
|
pyyaml
|
||||||
|
sqlalchemy
|
||||||
|
alembic
|
||||||
|
psycopg
|
||||||
|
pymysql
|
||||||
|
flask
|
||||||
|
gunicorn
|
||||||
|
|||||||
Reference in New Issue
Block a user