简体   繁体   English

Telegram Bot:从私人群组转发消息

[英]Telegram Bot: Forwarding Messages from Private Group

Is there any way using Python / JS to forward messages which I, as a member, do receive in a private read-only group?有没有办法使用 Python / JS 来转发我作为成员在私人只读组中收到的消息? I'm trying to set it up using python-telegram-bot but it seems I gotta add the bot to the group to have it interact with the contents sent in the group.我正在尝试使用 python-telegram-bot 进行设置,但似乎我必须将机器人添加到组中,以使其与组中发送的内容进行交互。 But I can't as Im just a reading / receiving member...但我不能因为我只是一个阅读/接收成员......

Is there maybe a way without using the Telegram API, but using some sort of JS Browser automation to forward those?有没有办法不使用电报 API,而是使用某种 JS 浏览器自动化来转发这些? This is the only thing which comes to my mind... Thanks in advance!这是我唯一想到的……提前谢谢!

Answering my own question in case someone needs it.回答我自己的问题以防有人需要。

As @CallMeStag pointed out, one needs a library which support "User bots".正如@CallMeStag 指出的那样,需要一个支持“用户机器人”的库。 These are librarys directly implementing the MTProto.这些是直接实现 MTProto 的库。

For python, eg Pyrogram is suitable and very easy to use.对于 python,例如 Pyrogram 是合适的并且非常易于使用。

First of all, one needs an API key and API hash to identify the Python Script on the Telegram server to communicate in the MTProto. First of all, one needs an API key and API hash to identify the Python Script on the Telegram server to communicate in the MTProto.

https://my.telegram.org/auth?to=apps -> Login using your credentials and create an "App". https://my.telegram.org/auth?to=apps -> 使用您的凭据登录并创建一个“应用程序”。 Define those into API_ID and API_HASH below.将它们定义为下面的API_IDAPI_HASH

Now, I use this code to copy messages from the SOURCE_CHAT to the TARGET_chat :现在,我使用此代码将消息从SOURCE_CHAT复制到TARGET_chat

#!/usr/bin/env python3
from pyrogram import Client
from pyrogram import filters

# ~~~~~~ CONFIG ~~~~~~~~ #
ACCOUNT = "@xy"
PHONE_NR = '+49....'

# https://my.telegram.org/auth?to=apps
API_ID = 1111111 
API_HASH = "your_hash"

# CHAT ID
SOURCE_CHAT = -11111 
TARGET_CHAT = -22222
# ~~~~~~~~~~~~~~~~~~~~~~ #

app = Client(
    ACCOUNT,
    phone_number=PHONE_NR,
    api_id=API_ID,
    api_hash=API_HASH
)

# filters.chat(SOURCE_CHAT)
@app.on_message(filters.chat(SOURCE_CHAT))
def my_handler(client, message):
    message.copy(  # copy() so there's no "forwarded from" header
        chat_id=TARGET_CHAT,  # the channel you want to post to
        caption="Copied from XYZ"  # Caption
    )

app.run()

To find out the CHAT_ID of Source and Target, I temporarly disabled the Filter, and printed the message.为了找出源和目标的CHAT_ID ,我暂时禁用了过滤器,并打印了消息。

@app.on_message()
def my_handler(client, message):
    print(message)

Doing so, enables you to: whenever receiving a message in the specific group, you can find message.chat.id (attention: negative Values.).这样做,可以让你:每当收到特定群组的消息时,你都可以找到message.chat.id (注意:负值。)。 Configure those for SOURCE_CHAT and TARGET_CHAT in the full script above.在上面的完整脚本中为SOURCE_CHATTARGET_CHAT配置这些。

EDIT: Another option to get all chat IDs for all dialogues without first needing someone to send a message in the channel/group/private/chat:编辑:另一种选择获取所有对话的所有聊天 ID,而无需首先需要某人在频道/组/私人/聊天中发送消息:

def getAllChatIDs():
    for x in app.get_dialogs():
        print (x.chat.type, x.chat.title, x.chat.id)

Simply call it once and you'll get a list of dialogues:)只需调用一次,您将获得对话列表:)

It's indeed not possible with Telegram Bots - you'd have to add them to the group. Telegram Bots 确实不可能 - 你必须将它们添加到组中。 You can however automate your personal account using so called "user bots".但是,您可以使用所谓的“用户机器人”自动化您的个人帐户。 Here is an article about them. 是一篇关于它们的文章。

hi guys im struggling with this getting a syntax error?大家好,我正在为这个语法错误而苦苦挣扎? any help would be apricated任何帮助都会得到帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Telegram bot(telegraf.js 库)将消息从一个通道转发到另一个通道方法? - Telegram bot ( telegraf.js library ) forwarding messages from one to another channel method? js电报机器人。 如何收听来自我的私人频道的消息并重定向到我的频道 - js telegram bot. How to listen messages from my private channels and redirect to my channel 如何正确实现从机器人到电报频道的消息转发? - How to properly implement the forwarding of a message from a bot to a telegram channel? 删除@ids 和文本包含来自电报机器人消息的链接 - delete @ids and text contain link from messages of telegram bot 从群聊回复(电报机器人 - 支持)(NODE.JS) - reply from group chat (telegram bot - support) (NODE JS) node-telegram-bot-api 中的消息延迟 - Delay for messages in node-telegram-bot-api 如何使用 npm 库 Telegraf 从数据库中的 id 列表一次向所有电报机器人用户发送消息 - How to send messages to all telegram bot users from a list of ids in a database at once using npm library Telegraf 如何获得电报Bot超级群组ID? - How to get Telegram Bot Super Group ID? 如何使 Telegram bot 内联键盘私有? 节点JS - How to make Telegram bot inline keyboard private? Node.JS 有没有办法在没有机器人的情况下在 node.js 中接收电报消息? - Is there a way to receive telegram messages in node.js without bot?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM