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

时间:2017-07-04 13:59:13

标签: python api telegram telethon

这是我的频道信息:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{

    let cell : cellVC = collectionView.dequeueReusableCell(withReuseIdentifier: "cellVC", for: indexPath) as! cellVC
    cell.sampleLabel. = "sample text"
    cell.buttonClicked.tag = indexPath.row
    cell.buttonClicked.addTarget(self, action: #selector(masterAction(sender:)), for: .touchUpInside)

    cell.buttonClicked.backgroundColor = UIColor.cyan
    return cell
}


func masterAction(_ sender: UIButton) {
    print ("button click")
}

我有3个联系人:

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

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

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

什么是chat_id?和user_to_add?

当我使用此代码时

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

我看到了这个错误

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

2 个答案:

答案 0 :(得分:2)

正如我在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_id的类型为InputUser(但您也可以将User传递给final String text = (String) ((TextView)view).getText(); )。

答案 1 :(得分: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 
))