如何在Telegram API中转发消息 [英] How to forward a message in Telegram API
问题描述
Telegram API中有2种方法可以转发消息:
There are 2 methods in Telegram API that forward message:
- messages.forwardMessage
- messages.forwardMessages
我想使用forwardMessage
方法将消息从channel
,group
或user
转发到另一个.该方法的定义是:
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;
如您所见,此方法有3个输入参数:
As you see this method has 3 input parameters:
-
peer
代表将消息转发到的channel
,group
或user
. (目的地) -
id
即message_id
.
有内部用途的 -
random_id
.
peer
that represents thechannel
,group
oruser
that we forward message to. (Destination)id
that ismessage_id
.random_id
that has internal use.
我们知道message_id
是聊天中的唯一号码.因此group
中的message_id
所引用的消息与其他组中的同一message_id
不同.
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.
因此,主要问题是我们如何确定转发的来源peer
?因为源peer
不是由message_id
确定的.
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:我的问题是关于Telegram API的方法,而不是Telegram Bot API.
P.S: My question is about methods in Telegram API, not Telegram Bot API.
推荐答案
ForwardMessageRequest似乎有一个问题,该问题未指定源聊天.显然message_id不是唯一的,通过我的测试,我注意到仅指定message_id将转发错误的消息.而且我注意到message_id不是唯一的.
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.
但是ForwardMessagesRequest不存在此问题.以下是如何使用ForwardMessagesRequest版本的示例.
But the issue doesn't exist with ForwardMessagesRequest. Following is an example how to use the ForwardMessagesRequest version.
转发示例:
这是我用于测试的代码(我将Telethon用于python,但这没关系,因为它直接调用电报API):
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))
这篇关于如何在Telegram API中转发消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!