1

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

Jongware
  • 22,200
  • 8
  • 54
  • 100
netdevil
  • 309
  • 1
  • 7
  • 18
  • Possible duplicate of [How can I send a message to someone with my telegram bot using their Username](https://stackoverflow.com/questions/41664810/how-can-i-send-a-message-to-someone-with-my-telegram-bot-using-their-username) – brandonscript Jul 02 '17 at 14:12
  • is no duplicate becuase i want use the telethon python api – netdevil Jul 02 '17 at 14:19
  • You might want to ask on the github page, as I looked through their docs and nothing stood out to me – Parker Jul 02 '17 at 15:00

1 Answers1

3

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.

Lonami
  • 5,945
  • 2
  • 20
  • 38