最近我编写了代码,应该将某个用户的每条消息转发到我加入的所有组,但事实并非如此。 这是我的代码:
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 中的任何实体发送消息,您需要两条信息:
- 实体的常量唯一ID(它是一个整数。它不是用户名字符串)
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/