【问题标题】:forward message to super group with telethon使用 Telethon 将消息转发到超级组
【发布时间】:2018-11-16 22:27:39
【问题描述】:

最近我编写的代码应该将来自某个用户的每条消息转发到我加入的所有组,但它没有。 这是我的代码:

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

【问题讨论】:

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


    【解决方案1】:

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

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

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

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

    dialogs, entities = client.get_dialogs()
    

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

    【讨论】:

    • 我试过你的代码,但它也不起作用。这是我的代码,正如您所见,您编写的代码行不起作用。 link 并且没有尝试,除非我收到此错误:“dialogs,entities = client.get_dialogs() ValueError: too many values to unpack (expected 2)”
    • 我现在发现了我的问题,在我的完整代码中,我在我的范围内使用了 try 除了 2 次,当我删除时,我的代码工作正常。
    猜你喜欢
    • 2023-02-26
    • 1970-01-01
    • 1970-01-01
    • 2020-04-24
    • 1970-01-01
    • 2022-08-02
    • 1970-01-01
    • 1970-01-01
    • 2022-11-24
    相关资源
    最近更新 更多