【问题标题】:how to delete contact by number telethon如何通过电话号码删除联系人
【发布时间】:2021-11-17 09:33:24
【问题描述】:

进口:

from telethon import TelegramClient, events, sync
from telethon.tl.types import InputPhoneContact
from telethon import functions, types

代码:

phone_number = "some_friend_phone_no"
contact = InputPhoneContact(client_id=0, phone=phone_number, first_name="first", last_name="last") #enter phone number with settings
contacts = client(functions.contacts.ImportContactsRequest([contact])) #import that number to contacts
if len(contacts.users) > 0: #if user exists on telegram
    username = contacts.users[0].username #then insert user value into username variable
    if username is not None: #if username is not empty
        client(functions.contacts.DeleteContactsRequest(id=[username])) #then DELETE that contact

该代码首先将phone_number 添加到我们的联系人列表中,然后我们可以轻松获取他们的用户名。

在我们删除该联系人后,因为我们只需要他们的用户名。

但是有些用户没有设置用户名,所以我们得到用户名None

现在如何删除该联系人? 目前我使用client(functions.contacts.DeleteContactsRequest(id=[username])),如果用户名是None会失败。

【问题讨论】:

    标签: python telegram telethon


    【解决方案1】:

    根据docs,你可以做一个InputUser,然后交给DeleteContactRequest

    由于您已经拥有该用户,您可以:

    client(functions.contacts.DeleteContactsRequest(id=[contacts.users[0]]))
    

    【讨论】:

    • 非常简单直接的答案。代码奏效了。非常感谢。
    猜你喜欢
    • 2016-12-26
    • 1970-01-01
    • 2012-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-01
    • 2011-07-30
    相关资源
    最近更新 更多