Telegram python 机器人 - 发送照片问题

Telegram python bot - Send a photo issue

我正在为 telegram-python-bot 而苦恼。我正在尝试在触发操作时发送图像。机器人有一个带有一些选项的菜单,当用户选择其中一个选项时,机器人会回复一些文本响应。我想要的是连同信息一起发送图片。 我所做的是:

def opciones(bot, update, context):
  
  query = bot.callback_query
  query.answer()
  
  if query.data == "option1":
    query.message.reply_text(text=option1_info(), parse_mode='html', quote=False)
    context.bot.send_photo(chat_id=update.effective_chat.id, photo=open(image_option1, 'rb'))

此回复为“TypeError:opciones() 缺少 1 个必需的位置参数:'context'”

我也试过在 query.message.replytext 之后添加这个。

requests.post('https://api.telegram.org/bot' + TOKEN + '/sendPhoto', files={'photo': (image_arcadyan, open(image_arcadyan,'rb'))}, data= {'chat_id': chatId})

这有效,但仅适用于 chat_id,我无法为每个要求它的人或团体更新 chat_id,我尝试使用 chat_id=update.effective_chat.id 但更新参数有问题。 有任何想法吗? 提前致谢

TypeError: opciones() missing 1 required positional argument: 'context'

你得到这个异常是因为在 python-telegram-bot (v12+) 处理程序回调必须接受类型 telegram.Updatetelegram.ext.CallbackContext. 的两个位置参数,即你的函数签名应该是

def opciones(update, context):
    ...

其中 updatetelegram.Update 的实例,contexttelegram.ext.CallbackContext 的实例。

请注意 Bot 实例可用 context.bot。有关 PTB 的一般介绍,请参阅 CallbackContext for more info on that object and the tutorial 的文档。

如果您更改签名并将 query = bot.callback_query 更改为 query = update.callback_query,您的代码段的其余部分看起来没问题。


免责声明:我目前是 python-telegram-bot.

的维护者