Compare commits
2 Commits
f8c09d963e
...
db3e02356d
| Author | SHA1 | Date | |
|---|---|---|---|
| db3e02356d | |||
| e230d6f3dc |
@ -21,11 +21,11 @@ RUN --mount=type=cache,target=/root/.cache/pip \
|
|||||||
|
|
||||||
# copy i18n files
|
# copy i18n files
|
||||||
COPY --chown=bot i18n.yaml i18n/i18n.yaml
|
COPY --chown=bot i18n.yaml i18n/i18n.yaml
|
||||||
VOLUME i18n/
|
|
||||||
|
|
||||||
# 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
|
||||||
@ -35,3 +35,5 @@ ENV I18N_PATH=/i18n/i18n.yaml
|
|||||||
USER bot
|
USER bot
|
||||||
|
|
||||||
CMD ["python3", "-m", "mybot"]
|
CMD ["python3", "-m", "mybot"]
|
||||||
|
|
||||||
|
VOLUME i18n/
|
||||||
|
|||||||
@ -21,6 +21,11 @@ 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)
|
||||||
|
if config.use_webhook:
|
||||||
|
bot.delete_webhook()
|
||||||
|
bot.set_webhook(config.webhook.url,
|
||||||
|
drop_pending_updates=config.webhook.drop_pending_updates,
|
||||||
|
max_connections=config.webhook.max_connections)
|
||||||
return bot
|
return bot
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -21,6 +21,19 @@ class BotConfig:
|
|||||||
os.getenv("BOT_PARSE_MODE", "html"))
|
os.getenv("BOT_PARSE_MODE", "html"))
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class WebhookConfig:
|
||||||
|
url: str
|
||||||
|
max_connections: int
|
||||||
|
drop_pending_updates: bool
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_env(cls):
|
||||||
|
return cls(os.getenv("WEBHOOK_URL"),
|
||||||
|
int(os.getenv("WEBHOOK_MAX_CONNECTIONS", 40)),
|
||||||
|
bool(int(os.getenv("WEBHOOK_DROP_PENDING", True))))
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class I18NConfig:
|
class I18NConfig:
|
||||||
path: str
|
path: str
|
||||||
@ -70,7 +83,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
|
||||||
|
|
||||||
@ -81,6 +96,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)),
|
||||||
)
|
)
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
pytelegrambotapi
|
pytelegrambotapi
|
||||||
environs
|
|
||||||
pyyaml
|
pyyaml
|
||||||
sqlalchemy
|
sqlalchemy
|
||||||
alembic
|
alembic
|
||||||
psycopg
|
psycopg
|
||||||
|
flask
|
||||||
|
gunicorn
|
||||||
|
|||||||
27
webapp/__init__.py
Normal file
27
webapp/__init__.py
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
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
|
||||||
16
webapp/bot.py
Normal file
16
webapp/bot.py
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
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)
|
||||||
Loading…
x
Reference in New Issue
Block a user