问题描述
Telegram API 中有两种转发消息的方法:
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 for 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 中转发消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!