我正在研究Telegra的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
仅适用于我的PeerUser
,即此方法禁止使用非对等电话。
所以问题是,如果他不是我的联系人之一,如何将某人添加到群组聊天中?
答案 0 :(得分:0)
根据电报文件:
您可以添加联系人,也可以使用用户名搜索。
因此,您可以按照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
,但之前的版本几乎相同
答案 1 :(得分:0)
完成答案以删除联系人,您可以使用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]))