diff --git a/config.example.yaml b/config.example.yaml index 8a4b52f..76f2a01 100644 --- a/config.example.yaml +++ b/config.example.yaml @@ -1,14 +1,14 @@ -TGBOT: # True or False (if True, the bot will be used) -TOKEN: # required if TGBOT is True -WEBHOOK_URL: # required if TGBOT is True -SECRET_TOKEN: # required if TGBOT is True +TG_BOT_ENABLED: # True or False (if True, the bot will be used) +TG_TOKEN: # required if TG_BOT_ENABLED is True +TG_WEBHOOK_URL: # required if TG_BOT_ENABLED is True +TG_SECRET_TOKEN: # required if TG_BOT_ENABLED is True DEV: # True or False PGUSER: PGPASSWORD: PGPORT: PGHOST: PGDATABASE: -ADMIN_TG_ID: # Telegram user ID of the admin, required if TGBOT is True +TG_ADMIN_ID: # Telegram user ID of the admin, required if TG_BOT_ENABLED is True SSL_KEYFILE: # Path to the SSL key file SSL_CERTFILE: # Path to the SSL certificate file TYPESENSE_API_KEY: diff --git a/run.py b/run.py index 157476a..76f9802 100644 --- a/run.py +++ b/run.py @@ -17,7 +17,7 @@ async def run() -> None: routes = server_routes tgbot_application = None - if config['TGBOT']: + if config['TG_BOT_ENABLED']: from tgbot.handlers import set_up_application tgbot_application = await set_up_application() from tgbot.routes import get_routes as get_tgbot_routes diff --git a/tgbot/handlers.py b/tgbot/handlers.py index 2fba580..cb20dbc 100644 --- a/tgbot/handlers.py +++ b/tgbot/handlers.py @@ -16,29 +16,6 @@ filters, CallbackQueryHandler, ) from telegram.ext import ContextTypes -from config import config -from server.base import Source -from server.sources import sources as defined_sources -from .persistence import SQLitePersistence -from .stop_times_filter import StopTimesFilter -import gettext -import logging -import os -import sys -from datetime import timedelta, datetime, date - -import requests -from babel.dates import format_date -from telegram import Update, KeyboardButton, ReplyKeyboardMarkup, InlineKeyboardButton, InlineKeyboardMarkup, \ - ReplyKeyboardRemove, Bot -from telegram.ext import ( - Application, - CommandHandler, - ConversationHandler, - MessageHandler, - filters, CallbackQueryHandler, ) -from telegram.ext import ContextTypes - from config import config from server.base import Source from server.sources import sources as defined_sources @@ -82,7 +59,7 @@ async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: async def announce(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: - if config.get('ADMIN_TG_ID') != update.effective_user.id: + if config.get('TG_ADMIN_ID') != update.effective_user.id: return persistence: SQLitePersistence = thismodule.persistence @@ -537,7 +514,7 @@ async def cancel(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int: async def set_up_application(): persistence = SQLitePersistence() - application = Application.builder().token(config['TOKEN']).persistence(persistence=persistence).build() + application = Application.builder().token(config['TG_TOKEN']).persistence(persistence=persistence).build() thismodule.sources = defined_sources thismodule.persistence = persistence @@ -548,7 +525,7 @@ async def set_up_application(): trans = gettext.translation('messages', localedir, languages=[lang]) _ = trans.gettext language_code = lang if lang != default_lang else '' - r = requests.post(f'https://api.telegram.org/bot{config["TOKEN"]}/setMyCommands', json={ + r = requests.post(f'https://api.telegram.org/bot{config["TG_TOKEN"]}/setMyCommands', json={ 'commands': [ {'command': _('stop'), 'description': _('search_by_stop')}, {'command': _('line'), 'description': _('search_by_line')} @@ -587,10 +564,10 @@ async def set_up_application(): application.add_handler(MessageHandler(filters.Regex(r'^\/announce '), announce)) application.add_handler(conv_handler) bot: Bot = application.bot - webhook_url = config['WEBHOOK_URL'] + '/tg_bot_webhook' + webhook_url = config['TG_WEBHOOK_URL'] + '/tg_bot_webhook' if config.get('DEV', False): - await bot.set_webhook(webhook_url, os.path.join(parent_dir, 'cert.pem'), secret_token=config['SECRET_TOKEN']) + await bot.set_webhook(webhook_url, os.path.join(parent_dir, 'cert.pem'), secret_token=config['TG_SECRET_TOKEN']) else: - await bot.set_webhook(webhook_url, secret_token=config['SECRET_TOKEN']) + await bot.set_webhook(webhook_url, secret_token=config['TG_SECRET_TOKEN']) return application diff --git a/tgbot/routes.py b/tgbot/routes.py index 1d6f573..3d02409 100644 --- a/tgbot/routes.py +++ b/tgbot/routes.py @@ -8,7 +8,7 @@ def get_routes(application): async def telegram(request: Request) -> Response: - if request.headers['X-Telegram-Bot-Api-Secret-Token'] != config['SECRET_TOKEN']: + if request.headers['X-Telegram-Bot-Api-Secret-Token'] != config['TG_SECRET_TOKEN']: return Response(status_code=403) await application.update_queue.put( Update.de_json(data=await request.json(), bot=application.bot)