【问题标题】:How to send message with @ID telegram using telethon library如何使用 Telethon 库发送带有 @ID 电报的消息
【发布时间】:2017-12-05 21:37:59
【问题描述】:

我想用 Telethon 发送消息,但我没有这个电话号码。 我只有@username 电报。 使用此代码,我可以为我的联系电话发送消息:

result = client.invoke(ImportContactsRequest([contact], replace=True))
contacts = client.invoke(GetContactsRequest(""))
for u in result.users:
    client.send_message(u, 'Hi')

但我想向@username Telegram 发送消息

【问题讨论】:

标签: python telegram telethon


【解决方案1】:

您现在可以执行以下操作:

client.send_message('username', 'hello')

旧答案:

它在Project's wiki 上,引用如下。

通过 ResolveUsernameRequest

“实体”用于指代UserChat(包括Channel)。也许最直接的方法是通过resolving their username

from telethon.tl.functions.contacts import ResolveUsernameRequest

result = client.invoke(ResolveUsernameRequest('username'))
found_chats = result.chats
found_users = result.users
# result.peer may be a PeerUser, PeerChat or PeerChannel

有关此结果的更多信息,请参阅Peer

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-08-15
    • 2022-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多