小编Jam*_*eed的帖子

如何使用 Telegram 的 python 机器人 API 编辑发送的消息?

文档(https://core.telegram.org/bots/api#editmessagetext)说我需要指定要编辑的消息 ID,但我不知道如何获取该消息 ID。

我尝试将 ID 设置为变量:

import telepot
from telepot.namedTuple import InlineKeyboardMarkup, InlineKeyboardButton

messageEditID = bot.sendMessage(<location ID>, "Test", reply_markup=InlineKeyboardMarkup(inline_keyboard=[[InlineKeyboardButton(text="Testing", callback_data="test"]]))['message_id']
Run Code Online (Sandbox Code Playgroud)

因为此类消息的 POST 数据将是

{"message_id":1140126,"chat":{"title":"<location name>","type":"supergroup","id":<location ID>},"date":1477960655,"from":{"username":"<bot username>","first_name":"<bot name>","id":<bot ID>},"text":"Test"}
Run Code Online (Sandbox Code Playgroud)

然后根据内联回复数据回调

if msg['data'] == 'test':
    bot.editMessage(messageEditID, "Did it work?")
Run Code Online (Sandbox Code Playgroud)

但它会抛出异常并显示以下消息:

Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/telepot/__init__.py", line 738, in collector
    callback(item)
  File "testingStuff.py", line 150, in handle
    bot.editMessageText(messageEditID, "Does it work?")
  File "/usr/local/lib/python3.5/dist-packages/telepot/__init__.py", line 592, in editMessageText
    return self._api_request('editMessageText', _rectify(p))
  File "/usr/local/lib/python3.5/dist-packages/telepot/__init__.py", line 398, in _api_request
    return api.request((self._token, …
Run Code Online (Sandbox Code Playgroud)

python telegram-bot

5
推荐指数
1
解决办法
2万
查看次数

标签 统计

python ×1

telegram-bot ×1