为什么使用`message.send_copy()`的回声机器人不会对照片做出反应?

huangapple go评论69阅读模式
英文:

Why echo bot with message.send_copy() doesn't react on photos?

问题

我发现一个Telegram机器人不响应图片的问题。这个回声机器人应该复制除了/start以外的每条消息,并使用message.send_copy(message.from_id)发送回去。它可以处理简单的文本消息,但如果我发送一张照片给机器人,它什么也不做。以下是代码:

import config
from aiogram import Bot, Dispatcher, executor, types

bot = Bot(config.TOKEN, parse_mode="HTML")
dp = Dispatcher(bot)

@dp.message_handler(commands=['start'])
async def start_cmd(message: types.Message):
    await message.answer('Hello!')

@dp.message_handler()
async def echo(message: types.Message):
    await message.send_copy(message.from_id)

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

我尝试使用content_types='*',如下所示:

@dp.message_handler(content_types='*')
async def echo(message: types.Message):
    await message.send_copy(message.from_id)

但这没有起作用。

英文:

I was writing a Telegram bot noticed that it doesn't respond to photos. This echo bot should copy every message (other than /start) and sendit back using message.send_copy(message.from_id). It works on simple text messages, but if I send a photo to the bot, it doesn't do anything. Here is the code:

import config
from aiogram import Bot, Dispatcher, executor, types

bot = Bot(config.TOKEN, parse_mode="HTML")
dp = Dispatcher(bot)

@dp.message_handler(commands=['start'])
async def start_cmd(message: types.Message):
    await message.answer('Hello!')

@dp.message_handler()
async def echo(message: types.Message):
    await message.send_copy(message.from_id)

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

I tried to use content_types='*' like this:

@dp.message_handler(content_types='*')
async def echo(message: types.Message):
    await message.send_copy(message.from_id)

but this did not work.

答案1

得分: 1

感谢 Kolay.Ne,我找到了我正在寻找的内容。我应该修改我的代码如下:

import config
from aiogram import Bot, Dispatcher, executor, types
from aiogram.enums.content_type import ContentType

bot = Bot(config.TOKEN, parse_mode="HTML")
dp = Dispatcher(bot)

@dp.message_handler(commands=['start'])
async def start_cmd(message: types.Message):
    await message.answer('Hello!')

@dp.message_handler(content_types=ContentType.ANY)
async def echo(message: types.Message):
    await message.send_copy(message.from_id)

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

content_types=ContentType.ANY 替换 content_types='*'

英文:

Thanks Kolay.Ne I found what I was searching. I should modify my code like this:

import config
from aiogram import Bot, Dispatcher, executor, types
from aiogram.enums.content_type import ContentType

bot = Bot(config.TOKEN, parse_mode="HTML")
dp = Dispatcher(bot)

@dp.message_handler(commands=['start'])
async def start_cmd(message: types.Message):
    await message.answer('Hello!')

@dp.message_handler(content_types=ContentType.ANY) # or 'any'
async def echo(message: types.Message):
    await message.send_copy(message.from_id)

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

replacing content_types='*' with ContentType.ANY.

huangapple
  • 本文由 发表于 2023年7月28日 01:16:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76782088.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定