telegram-bot-template/bot.py

52 lines
1.4 KiB
Python
Raw Normal View History

2023-07-31 15:48:34 +03:00
#!/usr/bin/env python
import logging
from aiogram import executor
from load import dp
from config import parse_bool, config
import filters
import handlers
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO
)
logger = logging.getLogger(__name__)
async def on_startup(dp):
webhook_url = "{host}{path}{token}".format(
host=config["WebHook"]["webhook_host"],
path=config["WebHook"]["webhook_path"],
token=config["Bot"]["token"]
)
await dp.bot.set_webhook(url=webhook_url)
async def on_shutdown(dp):
await dp.bot.delete_webhook()
def main():
skip_updates = parse_bool(text=config["Bot"]["skip_updates"])
if parse_bool(text=config["WebHook"]["use"]):
# https://github.com/aiogram/aiogram/pull/795/commits/a7e0ce2971a0a320849c8b11080fa6b737b6935b
# Needed for async tasks :3
# Only for webhooks! For polling not needed patching file
executor.start_webhook(
dispatcher=dp,
skip_updates=skip_updates,
webhook_path="{}{}".format(config["WebHook"]["webhook_path"], config["Bot"]["token"]),
on_startup=on_startup,
on_shutdown=on_shutdown,
host=config["WebHook"]["app_host"],
port=int(config["WebHook"]["app_port"])
)
else:
executor.start_polling(dp, skip_updates=skip_updates)
if __name__ == '__main__':
main()