3

How can I see all my contacts and send them messages? i use Telethon (API telegram python).

from telethon.tl.functions.contacts import ResolveUsernameRequest
from telethon.tl.types import InputChannelEmpty
from telethon import TelegramClient
from telethon.tl.types.messages import Messages
from telethon.tl.types.contacts import Contacts
api_id = 1****
api_hash = '5fbd2d************************'
client = TelegramClient('arta0', api_id, api_hash)
client.connect()
Sam Hartman
  • 6,210
  • 3
  • 23
  • 40
netdevil
  • 309
  • 1
  • 7
  • 18

3 Answers3

5

Just add this line to your code:

contacts = client.invoke(GetContactsRequest(""))
print(contacts)

And you should see the contacts in the result.

To send messages to contacts, you can use the send_message function defined in telegram_client.py and has an example in InteractiveTelegramClient.py.

for u in contacts.users: 
    client.send_message(InputPeerUser(u.id, u.access_hash), "hi")

If you need more details comment below and I will try to reply.

apadana
  • 13,456
  • 15
  • 82
  • 98
1

Sending empty string didn't worked for me:

contacts = client.invoke(GetContactsRequest(""))
*** struct.error: required argument is not an integer

So I think you should use '0' instead:

contacts = client.invoke(GetContactsRequest(0))
print(contacts)
Mr Singh
  • 3,936
  • 5
  • 41
  • 60
1

I think in new update client.invoke() is not defined. I used following code and it worked for me.

from telethon.tl.functions.contacts import GetContactsRequest
contacts = client(GetContactsRequest(0))
Majid A
  • 802
  • 9
  • 19