gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 17:59:15 51 4
gpt4 key购买 nike

文档 ( 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']

因为此类消息的 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"}

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

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

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

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, method, params, files), **kwargs)
File "/usr/local/lib/python3.5/dist-packages/telepot/api.py", line 131, in request
return _parse(r)
File "/usr/local/lib/python3.5/dist-packages/telepot/api.py", line 126, in _parse
raise exception.TelegramError(description, error_code, data)
telepot.exception.TelegramError: ('Bad Request: Wrong inline message identifier specified', 400, {'description': 'Bad Request: Wrong inline message identifier specified', 'ok': False, 'error_code': 400})

当我尝试根据响应编辑消息时,它抛出了相同的错误,

if msg['data'] == 'test':
msgID = msg['message']['message_id']
bot.editMessageText(msgID, "OBOY")

因为传入内联回复的数据是:

{'chat_instance': '112564336693044113',
'data': 'test',
'from': {'first_name': 'B(', 'id': <user ID>, 'username': '<username>'},
'id': '852908411206280027',
'message': {'chat': {'id': <chat ID>,
'title': 'We da bes',
'type': 'supergroup',
'username': '<chat username>'},
'date': 1477961765,
'from': {'first_name': 'Testing For James',
'id': <bot ID>,
'username': '<bot username>'},
'message_id': 63180,
'text': 'Test'}}

谁能帮我找出哪里出了问题?

最佳答案

这是一个不使用telepot编辑消息的简单示例

演示:Before edit After edited

from telegram.ext import Updater, CommandHandler, MessageHandler, CallbackContext
from telegram import Update
import logging

# Enable logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)

logger = logging.getLogger(__name__)


def start(update: Update, context: CallbackContext) -> None:
context.bot.send_message(chat_id=update.message.chat_id, text="hi")


def edit(update: Update, context: CallbackContext) -> None:
context.bot.editMessageText(chat_id=update.message.chat_id,
message_id=update.message.reply_to_message.message_id,
text="edited")


def main() -> None:
updater = Updater("TOKEN")
updater.dispatcher.add_handler(CommandHandler('start', start))
updater.dispatcher.add_handler(CommandHandler('edit', edit))
updater.start_polling()
updater.idle()


if __name__ == '__main__':
main()

关于python - 如何使用 Telegram 的 python 机器人 API 编辑发送的消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40352927/

51 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com