How to forward a message in Telegram API

匿名 (未验证) 提交于 2019-12-03 01:01:02

问题:

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.

回答1:

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


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!