简体   繁体   English

为什么带有 Webhook 的 Python 上的电报机器人不能像带有长轮询的机器人那样同时处理来自许多用户的消息?

[英]Why telegram-bot on Python with Webhooks can't process messages from many users simultaneously unlike a bot with Long Polling?

I use aiogram .我使用aiogram Logic of my bot is very simple - he receive messages from user and send echo-message after 10 seconds .我的机器人的逻辑非常简单——他从用户那里接收消息并在 10 秒后发送回显消息。 This is a test bot, but in general, I want to make a bot for buying movies with very big database of users.这是一个测试机器人,但总的来说,我想制作一个用于购买拥有大量用户数据库的电影的机器人。 So, my bot must be able to process messages from many users simultaneously and must receive messages using Webhooks.因此,我的机器人必须能够同时处理来自许多用户的消息,并且必须使用 Webhook 接收消息。 Here are two python scripts:这里有两个 python 脚本:

Telegram-bot on Long Polling :长轮询电报机器人:

import asyncio
import logging
from aiogram import Bot, Dispatcher, executor, types
from bot_files.config import *

# Configure logging
logging.basicConfig(level=logging.INFO)

# Initialize bot and dispatcher
bot = Bot(token=bot_token)
dp = Dispatcher(bot)

@dp.message_handler()
async def echo(message: types.Message):
    await asyncio.sleep(10)
    await message.answer(message.text)

if __name__ == '__main__':
    executor.start_polling(dp, skip_updates=True)

Telegram-bot on Webhooks : Webhooks上的电报机器人:

import asyncio
import logging
from aiogram import Bot, Dispatcher, executor, types
from bot_files.config import *

# Configure logging
logging.basicConfig(level=logging.INFO)

# Initialize bot and dispatcher
bot = Bot(token=bot_token)
dp = Dispatcher(bot)

WEBHOOK_HOST = f'https://7417-176-8-60-184.ngrok.io'
WEBHOOK_PATH = f'/webhook/{bot_token}'
WEBHOOK_URL = f'{WEBHOOK_HOST}{WEBHOOK_PATH}'

# webserver settings
WEBAPP_HOST = '0.0.0.0'
WEBAPP_PORT = os.getenv('PORT', default=5000)

async def on_startup(dispatcher):
    await bot.set_webhook(WEBHOOK_URL, drop_pending_updates=True)

async def on_shutdown(dispatcher):
    await bot.delete_webhook()

@dp.message_handler()
async def echo(message: types.Message):
    await asyncio.sleep(10)
    await message.answer(message.text)

if __name__ == '__main__':
    executor.start_webhook(
        dispatcher=dp,
        webhook_path=WEBHOOK_PATH,
        skip_updates=True,
        on_startup=on_startup,
        on_shutdown=on_shutdown,
        host=WEBAPP_HOST,
        port=WEBAPP_PORT
    )

In the first case, if two users send messages simultaneously, both of messages are processed also simultaneously(acynchrony) - 10 seconds.在第一种情况下,如果两个用户同时发送消息,则两个消息也会同时处理(同步)- 10 秒。 In the second case messages are processed linearly(not asynchrony) - one of two users must wait 20 seconds.在第二种情况下,消息是线性处理的(不是异步的)——两个用户之一必须等待 20 秒。 Why telegram-bot on Python with Webhooks can't process messages from many users simultaneously unlike a bot with Long Polling?为什么带有 Webhook 的 Python 上的电报机器人不能像带有长轮询的机器人那样同时处理来自许多用户的消息?

Actually telegram-bot on Python with Webhooks can process messages from many users simultaneously.实际上,带有 Webhook 的 Python 上的电报机器人可以同时处理来自许多用户的消息。 You need just to put @dp.async_task after handler您只需要将@dp.async_task放在处理程序之后

@dp.message_handler()
@dp.async_task
async def echo(message: types.Message):
    await asyncio.sleep(10)
    await message.answer(message.text)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Python播放来自网址的音频(Telegram-bot) - Play Audio From Url using Python,(Telegram-bot) Python电报机器人:提示输入另一个 - Python telegram-bot: Prompt for another input 如何在 python-telegram-bot 中接收来自用户的消息? - How can I receive messages from users in python-telegram-bot? 电报机器人传感器数据 - Telegram-bot sensor data 为什么我的机器人不能再回复电报消息了? - Why can't my bot reply to telegram messages anymore? 为什么telegram.Bot.getUpdates()长轮询立即返回? - Why does telegram.Bot.getUpdates() long polling returns immediately? 我想用 python 为电报机器人编写一个计时器。 机器人从用户的 msg (str) 中获取“时间”。 如何将“时间”从 msg 转换为 (int) 类型? - I want to write a timer for telegram-bot with python. The bot gets "time" from user's msg (str). How can I convert "time" from msg to (int) type? 有没有办法使用python Telegram-bot获取个人资料图片更新? - Is there a way to get profile picture updates using python Telegram-Bot? Python 3.X 和电报机器人 5.6 消息转发不起作用 - Python 3.X and telegram-bot 5.6 message forwarding is not working 使用电报机器人警报创建成功挂钩 - Create success hook with telegram-bot alert
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM