回答我自己的问题以防有人需要。
正如@CallMeStag 所指出的,需要一个支持“用户机器人”的库。这些是直接实现 MTProto 的库。
对于 python,例如Pyrogram 非常适合且非常易于使用。
首先,需要一个 API 密钥和 API 哈希来识别 Telegram 服务器上的 Python 脚本,以便在 MTProto 中进行通信。
https://my.telegram.org/auth?to=apps -> 使用您的凭据登录并创建一个“应用程序”。将它们定义为下面的API_ID 和API_HASH。
现在,我使用此代码将消息从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()
为了找出源和目标的CHAT_ID,我暂时禁用了过滤器,并打印了消息。
@app.on_message()
def my_handler(client, message):
print(message)
这样做,您可以:每当收到特定群组中的消息时,您都可以找到message.chat.id(注意:负值!)。在上面的完整脚本中为SOURCE_CHAT 和TARGET_CHAT 配置它们。
编辑:
无需某人先在频道/群组/私人/聊天中发送消息即可获取所有对话的所有聊天 ID 的另一种选择:
def getAllChatIDs():
for x in app.get_dialogs():
print (x.chat.type, x.chat.title, x.chat.id)
只需调用一次,你就会得到一个对话列表:)