我正在使用 Python 中的 aiogram 库为商店开发 Telegram 机器人。我的项目需要两个机器人,即商店机器人和商店的快递机器人,在同一个应用程序中协同工作。我尝试了各种方法,但我相信最好的解决方案是使用 webhooks。
我尝试编写自己的服务器,以便它将更新发送到正确的机器人。
from aiogram import Bot, Dispatcher, types
from aiogram.contrib.fsm_storage.memory import MemoryStorage
from aiohttp import web
from datetime import datetime
import courier
import registration
import shop
import time
import config
import middlewares
users_notified = {}
start_time = datetime.fromtimestamp(time.time())
dp = Dispatcher(config.bot, storage=MemoryStorage()) #User bot
dpc = Dispatcher(config.bot_courier, storage=MemoryStorage()) #Courier Bot
dp.middleware.setup(middlewares.AntiSpamMiddleware())
dp.middleware.setup(middlewares.OnlineTimeMiddleWare())
registration.BindRegistration(dp)
shop.BindShop(dp)
# Бот курьера
dpc = Dispatcher(config.bot_courier, storage=MemoryStorage())
courier.BindCourier(dpc)
# Create aiohttp application
app = web.Application()
async def on_startup(app):
await config.bot.set_webhook(config.WEBHOOK_URL, drop_pending_updates=False)
await config.bot_courier.set_webhook(config.WEBHOOK_URL_COURIER, drop_pending_updates=False)
async def on_shutdown(app):
await config.bot.delete_webhook()
session1 = await config.bot.get_session()
await session1.close()
await config.bot_courier.delete_webhook()
session2 = await config.bot_courier.get_session()
await session2.close()
async def process_update1(request):
data = await request.json()
update = types.Update(**data)
if update.message.date < start_time:
user_id = update.message.from_user.id
if user_id not in users_notified and config.notify:
await config.bot.send_message(user_id, "There was an error, but we are back online.")
users_notified[user_id] = True # Mark the user as notified
return web.Response()
Bot.set_current(config.bot)
await dp.process_update(update)
return web.Response()
async def process_update2(request):
data = await request.json()
update = types.Update(**data)
# Ignore updates that were sent before the server was started
if update.message.date < start_time:
return web.Response()
Bot.set_current(config.bot_courier)
await dpc.process_update(update)
return web.Response()
app.on_startup.append(on_startup)
app.on_shutdown.append(on_shutdown)
app.router.add_post(config.WEBHOOK_PATH, process_update1)
app.router.add_post(config.WEBHOOK_PATH_COURIER, process_update2)
web.run_app(app, host='0.0.0.0', port=3000)
但是我收到错误“state = Dispatcher.get_current().current_state() ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'NoneType' 对象没有属性 'current_state'”,我在第一次调用“get_current”时得到它,无论它第一次出现在哪里。
请帮忙,解释一下我做错了什么,也许还有其他一些更简单的方法来解决这个问题提前谢谢!
我尝试了不同的方法在一个项目中实现 2 个机器人,尝试使用 asyncio.gather(executor.start_webhook(),...) 将其作为 caroutine 运行。我也尝试使用 long-poolong 但所有这些都无法正常工作。
对我来说,使用 aiogram 2.17.1 和长轮询看起来像这样: 在初始化程序中创建了 2 个 Bot 和 2 个 Dispatcher 对象
async def start() -> None:
initializer = Initializer()
await asyncio.gather(initializer.dp.start_polling(), initializer.admin_dp.start_polling())
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(start())