mirror of
https://github.com/civsocit/olgram.git
synced 2025-12-16 01:26:17 +00:00
Instance в режиме Polling
This commit is contained in:
46
bot.py
46
bot.py
@@ -1,29 +1,65 @@
|
||||
import asyncio
|
||||
from aiogram import Bot, Dispatcher, executor
|
||||
|
||||
import aiogram.types
|
||||
import tortoise.transactions
|
||||
from aiogram import Bot as AioBot, Dispatcher
|
||||
from aiogram.contrib.fsm_storage.memory import MemoryStorage
|
||||
|
||||
from settings import BotSettings
|
||||
from olgram.settings import BotSettings
|
||||
|
||||
from olgram.bot.bots import router as bots_router
|
||||
from olgram.bot.start import router as start_router
|
||||
from olgram.bot.bot import router as bot_router
|
||||
from olgram.utils.database import init_database
|
||||
|
||||
from olgram.models.models import Bot, GroupChat
|
||||
|
||||
from instance.bot import BotInstance
|
||||
|
||||
import typing as ty
|
||||
|
||||
|
||||
async def invite_callback(identify: int, message: aiogram.types.Message):
|
||||
bot = await Bot.get(id=identify)
|
||||
chat, _ = await GroupChat.get_or_create(chat_id=message.chat.id,
|
||||
defaults={"name": message.chat.full_name})
|
||||
if chat not in await bot.group_chats.all():
|
||||
await bot.group_chats.add(chat)
|
||||
|
||||
|
||||
def run_bot(bot: BotInstance, loop: ty.Optional[asyncio.AbstractEventLoop] = None):
|
||||
loop = loop or asyncio.get_event_loop()
|
||||
loop.create_task(bot.start_polling())
|
||||
|
||||
|
||||
async def run_all_bots(loop: asyncio.AbstractEventLoop):
|
||||
bots = await Bot.all()
|
||||
for bot in bots:
|
||||
run_bot(BotInstance(bot.token,
|
||||
bot.super_chat_id,
|
||||
bot.start_text,
|
||||
invite_callback=invite_callback,
|
||||
identify=bot.id), loop)
|
||||
|
||||
|
||||
def main():
|
||||
"""
|
||||
Classic polling
|
||||
"""
|
||||
asyncio.get_event_loop().run_until_complete(init_database())
|
||||
loop = asyncio.get_event_loop()
|
||||
loop.run_until_complete(init_database())
|
||||
|
||||
bot = Bot(BotSettings.token())
|
||||
bot = AioBot(BotSettings.token())
|
||||
dp = Dispatcher(bot, storage=MemoryStorage())
|
||||
|
||||
start_router.setup(dp)
|
||||
bots_router.setup(dp)
|
||||
bot_router.setup(dp)
|
||||
|
||||
executor.start_polling(dp, skip_updates=True)
|
||||
loop.run_until_complete(run_all_bots(loop))
|
||||
loop.create_task(dp.start_polling())
|
||||
|
||||
loop.run_forever()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
Reference in New Issue
Block a user