how to get chat_id and message_id in telebot(pytelegramBotAPI) to update last sent message in telegram bot(Python)

15,633

I think you can get it with:

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

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

You should create a keyboard:

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

Create a command:

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

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

Create Callback:

@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
Share:
15,633
Jahongirmirzo
Author by

Jahongirmirzo

Updated on June 11, 2022

Comments

  • Jahongirmirzo
    Jahongirmirzo almost 2 years

    Here is my piece of code

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