from pyrogram import Client
from pyrogram.types import InputPhoneContact
import json
import tempfile
api_id = 1234567
api_hash = "1q2w3e4r5t6yhbv"
app = Client("number", api_id=api_id, api_hash=api_hash)
def get_chat_id(phone_num):
temp_contact_name = tempfile.NamedTemporaryFile().name.split('\\')[-1]
good_res = list()
with app:
app.import_contacts([InputPhoneContact(phone=phone_num, first_name=temp_contact_name)])
contacts = app.get_contacts()
for contact in contacts:
contact_data = json.loads(str(contact))
if contact_data['first_name'] == temp_contact_name:
good_res.append(contact_data)
app.delete_contacts(contact_data['id'])
try:
good_res = good_res[0]['id']
except:
good_res = None
return good_res
print(get_chat_id('+79000000000))
Функция сначала добавляет номер по телефону, смотрит его chat_id потом удаляет контакт