Telegram Bot:转发来自私人组的消息 [英] Telegram Bot: Forwarding Messages from Private Group

查看:35
本文介绍了Telegram Bot:转发来自私人组的消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以使用 Python/JS 转发我作为成员在私人只读组中收到的消息?我正在尝试使用 python-telegram-bot 进行设置,但似乎我必须将机器人添加到组中才能与组中发送的内容进行交互.但我不能因为我只是一个阅读/接收成员...

Is there any way using Python / JS to forward messages which I, as a member, do receive in a private read-only group? 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. But I can't as Im just a reading / receiving member...

有没有办法不使用 Telegram API,而是使用某种 JS 浏览器自动化来转发那些?这是我唯一想到的事情......提前致谢!

Is there maybe a way without using the Telegram API, but using some sort of JS Browser automation to forward those? This is the only thing which comes to my mind... Thanks in advance!

推荐答案

回答我自己的问题,以防有人需要.

Answering my own question in case someone needs it.

正如@CallMeStag 所指出的,人们需要一个支持用户机器人"的库.这些是直接实现 MTProto 的库.

As @CallMeStag pointed out, one needs a library which support "User bots". These are librarys directly implementing the MTProto.

对于python,例如Pyrogram 是合适的并且非常容易使用.

For python, e.g. Pyrogram is suitable and very easy to use.

首先,需要一个 API 密钥和 API 哈希来识别 Telegram 服务器上的 Python 脚本,以便在 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->使用您的凭据登录并创建一个应用程序".将它们定义为下面的 API_IDAPI_HASH.

https://my.telegram.org/auth?to=apps -> Login using your credentials and create an "App". Define those into API_ID and API_HASH below.

现在,我使用此代码将消息从 SOURCE_CHAT 复制到 TARGET_chat:

Now, I use this code to copy messages from the SOURCE_CHAT to the 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()

为了找出源和目标的CHAT_ID,我暂时禁用了过滤器,并打印了消息.

To find out the CHAT_ID of Source and Target, I temporarly disabled the Filter, and printed the message.

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

这样做,使您能够:无论何时在特定组中收到消息,您都可以找到message.chat.id(注意:负值!).在上面的完整脚本中为 SOURCE_CHATTARGET_CHAT 配置这些.

Doing so, enables you to: whenever receiving a message in the specific group, you can find message.chat.id (attention: negative Values!). Configure those for SOURCE_CHAT and TARGET_CHAT in the full script above.

无需先有人在频道/群组/私人/聊天中发送消息即可获取所有对话的所有聊天 ID 的另一种选择:

Another option to get all chat IDs for all dialogues without first needing someone to send a message in the channel/group/private/chat:

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

这篇关于Telegram Bot:转发来自私人组的消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆