Telegram bot API: get message id to forward it
Asked Answered
C

5

7

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))
Chiao answered 31/10, 2015 at 13:48 Comment(5)
How did you get the message you are trying to forward? (what is it's source?)Mounting
@DeanFenster It's from a group I'm inSignboard
Throught what API? I'm having a hard time understanding how come you don't get the message id with the actual message...Mounting
@DeanFenster I'm using github.com/eternnoir/pyTelegramBotAPISignboard
you've tagged this question wrong. python-telegram-bot refers to another apiDinorahdinosaur
W
3

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!!

Wills answered 19/5, 2022 at 17:21 Comment(2)
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.Sonny
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 itWills
M
2

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

Mounting answered 31/10, 2015 at 15:2 Comment(9)
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.Signboard
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...Signboard
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.Mounting
Oh, so it should be a bigger like random number. Well, try talking to my bot. It will not give you that.Signboard
So it seems to be a running index per chat. But why does that make it invalid?Mounting
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?Signboard
453-465. Let the message_id picking algorithm remain a mystery. Have you tried forwarding a message?Mounting
Yes, and it doesn't output the same IDSignboard
Let us continue this discussion in chat.Mounting
E
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.

Elaterid answered 13/6, 2017 at 14:29 Comment(0)
S
1

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

Soult answered 19/7, 2020 at 10:32 Comment(0)
P
0

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

https://mcmap.net/q/435239/-telegram-bot-how-to-delete-or-remove-a-message-or-media-from-a-channel-or-group

Psych answered 31/5, 2022 at 20:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.