Alireza Zojaji
Alireza Zojaji

Reputation: 837

How to forward a message in Telegram API

There are two methods in Telegram API that forward message:

I want to use forwardMessage method to forward a message from a channel, group or user to another one. Definition of this method is:

messages.forwardMessage#33963bf9 peer:InputPeer id:int random_id:long = Updates;

As you see this method has three input parameters:

As we know the message_id is a unique number in a chat. so a message_id in a group has refers to a message that differs with the same message_id in other group.

So the main question is that how we determine the source peer of forwarding? Because the source peer is not determined by message_id.

P.S: My question is about methods in Telegram API, not Telegram Bot API.

Upvotes: 4

Views: 15497

Answers (1)

apadana
apadana

Reputation: 14620

There seems to an issue with ForwardMessageRequest which doesn't specify the source chat. Obviously message_id is not unique and through my tests I noticed wrong messages will be forwarded by just specifying the message_id. And I noticed message_id is not unique.

But the issue doesn't exist with ForwardMessagesRequest. Following is an example how to use the ForwardMessagesRequest version.

Forwarding Example:

Here is the code I used for testing (I am using Telethon for python, but it won't matter since it's directly calling telegram API):

source_chat = InputPeerChannel(source_chat_id, source_access_hash)
total_count, messages, senders = client.get_message_history(
                    source_chat, limit=10)

for msg in reversed(messages):
    print ("msg:", msg.id, msg)

msg = messages[0]    
print ("msg id:", msg.id)

dest_chat = InputPeerChat(dest_chat_id)    

result = client.invoke(ForwardMessagesRequest(from_peer=source_chat, id=[msg.id], random_id=[generate_random_long()], to_peer=dest_chat))

Upvotes: 2

Related Questions