skip to Main Content

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

3

Answers


  1. 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.

    Login or Signup to reply.
  2. 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)
    
    Login or Signup to reply.
  3. 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))
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search