How to forward a message in Telegram API

11,779

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))
Share:
11,779
Alireza Zojaji
Author by

Alireza Zojaji

I started studying Electronics engineering in Sharif university at 1985, but one years later in 1986 I faced ZX-Spectrum computers in the high-school that I was teaching sometimes. That was a new world opened in front of me. I learned its Basic language and started to developing programs for it. Dooz game (6x6 tic-tac-toevariant) on ZX-Spectrum was the first game I produced that made me so pleasant. In 1988 I knew IBM Computer PC-XT with 20 MB HDD at the same high-school and faced MS-DOS and GW-Basic. later I started developing Database applications by GW-Basic! For example I produced a students' educational information system for the same school. In 1991 I confronted with FoxPro database system. It was very amazing for me, so started to working with it and producing database systems on it. later I developed a Farsi language tools for FoxPro. some years later when Our team was designing a new tools for FoxPro that made "Data Entry Forms" and "Report Forms" automatically and we could change it visually, we understood that "FoxPro 2.6" is released simultaneously and has all these features inside it exactly same as we designed! In 1992 I faced with "Borland Turbo Pacal". A new world of structural programming, and later "Borland Pascal". In 1995 I used "Borland C++" for developing some low level applications, but yet I had not found the language I needed. In 1997 I started windows programming with "Borland Delphi 3.0" a new way for event programming and visual develping. Delphi also made me integrate database programming of FoxPro and Structural programming of Turbo Pascal. I found my lost in Delphi! I grew with Delphi until Borland was smashed! But I was so faithful that continue developing by "Delphi 7" even when it was dead! I have used Delphi for desktop applications until now. In 1998 I started web programming with Delphi, but it was too heavy to be used as web programming tool. In 1999 I learnt "PHP 4" language and my lost ring of web programming. When tried to use mySQL as a database twin of PHP, I found that mySQL is very poor. I needed a database that I can use a query as substitute of column names and table names in every position of another query. So I used Microsoft SQL Server as database beside PHP. later untill now I am using PHP for web programming beside SQL Server as database. I have developed complete tools for making PHP forms and reports. In 2016 I found that Telegram application is replacing web media gradually at least in Iran. so I started using Telegram bot for some applications in PHP language. In 2017, because of some limitations in Telegram bot methods I started Telegram API, but its documents is very poor. so I used TLSahrp library for C#. So I learnt C# for developing TLSharp and developing applications with TSharp. In 2018 I started developing smart contracts on different blockchain systems.

Updated on June 14, 2022

Comments

  • Alireza Zojaji
    Alireza Zojaji almost 2 years

    There are 2 methods in Telegram API that forward message:

    • messages.forwardMessage
    • messages.forwardMessages

    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 3 input parameters:

    • peer that represents the channel, group or user that we forward message to. (Destination)
    • id that is message_id.
    • random_id that has internal use.

    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.