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))
TOKEN = "1234646854fdghdfhfghfghssfhhgh"
def send_group_img(chat_id, text):
temp_files_list = list()
media = list()
files = dict()
for filename in os.listdir("temp"):
temp_files_list.append(f'{os.getcwd()}\\temp\\{filename}')
for f in enumerate(temp_files_list):
files[f"random-name-{f[0]}"] = open(f[1], "rb")
if f[0] == 0:
media.append({"type": "photo",
"media": f"attach://random-name-{f[0]}",
"caption": text}
)
else:
media.append({"type": "photo",
"media": f"attach://random-name-{f[0]}"})
params = {
"chat_id": chat_id, "media": str(media).replace("'", '"')}
request_url = "https://api.telegram.org/bot" + TOKEN + "/sendMediaGroup"
result = requests.post(request_url, params=params, files=files)
if result.status_code == 200:
return True
else:
return False
TOKEN = "1234646854fdghdfhfghfghssfhhgh"
def send_group_img(chat_id, text):
temp_files_list = list()
media = list()
files = dict()
for filename in os.listdir("temp"):
temp_files_list.append(f'{os.getcwd()}\\temp\\{filename}')
for f in enumerate(temp_files_list):
files[f"random-name-{f[0]}"] = open(f[1], "rb")
if f[0] == 0:
media.append({"type": "photo",
"media": f"attach://random-name-{f[0]}",
"caption": text}
)
else:
media.append({"type": "photo",
"media": f"attach://random-name-{f[0]}"})
params = {
"chat_id": chat_id, "media": str(media).replace("'", '"')}
request_url = "https://api.telegram.org/bot" + TOKEN + "/sendMediaGroup"
result = requests.post(request_url, params=params, files=files)
if result.status_code == 200:
return True
else:
return False