python - 用电视节目将消息转发给 super 组

标签 python python-3.x telegram python-telegram-bot telethon

最近我编写了代码,应该将某个用户的每条消息转发到我加入的所有组,但事实并非如此。 这是我的代码:

        for message in client.iter_messages('aliakhtari78'):
        try:
            dialogs = client.get_dialogs()
            for dialog in dialogs:
                id_chat = dialog.message.to_id.channel_id
                entity = client.get_entity(id_chat)
                client.forward_messages(
                    entity,  # to which entity you are forwarding the messages
                    message.id,  # the IDs of the messages (or message) to forward
                    'somebody'  # who sent the messages?
                )

        except:
            pass

在这段代码中,我首先获取 'aliakhtari78' 发送给我的每条消息,然后获取我加入的群组的实体,最后它应该将消息转发给所有群组,但是它没有,我检查我的代码并用用户实体替换实体并且它有效并且我知道问题是因为实体,但我无法找出我的问题是什么。 另外,很抱歉我的问题写错了。

最佳答案

为了向 Telegram 中的任何实体发送消息,您需要两条信息:

  1. 实体的常量唯一ID(它是一个整数。它不是用户名字符串)
  2. access_hash 对于每个实体的每个用户都是不同的

您只能将 @username 传递给 client.get_entity,Telethon 会自动将 @username 解析为具有 id 的实体access_hash。这就是为什么当您这样更改代码时它会起作用的原因。但是,在您的代码中,您已将 channel_id(实体的常量唯一 ID)传递给 client.get_entity,而不是 username

请注意,client.get_dialogs 返回 entities 以及 dialogs。你刚刚忽略了实体!这是获取所有实体数组的方法:

dialogs, entities = client.get_dialogs()

然后简单地将相应的实体从entities数组传递给client.forward_messages

关于python - 用电视节目将消息转发给 super 组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53346239/

相关文章:

python - 数组和向量的 numpy 逐元素乘法

python - 如何检测图像之间的偏移

python - Pandas:将函数应用于每个组并将结果存储在新列中

Python-值错误: I/O operation on closed file - traceroute script + printing

python-3.x - 如何在 Visual Studio Code 中使用 activate.bat 而不是 ps1 运行选定文本?

python - 具有挑战性的特殊 numpy 运算

python - 如何使用tor获取来自telegram api的请求?

Python3 - 为什么这段代码出现在索引之外?

api - 我可以使用 Telegram bot api 检测我的机器人组吗

php - 我的 Telegram 查询出了什么问题?