如何在Telegram API中转发消息

编程入门 行业动态 更新时间:2024-10-21 03:55:08
本文介绍了如何在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 the channel, group or user that we forward message to. (Destination)
  • id that is message_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中转发消息

本文发布于:2023-11-25 22:50:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1631660.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:发消息   如何在   Telegram   API

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!