如何在 Telebot(pytelegramBotAPI)中获取 chat_id 和 message_id 以更新电报机器人(Python)中最后发送的消息 [英] how to get chat_id and message_id in telebot(pytelegramBotAPI) to update last sent message in telegram bot(Python)

查看:114
本文介绍了如何在 Telebot(pytelegramBotAPI)中获取 chat_id 和 message_id 以更新电报机器人(Python)中最后发送的消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的一段代码

bot.edit_message_text(chat_id = CHAT_ID, message_id = MESSAGE_ID, text = "message has been updated", reply_markup=inline_keyboard)

推荐答案

我认为您可以通过以下方式获得:

I think you can get it with:

lastMessageId = message[-1].message_id
lastChatId = message[-1].chat.id

我不知道您为什么需要它,但我发送示例让您了解如何使用消息 ID 和用户 ID.

I don´t know why you need it, but I send example for you to understand how use message id and user id.

您应该创建一个键盘:

keyboard = types.InlineKeyboardMarkup()
keyboard.add(types.InlineKeyboardButton('Yes', callback_data='yes'),
             types.InlineKeyboardButton('No', callback_data='no'))

创建命令:

@bot.message_handler(commands=['like'])
def like(message):

  cid = message.chat.id
  bot.send_message(cid, "Do you like it?", reply_markup=keyboard)

创建回调:

@bot.callback_query_handler(func=lambda call: call.data in ['yes', 'no'])
def callback_handler(call):

    cid = call.message.chat.id
    mid = call.message.message_id
    answer = call.data
    update_lang(cid, answer)
    try:
        bot.edit_message_text("You voted: " + answer, cid, mid, reply_markup=keyboard)
    except:
        pass

这篇关于如何在 Telebot(pytelegramBotAPI)中获取 chat_id 和 message_id 以更新电报机器人(Python)中最后发送的消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆