5

这是我的频道的信息:

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'
4

2 回答 2

3

正如我在我认为您也打开过的问题上所说的那样,您需要使用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
))

与往常一样,文档是您的朋友,并且文档AddChatUserRequest清楚地表明这user_id是类型InputUser(但您也可以传递它User)。

于 2017-07-08T08:53:54.813 回答
0

应用这种方式,当您在组中添加目标合同或 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 
))
于 2021-01-25T08:27:58.253 回答