Modified the load code: app.py, load.py
This commit is contained in:
parent
fa73d39a0b
commit
65eada8924
56
app.py
56
app.py
|
@ -1,18 +1,24 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from aiogram import executor
|
|
||||||
from load import dp, bot, scheduler
|
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
|
import config
|
||||||
|
|
||||||
|
|
||||||
dp.filters_factory.bind(filters.AvaibleRolesFilter)
|
# dp.filters_factory.bind(filters.AvaibleRolesFilter)
|
||||||
dp.filters_factory.bind(filters.ReplayMessageFilter)
|
# dp.filters_factory.bind(filters.ReplayMessageFilter)
|
||||||
|
|
||||||
import handlers
|
# import handlers
|
||||||
|
|
||||||
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
|
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}"
|
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
|
from utils.notify_start import notify_started_bot, database_is_empty
|
||||||
|
|
||||||
DATABASE_EMPTY = database_is_empty()
|
DATABASE_EMPTY = database_is_empty()
|
||||||
if DATABASE_EMPTY:
|
if DATABASE_EMPTY:
|
||||||
await bot.send_message(config.second_group_id,
|
await bot.send_message(
|
||||||
"Member table is empty, run: `!reload`", parse_mode="Markdown")
|
config.second_group_id,
|
||||||
|
"Member table is empty, run: `!reload`", parse_mode="Markdown"
|
||||||
|
)
|
||||||
|
|
||||||
await notify_started_bot(bot)
|
await notify_started_bot(bot)
|
||||||
|
|
||||||
from utils.default_commands import set_default_commands
|
from utils.default_commands import set_default_commands
|
||||||
await set_default_commands(dp)
|
await set_default_commands(bot)
|
||||||
|
|
||||||
# Reloading users data
|
# Reloading users data
|
||||||
from utils import reload_users_data
|
from utils import reload_users_data
|
||||||
|
@ -49,30 +57,30 @@ async def on_startup(dp):
|
||||||
await bot.set_webhook(WEBHOOK_URL)
|
await bot.set_webhook(WEBHOOK_URL)
|
||||||
|
|
||||||
|
|
||||||
async def on_shutdown(dp):
|
async def on_shutdown():
|
||||||
await bot.delete_webhook()
|
await bot.delete_webhook()
|
||||||
|
|
||||||
# Close Redis connection.
|
# Close Redis connection.
|
||||||
await dp.storage.close()
|
await dp.storage.close()
|
||||||
await dp.storage.wait_closed()
|
await bot.session.close()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def main() -> None:
|
def main() -> None:
|
||||||
|
dp.startup.register(on_startup)
|
||||||
|
dp.shutdown.register(on_shutdown)
|
||||||
|
|
||||||
if config.USE_WEBHOOK:
|
if config.USE_WEBHOOK:
|
||||||
executor.start_webhook(
|
app = Application()
|
||||||
|
app["bot"] = bot
|
||||||
|
SimpleRequestHandler(
|
||||||
dispatcher=dp,
|
dispatcher=dp,
|
||||||
webhook_path=WEBHOOK_PATH,
|
bot=bot,
|
||||||
on_startup=on_startup,
|
).register(app, path=WEBHOOK_PATH)
|
||||||
on_shutdown=on_shutdown,
|
setup_application(app, dp, bot=bot)
|
||||||
skip_updates=True,
|
run_app(app, host=WEBAPP_HOST, port=WEBAPP_PORT)
|
||||||
host=WEBAPP_HOST,
|
|
||||||
port=WEBAPP_PORT
|
|
||||||
)
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
executor.start_polling(dp, skip_updates=True)
|
dp.run_polling()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|
18
load.py
18
load.py
|
@ -1,8 +1,10 @@
|
||||||
from aiogram import Bot, Dispatcher
|
from aiogram import Bot, Dispatcher
|
||||||
from aiogram import types
|
from aiogram import types
|
||||||
from aiogram.bot.api import TelegramAPIServer
|
from aiogram.client.telegram import TelegramAPIServer
|
||||||
from aiogram.contrib.fsm_storage.memory import MemoryStorage
|
from aiogram.fsm.storage.memory import MemoryStorage
|
||||||
from apscheduler.schedulers.asyncio import AsyncIOScheduler
|
from apscheduler.schedulers.asyncio import AsyncIOScheduler
|
||||||
|
from aiogram.client.session.aiohttp import AiohttpSession
|
||||||
|
|
||||||
|
|
||||||
import config
|
import config
|
||||||
import utils
|
import utils
|
||||||
|
@ -15,9 +17,13 @@ tgc = utils.TelegramClient(config.api_id, config.api_hash, config.token)
|
||||||
|
|
||||||
scheduler = AsyncIOScheduler()
|
scheduler = AsyncIOScheduler()
|
||||||
|
|
||||||
bot = Bot(
|
session = AiohttpSession(
|
||||||
token=config.token,
|
api=TelegramAPIServer.from_base(config.telegram_api_server),
|
||||||
server=TelegramAPIServer.from_base(config.telegram_api_server),
|
|
||||||
)
|
)
|
||||||
|
|
||||||
dp = Dispatcher(bot, storage=storage)
|
bot = Bot(
|
||||||
|
token=config.token,
|
||||||
|
session=session,
|
||||||
|
)
|
||||||
|
|
||||||
|
dp = Dispatcher(storage=storage)
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
async def set_default_commands(dp):
|
async def set_default_commands(bot):
|
||||||
from load import types
|
from load import types
|
||||||
await dp.bot.set_my_commands([
|
|
||||||
types.BotCommand("start","Start bot"),
|
await bot.set_my_commands(
|
||||||
types.BotCommand("help","Help")
|
commands=[
|
||||||
])
|
types.BotCommand(command="start", description="Start bot"),
|
||||||
|
types.BotCommand(command="help", description="Help")
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
Reference in New Issue