Skip to content

Commit

Permalink
Add prefix TG_ to all telegram-related config variables
Browse files Browse the repository at this point in the history
  • Loading branch information
gsarrco committed Oct 30, 2023
1 parent 201f7e0 commit 412f5d0
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 36 deletions.
10 changes: 5 additions & 5 deletions config.example.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
2 changes: 1 addition & 1 deletion run.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 6 additions & 29 deletions tgbot/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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')}
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion tgbot/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 412f5d0

Please sign in to comment.