使用Telethon发送电报消息:某些实体参数有效,而其他实体参数无效吗? [英] Sending Telegram messages with Telethon: some entity parameters work, others don't?
问题描述
我正在使用Telethon的 send_message 函数可将消息发送到各种聊天.
I'm using Telethon's send_message function to send messages to various chats.
有时,目的地是另一个用户(只是一个普通的一对一聊天),有时是一个组,有时是一个超级组,有时是一个频道(我是我的管理员).
Sometimes, the destination is another user (just a regular one on one chat), sometimes a group, sometimes a supergroup, and sometimes a channel (of which I'm admin).
如果我理解正确,那么语法应该是:
If I understand correctly, the syntax is supposed to be:
client.send_message(entity,text)
但是我无法弄清楚在不同情况下entity
参数应该是什么.我发现特别令人困惑的是,对于某些组,指定整数ID似乎可以正常工作,但对于另一些组则不能.
But I can't figure out what the entity
parameter is supposed to be in different cases. What I find especially confusing is specifying an integer id seems to work fine for some groups, but not for others.
例如:
我与具有user_id 11111
的人以及与另一个具有user_id 22222
的人进行正常的一对一聊天.
此外,我分为两组(实际上是超组),它们具有channel_id 33333
和44444
.
I have a normal 1-to-1 chat with someone who has user_id 11111
, and also with another person who has user_id 22222
.
Furthermore I'm in two groups (supergroups actually) which have channel_id 33333
and 44444
.
我可以将11111
或33333
指定为实体,并且消息将正确发送(分别发送给第一人或第一组).但是,如果指定22222
或44444
,则会出现错误:
I can specify 11111
or 33333
as entity, and the message gets sent correctly (to the first person or the first group respectively). However if I specify 22222
or 44444
, I'm getting an error:
找不到与"{}"对应的任何实体..format(string)
ValueError:找不到与"22222"相对应的任何实体
Cannot find any entity corresponding to "{}"'.format(string)
ValueError: Cannot find any entity corresponding to "22222"
我也正在使用同一Telethon实例从所有4个聊天中接收消息,并且一切正常.
I am also receiving mesasges from all 4 chats using this same Telethon instance, and that's working all fine.
所以我的问题是:如何获取send_message()的正确实体数据?
推荐答案
I suggest reading this section of the document (entities)
例如,我想将消息发送给用户名为alix
for example, I want to send the message to a user with the username: alix
client = TelegramClient('session_name',
api_id,
api_hash,
)
client.start()
destination_user_username='alix'
entity=client.get_entity(destination_user_username)
client.send_message(entity=entity,message="Hi")
或者我要将邮件发送到用户名为test_ali3
or I want to send the message to a channel with username: test_ali3
client = TelegramClient('session_name',
api_id,
api_hash
)
client.start()
destination_channel_username='test_ali3'
entity=client.get_entity(destination_channel_username)
client.send_message(entity=entity,message="Hi")
或者我想通过 invite_link 将邮件发送到网上论坛:https://t.me/joinchat/Bn4WIhMF1T_ZAF-yM6WbHw
or I want to send the message to a group with invite_link: https://t.me/joinchat/Bn4WIhMF1T_ZAF-yM6WbHw
client = TelegramClient('session_name',
api_id,
api_hash
)
client.start()
destination_group_invite_link='https://t.me/joinchat/Bn4WIhMF1T_ZAF-yM6WbHw'
entity=client.get_entity(destination_group_invite_link)
client.send_message(entity=entity,message="Hi")
希望对您有用.
这篇关于使用Telethon发送电报消息:某些实体参数有效,而其他实体参数无效吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!