replace-bot/bot.py

70 lines
1.6 KiB
Python
Raw Normal View History

2022-02-16 18:13:44 +03:00
#!/usr/bin/env python3
import sys
import logging
import asyncio
from aiogram import executor
import filters
import handlers
from utils.announcements import scheduler
from load import dp, bot, config
from utils import set_commands
if (len(sys.argv) >= 2) and (sys.argv[1] == '-u'):
from parser import docs_parse; docs_parse()
sys.exit(0)
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO
)
logger = logging.getLogger("Bot")
WEBAPP_HOST = config.bot("ip")
WEBAPP_PORT = config.bot("port")
WEBHOOK_HOST = f'http://{WEBAPP_HOST}:{WEBAPP_PORT}'
WEBHOOK_PATH = f'/bot{config.bot("token")}/'
WEBHOOK_URL = f"{WEBHOOK_HOST}{WEBHOOK_PATH}"
async def on_startup(dp):
await set_commands(dp)
await bot.set_webhook(url=WEBHOOK_URL)
async def on_shutdown(dp):
await bot.delete_webhook()
def main() -> None:
if config.logging_user:
logger.info("Logging enabled!")
else:
logger.info("Logging disabled!")
#loop = asyncio.get_event_loop()
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
2022-02-22 15:29:44 +03:00
loop.create_task(scheduler())
2022-02-16 18:13:44 +03:00
if config.bot("use_webhook").lower() in ['t', 'true', '1', 'yes', 'y']:
executor.start_webhook(
dispatcher=dp,
loop=loop,
webhook_path=WEBHOOK_PATH,
on_startup=on_startup,
skip_updates=False,
on_shutdown=on_shutdown,
host=WEBAPP_HOST,
port=WEBAPP_PORT,
)
else:
executor.start_polling(dp, skip_updates=True)
if __name__ == '__main__':
main()