I
I
Irina2018-08-25 02:22:58
Python
Irina, 2018-08-25 02:22:58

How does bot.edit_message_text work in python telegram bot?

There is a handler that calls a function and there is a function that performs a series of actions and sends a message with an inline keyboard.

def make_b(message, namber_bort):
    key = telebot.types.InlineKeyboardMarkup()
    but = telebot.types.InlineKeyboardButton(text="бла-бла", callback_data=5)
    key.add(but)
        
    bot.edit_message_text(message.chat_id, text="тру-ту-ту", reply_markup=key )


@bot.message_handler(content_types=['text'])
def handle_text(message):
   if message.text==5
       make_b(message, namber_bort)

If I send a message as indicated below, then there is no problem.
bot.send_message(message.chat.id, text="тру-ту-ту", reply_markup=key)

And if through bot.edit_message_text, then the error is:

AttributeError: 'Message' object has no attribute 'chat_id'

And I need to change the message. Tell me what's wrong?
message.chat_id was set via message.chat.id. The error is like this:
(__init__.py:418 MainThread) ERROR - TeleBot: "A request to the Telegram API was unsuccessful. The server returned HTTP 400 Bad Request. Response body:
[b'{"ok":false,"error_code":400," description":"Bad Request: chat not found"}']"

And via message.chat.get('id'):
AttributeError: 'Chat' object has no attribute 'get'

And my fantasy is over.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
Irina, 2018-08-26
@vera_ira

I found a solution:
bot.edit_message_text only works to change an existing message, so it is logical that it should be placed only in @bot.callback_query_handler.

A
AlmazKayum, 2018-08-25
@AlmazKayum

In the edit_message_text method, you must specify the chat id chat_id and message id message_id .
Try like this:

bot.edit_message_text(chat_id=message.chat.id, message_id=message.message_id, text="тру-ту-ту", reply_markup=key )

H
hey_umbrella, 2021-10-07
@hey_umbrella

bot.edit_message_text does not have to be placed only in @bot.callback_query_handler, you can write the message to a variable, and then take the message_id from it and edit the message.

messagetoedit = bot.send_message(message.chat.id, 'Текст')
bot.edit_message_text(chat_id=message.chat.id, message_id=messagetoedit.message_id, text=f"Новый текст")

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question