Modified the load code: app.py, load.py

This commit is contained in:
hok7z 2023-02-10 20:57:15 +02:00
parent fa73d39a0b
commit 65eada8924
3 changed files with 52 additions and 35 deletions

50
app.py
View File

@ -1,18 +1,24 @@
#!/usr/bin/env python3
import logging
from aiogram import executor
from load import dp, bot, scheduler
from aiohttp.web_app import Application
from aiohttp.web import run_app
from aiogram.webhook.aiohttp_server import (
SimpleRequestHandler,
setup_application
)
import filters
# import filters
import config
dp.filters_factory.bind(filters.AvaibleRolesFilter)
dp.filters_factory.bind(filters.ReplayMessageFilter)
# dp.filters_factory.bind(filters.AvaibleRolesFilter)
# dp.filters_factory.bind(filters.ReplayMessageFilter)
import handlers
# import handlers
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
@ -25,18 +31,20 @@ WEBHOOK_PATH = f'/bot{config.token}/'
WEBHOOK_URL = f"{WEBHOOK_HOST}{WEBHOOK_PATH}"
async def on_startup(dp):
async def on_startup():
from utils.notify_start import notify_started_bot, database_is_empty
DATABASE_EMPTY = database_is_empty()
if DATABASE_EMPTY:
await bot.send_message(config.second_group_id,
"Member table is empty, run: `!reload`", parse_mode="Markdown")
await bot.send_message(
config.second_group_id,
"Member table is empty, run: `!reload`", parse_mode="Markdown"
)
await notify_started_bot(bot)
from utils.default_commands import set_default_commands
await set_default_commands(dp)
await set_default_commands(bot)
# Reloading users data
from utils import reload_users_data
@ -49,29 +57,29 @@ async def on_startup(dp):
await bot.set_webhook(WEBHOOK_URL)
async def on_shutdown(dp):
async def on_shutdown():
await bot.delete_webhook()
# Close Redis connection.
await dp.storage.close()
await dp.storage.wait_closed()
await bot.session.close()
def main() -> None:
dp.startup.register(on_startup)
dp.shutdown.register(on_shutdown)
if config.USE_WEBHOOK:
executor.start_webhook(
app = Application()
app["bot"] = bot
SimpleRequestHandler(
dispatcher=dp,
webhook_path=WEBHOOK_PATH,
on_startup=on_startup,
on_shutdown=on_shutdown,
skip_updates=True,
host=WEBAPP_HOST,
port=WEBAPP_PORT
)
bot=bot,
).register(app, path=WEBHOOK_PATH)
setup_application(app, dp, bot=bot)
run_app(app, host=WEBAPP_HOST, port=WEBAPP_PORT)
else:
executor.start_polling(dp, skip_updates=True)
dp.run_polling()
if __name__ == '__main__':

18
load.py
View File

@ -1,8 +1,10 @@
from aiogram import Bot, Dispatcher
from aiogram import types
from aiogram.bot.api import TelegramAPIServer
from aiogram.contrib.fsm_storage.memory import MemoryStorage
from aiogram.client.telegram import TelegramAPIServer
from aiogram.fsm.storage.memory import MemoryStorage
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from aiogram.client.session.aiohttp import AiohttpSession
import config
import utils
@ -15,9 +17,13 @@ tgc = utils.TelegramClient(config.api_id, config.api_hash, config.token)
scheduler = AsyncIOScheduler()
bot = Bot(
token=config.token,
server=TelegramAPIServer.from_base(config.telegram_api_server),
session = AiohttpSession(
api=TelegramAPIServer.from_base(config.telegram_api_server),
)
dp = Dispatcher(bot, storage=storage)
bot = Bot(
token=config.token,
session=session,
)
dp = Dispatcher(storage=storage)

View File

@ -1,6 +1,9 @@
async def set_default_commands(dp):
async def set_default_commands(bot):
from load import types
await dp.bot.set_my_commands([
types.BotCommand("start","Start bot"),
types.BotCommand("help","Help")
])
await bot.set_my_commands(
commands=[
types.BotCommand(command="start", description="Start bot"),
types.BotCommand(command="help", description="Help")
]
)