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

2

6

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'
Wisp answered 4/7, 2017 at 13:59 Comment(1)
chat_id is the id of channel(1135498252) and user_to_add is user's ID(231735496)Wingover
M
3

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

Mindi answered 8/7, 2017 at 8:53 Comment(2)
is not work and have eroore result telethon.errors.rpc_errors_400.UserIdInvalidError: (UserIdInvalidError(...), 'Invalid object ID for an user. Make sure to pass the right types.')Wisp
Because you haven't upgraded Telethon and therefore you still need to use the old style get_input_peer call. If you RTFM you'd notice it takes an InputPeer and not an User; but the latest versions allow you to use an User because it's more comfortable.Mindi
H
0

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 
))
Harvison answered 25/1, 2021 at 8:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.