danbooru-bot/main.py
mctaylors d363fa9afd
fix: use App.HostName (if set) in /about
oh yeah and we're also moving that strange ternary to main.py

Signed-off-by: mctaylors <cantsendmails@mctaylors.ru>
2025-03-26 14:18:41 +03:00

61 lines
1.7 KiB
Python

import configparser
import logging
import os
import requests
from telegram.ext import Application, CommandHandler, InlineQueryHandler
from commands import *
from inline_query import inline_query
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
)
# set higher logging level for httpx to avoid all GET and POST requests being logged
logging.getLogger("httpx").setLevel(logging.WARNING)
logger = logging.getLogger(__name__)
config = configparser.ConfigParser()
config.read("config.ini")
hostname = config.get('App', 'HostName')
hostname = hostname if hostname != "" else config.get('App', 'Host')
def main() -> None:
validate_config()
application = Application.builder().token(get_token()).build()
application.add_handler(CommandHandler("start", start_command))
application.add_handler(CommandHandler("help", help_command))
application.add_handler(CommandHandler("about", about_command))
application.add_handler(InlineQueryHandler(inline_query))
application.run_polling(allowed_updates=Update.ALL_TYPES)
def validate_config() -> None:
# noinspection PyBroadException
try:
response = requests.get(f"http://{config.get('App', 'Host')}/status.json")
if response.status_code != 200:
raise
except:
print("Unable validate App.Host in config.ini.")
exit(1)
def get_token():
if os.getenv("BOT_TOKEN") is not None:
return os.getenv("BOT_TOKEN")
if config.get('General', 'Token') != "":
return config.get('General', 'Token')
print("Set BOT_TOKEN environment variable or use General.Token in config.ini to set bot token.")
exit(1)
if __name__ == "__main__":
main()