如何使用Telethon库通过@ID电报发送消息 [英] How to send message with @ID telegram using telethon library

查看:902
本文介绍了如何使用Telethon库通过@ID电报发送消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want send message with telethon but i dont have phone number this . i have only @username Telegram. with this code i can send message for my contact phone :

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

但是我想向@username电报发送消息

But i want send message to @username Telegram

推荐答案

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

You can just do the following now:

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


旧答案:


Old answer:

它在项目的Wiki 中,在下面引用.

实体"用于指 User Chat (其中包括解析其用户名:

An "entity" is used to refer to either an User or a Chat (which includes a Channel). Perhaps the most straightforward way to get these is by 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 .

See Peer for more information about this result.

这篇关于如何使用Telethon库通过@ID电报发送消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆