6

So, I need my bot to forward a message of a chat. But in order to do so, I need to get the id of the message I want to forward (it's an old message). How can I get the id of that message so I can send it?

This is the code I'm using

@bot.message_handler(func=lambda m: True)
def reply_ids(message):
    cid = message.chat.id
    bot.reply_to(message, "The message id is: " + str(message.message_id) + " This chat ID is: " + str(cid))
José María
  • 2,835
  • 5
  • 27
  • 42

5 Answers5

2

When receiving a message, the id will be in message.message_id, as documented here.

Dean Fenster
  • 2,345
  • 1
  • 18
  • 27
  • The thing is, it's the message id from what looks like the bot conversation. It's difficult to explain. But you can take a look, try talking with my bot: @jmml_test_bot It will give the message ID of your message and the ID of the chat. One would expect the message ID to be 1 (or 0) if it's the first message in the conversation. However, it will be a bigger number, other numbers are older messages that bot has received. – José María Oct 31 '15 at 15:22
  • Then, trying to get the ID of a message in a group chat will do the same, it won't show me the correct ID... – José María Oct 31 '15 at 15:23
  • Why would the ID be 0 or 1? Telegram stores all messages on it's servers and should have a unique id for each and every one of them. – Dean Fenster Oct 31 '15 at 15:24
  • Oh, so it should be a bigger like random number. Well, try talking to my bot. It will not give you that. – José María Oct 31 '15 at 15:28
  • So it seems to be a running index per chat. But why does that make it invalid? – Dean Fenster Oct 31 '15 at 15:33
  • Because it's not per chat. Try talking to it with another account or adding it to a group... It will give the next number for each message... What number did you get? – José María Oct 31 '15 at 15:34
  • 453-465. Let the message_id picking algorithm remain a mystery. Have you tried forwarding a message? – Dean Fenster Oct 31 '15 at 15:37
  • Yes, and it doesn't output the same ID – José María Oct 31 '15 at 15:46
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/93878/discussion-between-dean-fenster-and-jose-maria). – Dean Fenster Oct 31 '15 at 15:51
2

If it is a supergroup or a channel, you can get the message_id by clicking on the message (in telegram web ) then choosing copy message link. the link will be in this form "https://t.me/channel_name/message_id"

This solution is to find the message_id manually!!

  • Looks like you're giving an answer on how to get a message ID manually (clicking through the UI) but the question is about getting the ID programmatically via the API. – OllyTheNinja May 20 '22 at 05:51
  • yes, my bad!!! I was searching for the method to find the id manually, in my search I found this question. After I found the solution I liked to share it – Said Assassi May 20 '22 at 22:04
1

Recently I've been working with callback queries from inline buttons. One things I noticed is that in order to reply to the exact message that had the buttons Telegram needs to know both message.chat_id and message.message_id. You can try with both. This is more a comment then an answer but I don't have enough reputation to comment.

fedeb
  • 122
  • 1
  • 4
1

UPDATE: Now, It's update.message.message_id

edoardottt
  • 100
  • 1
  • 11
0

Using python, if you have a CommandHandler() you can read the chat_id and message_id like so:

https://stackoverflow.com/a/72433953/1000741

Francois
  • 10,465
  • 4
  • 53
  • 64