[英]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_ID
和API_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_CHAT
和TARGET_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:)只需调用一次,您将获得对话列表:)
hi guys im struggling with this getting a syntax error?大家好,我正在为这个语法错误而苦苦挣扎? any help would be apricated
任何帮助都会得到帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.