2

我正在研究 Telegram 的 Telethon 库,它可以使用 Telegram API 充当 Telegram 客户端(重要:这是Telegram 客户端 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只适用于我PeerUser的s,即这种方法禁止使用非对等电话。

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

4

2 回答 2

4

根据电报文件:

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

因此,您可以按照以下步骤操作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

我用的是‍<code>Telethon V0.19,但之前的版本几乎一样

于 2018-05-16T21:01:46.520 回答
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]))
于 2020-07-29T11:56:46.043 回答