我如何使用 telethon API python 在我的频道中添加我的联系人

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

这是我频道的信息:

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

我有 3 个联系人:

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

我不知道如何使用此代码:

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

什么是 chat_id?和 user_to_add ?

当我使用此代码时

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

我看到这个错误

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'

正如我在the issue上所说的,我想你也打开了,你需要使用contact.users,而不是contacts.contacts。如果要添加第一个用户,先取回,然后在请求上使用:

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

一如既往,the documentation is your friendAddChatUserRequest 的文档明确指出 user_idInputUser 类型(但您可以将 User)。

按此方式应用,当您在群组中添加目标合约或id或用户名时...

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