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

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

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

问题

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

  1. import config
  2. from aiogram import Bot, Dispatcher, executor, types
  3. bot = Bot(config.TOKEN, parse_mode="HTML")
  4. dp = Dispatcher(bot)
  5. @dp.message_handler(commands=['start'])
  6. async def start_cmd(message: types.Message):
  7. await message.answer('Hello!')
  8. @dp.message_handler()
  9. async def echo(message: types.Message):
  10. await message.send_copy(message.from_id)
  11. if __name__ == '__main__':
  12. executor.start_polling(dp, skip_updates=True)

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

  1. @dp.message_handler(content_types='*')
  2. async def echo(message: types.Message):
  3. 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:

  1. import config
  2. from aiogram import Bot, Dispatcher, executor, types
  3. bot = Bot(config.TOKEN, parse_mode="HTML")
  4. dp = Dispatcher(bot)
  5. @dp.message_handler(commands=['start'])
  6. async def start_cmd(message: types.Message):
  7. await message.answer('Hello!')
  8. @dp.message_handler()
  9. async def echo(message: types.Message):
  10. await message.send_copy(message.from_id)
  11. if __name__ == '__main__':
  12. executor.start_polling(dp, skip_updates=True)

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

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

but this did not work.

答案1

得分: 1

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

  1. import config
  2. from aiogram import Bot, Dispatcher, executor, types
  3. from aiogram.enums.content_type import ContentType
  4. bot = Bot(config.TOKEN, parse_mode="HTML")
  5. dp = Dispatcher(bot)
  6. @dp.message_handler(commands=['start'])
  7. async def start_cmd(message: types.Message):
  8. await message.answer('Hello!')
  9. @dp.message_handler(content_types=ContentType.ANY)
  10. async def echo(message: types.Message):
  11. await message.send_copy(message.from_id)
  12. if __name__ == '__main__':
  13. 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:

  1. import config
  2. from aiogram import Bot, Dispatcher, executor, types
  3. from aiogram.enums.content_type import ContentType
  4. bot = Bot(config.TOKEN, parse_mode="HTML")
  5. dp = Dispatcher(bot)
  6. @dp.message_handler(commands=['start'])
  7. async def start_cmd(message: types.Message):
  8. await message.answer('Hello!')
  9. @dp.message_handler(content_types=ContentType.ANY) # or 'any'
  10. async def echo(message: types.Message):
  11. await message.send_copy(message.from_id)
  12. if __name__ == '__main__':
  13. 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:

确定