英文:
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
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论