How to send message with @ID telegram using telethon library

跟風遠走 提交于 2019-12-11 05:22:26

问题


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')

But i want send message to @username Telegram


回答1:


You can just do the following now:

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

Old answer:

It's on the Project's wiki, quoted below.

Via ResolveUsernameRequest

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

See Peer for more information about this result.



来源:https://stackoverflow.com/questions/44871661/how-to-send-message-with-id-telegram-using-telethon-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!