【问题标题】:telethon library: how to add user by phone numberTelethon 库:如何通过电话号码添加用户
【发布时间】:2020-07-29 19:56:46
【问题描述】:

我正在研究 Telegram 的 Telethon 库,它可以使用 Telegram API 充当 Telegram 客户端(重要提示:这是 Telegram client API,而不是 Bot API)。

我需要的功能是创建一个群聊并在那里邀请用户。 当我添加联系人列表中的某个人时,这很好用:

import telethon
from telethon.tl.functions.messages import CreateChatRequest
client = telethon.TelegramClient('some_session', 'key', '6284f5acf91b03somehash441ac9eef319')
client.start()
client(CreateChatRequest(['+79297226653'], 'Test Group')) # number from my contact list

但是,如果我传递的号码不在我的联系人列表中(我确定此电话号码已在 Telegram 中注册),则会中断

  File "/Users/1111/.virtualenvs/inviter-WB5rPISo/lib/python3.6/site-packages/telethon/telegram_client.py", line 1680, in _get_entity_from_string
    'Cannot turn "{}" into any entity (user or chat)'.format(string)
TypeError: Cannot turn "+79291101517" into any entity (user or chat)

我怀疑CreateChatRequest 只适用于我的PeerUsers,即这种方法禁止使用非对等电话。

所以问题是,如果某人不是我的联系人之一,我如何将他添加到群聊中?

【问题讨论】:

    标签: python telegram telethon


    【解决方案1】:

    根据电报文件:

    您可以添加您的联系人,或使用用户名搜索。

    所以你可以按照Telethon中的步骤操作:

    • 将用户添加到联系人
    • 将联系人添加到群组
    • 删除联系人(可选)

    例如,您可以使用以下代码:

    from telethon import TelegramClient
    from telethon.tl.functions.messages import AddChatUserRequest
    from telethon.tl.types import InputPhoneContact
    from telethon.tl.functions.contacts import ImportContactsRequest
    
    api_id = XXXXXX
    api_hash = 'XXXXXXXXXXXXXXXXXXXXXXXX'
    phone_number = '+98XXXXXXXXXX'
    ################################################
    group_id = 263549607 
    guest_phone_number=XXXXXXXXXXXX
    ################################################
    
    client = TelegramClient('session_name',
                        api_id,
                        api_hash)
    
    assert client.connect()
    if not client.is_user_authorized():
    client.send_code_request(phone_number)
    me = client.sign_in(phone_number, input('Enter code: '))
    
    # ---------------------------------------
    # add user to contact
    contact = InputPhoneContact(client_id=0, phone=guest_phone_number, first_name="custom_first_name", last_name="custom_last_name")
    result = client.invoke(ImportContactsRequest([contact]))
    # ---------------------------------------
    # add contact to your group
    client(AddChatUserRequest(user_id=result.users[0], fwd_limit=0, chat_id=group_id))
    # ---------------------------------------
    # remote contact
    

    我用的是‍Telethon V0.19,但是之前的版本差不多

    【讨论】:

    • 如何删除联系人?
    【解决方案2】:

    完成回答,要删除联系人,您可以使用 DeleteContactsRequest 函数:

    from telethon import TelegramClient
    from telethon.tl.functions.messages import AddChatUserRequest
    from telethon.tl.types import InputPhoneContact
    from telethon.tl.functions.contacts import ImportContactsRequest
    
    from telethon.tl.functions.contacts import DeleteContactsRequest
    
    api_id = XXXXXX
    api_hash = 'XXXXXXXXXXXXXXXXXXXXXXXX'
    phone_number = '+98XXXXXXXXXX'
    ################################################
    group_id = 263549607 
    guest_phone_number=XXXXXXXXXXXX
    ################################################
    
    client = TelegramClient('session_name',
                        api_id,
                        api_hash)
    
    assert client.connect()
    if not client.is_user_authorized():
    client.send_code_request(phone_number)
    me = client.sign_in(phone_number, input('Enter code: '))
    
    # ---------------------------------------
    # add user to contact
    contact = InputPhoneContact(client_id=0, phone=guest_phone_number, first_name="custom_first_name", last_name="custom_last_name")
    result = client.invoke(ImportContactsRequest([contact]))
    # ---------------------------------------
    # add contact to your group
    client(AddChatUserRequest(user_id=result.users[0], fwd_limit=0, chat_id=group_id))
    # ---------------------------------------
    # remove contact
    client(DeleteContactsRequest(id=result.users[0]))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-11
      • 2017-12-07
      • 2018-11-01
      • 2019-11-16
      • 1970-01-01
      • 2021-09-11
      • 2021-05-17
      • 1970-01-01
      相关资源
      最近更新 更多