netdevil
netdevil

Reputation: 319

sending message to contacts in telethon python telegram

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

Upvotes: 3

Views: 9437

Answers (3)

Majid Adibian
Majid Adibian

Reputation: 902

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

Upvotes: 1

Mr Singh
Mr Singh

Reputation: 4230

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)

Upvotes: 1

apadana
apadana

Reputation: 14690

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.

Upvotes: 5

Related Questions