netdevil
netdevil

Reputation: 319

How i can add my contact in my channel with telethon API python

this is info my channel :

dialogs, entities = client.get_dialogs(1)
entity = entities[0]
print(entity)
(channel (ID: 0xa14dca52) = (creator=True, kicked=None, left=None, editor=None, moderator=None, broadcast=True, verified=None, megagroup=None, restricted=None, democracy=None, signatures=None, min=None, id=1135498252, access_hash=-6282984409346664480, title=channel_test, username=None, photo=(chatPhotoEmpty (ID: 0x37c1011c) = ()), date=2017-07-04 06:11:05, version=0, restriction_reason=None))

and i have 3 contacts :

contacts = client.invoke(GetContactsRequest(""))
for u in contacts.contacts:
     print (u)
(contact (ID: 0xf911c994) = (user_id=231735496, mutual=False))
(contact (ID: 0xf911c994) = (user_id=408708469, mutual=False))
(contact (ID: 0xf911c994) = (user_id=442246143, mutual=False))

I dont know how i can use this code:

from telethon.tl.functions.messages import AddChatUserRequest

client.invoke(AddChatUserRequest(
   chat_id,
   user_to_add,
   fwd_limit=10  # allow the user to see the 10 last messages
))

what is chat_id ? and user_to_add ?

when i use this code

client.invoke(AddChatUserRequest(
   1135498252,
   231735496,
   fwd_limit=10  # allow the user to see the 10 last messages
))

i see this error

Traceback (most recent call last):
 File "<pyshell#102>", line 4, in <module>
   fwd_limit=10  # allow the user to see the 10 last messages
  File "C:\Python34\lib\site-packages\telethon\telegram_client.py", line 247, in invoke
   request, timeout=timeout, updates=updates)
   File "C:\Python34\lib\site-packages\telethon\telegram_bare_client.py",    line 188, in invoke
    self.sender.send(request)
  File "C:\Python34\lib\site-packages\telethon\network\mtproto_sender.py",    line 57, in send
   request.on_send(writer)
  File "C:\Python34\lib\site-packages\telethon\tl\functions\messages    \add_chat_user.py", line 39, in on_send
   self.user_id.on_send(writer)
    AttributeError: 'int' object has no attribute 'on_send'

Upvotes: 6

Views: 3947

Answers (2)

Jahirul islam
Jahirul islam

Reputation: 536

Apply this way, when you add a target contract or id or username in a group...

# Here get user data any way...work same
user = await client.get_entity('username') #using target username
user = await client.get_entity('+34xxxxxxxxx') #using target mobile number
user = await client.get_entity(target_id) #using target id
 # For instance
await awaitclient(AddChatUserRequest(
    chat_id=-1135498252,
    user_id=user,  
    fwd_limit=10 
))

Upvotes: 0

Lonami
Lonami

Reputation: 7066

As I said on the issue I presume you opened too, you need to use the contact.users, not contacts.contacts. If you want to add the first user, first retrieve it, and then use it on the request:

contacts = client(GetContactsRequest(''))
user = contacts.users[0]  # For instance
client(AddChatUserRequest(
    chad_id=1135498252,
    user_id=user,  # Yes, the name is misleading
    fwd_limit=10
))

As always, the documentation is your friend, and the documentation for AddChatUserRequest clearly says that user_id is of type InputUser (but you can pass it an User too).

Upvotes: 3

Related Questions