FreshPixels commited on
Commit
8527cee
·
verified ·
1 Parent(s): a660ade

Delete server/telegram_utils.py

Browse files
Files changed (1) hide show
  1. server/telegram_utils.py +0 -69
server/telegram_utils.py DELETED
@@ -1,69 +0,0 @@
1
- """Telegram утилиты: отправка сообщений и файлов"""
2
-
3
- import os
4
- import json
5
- import urllib.request
6
- import time
7
- import requests
8
- from .config import CF_URL, TOKEN
9
-
10
- def send_tg(chat_id, text):
11
- if not CF_URL or not TOKEN:
12
- return
13
- safe_text = str(text)[-4000:]
14
- url = f"{CF_URL}/bot{TOKEN}/sendMessage"
15
- data = json.dumps({"chat_id": chat_id, "text": safe_text, "parse_mode": "Markdown"}).encode('utf-8')
16
- headers = {"Content-Type": "application/json", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)"}
17
- req = urllib.request.Request(url, data=data, headers=headers)
18
- try:
19
- urllib.request.urlopen(req, timeout=15)
20
- except Exception as e:
21
- print(f"TG send error: {e}")
22
-
23
- def download_tg_file(file_id, file_name, retries=3):
24
- if not TOKEN:
25
- return None
26
- import urllib3
27
- urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
28
- base_api = CF_URL if CF_URL else "https://api.telegram.org"
29
- get_file_url = f"{base_api}/bot{TOKEN}/getFile?file_id={file_id}"
30
- for attempt in range(retries):
31
- try:
32
- res = requests.get(get_file_url, timeout=15, verify=False).json()
33
- if not res.get("ok"):
34
- return None
35
- file_path = res["result"]["file_path"]
36
- dl_url = f"{base_api}/file/bot{TOKEN}/{file_path}"
37
- os.makedirs("downloads", exist_ok=True)
38
- safe_name = os.path.basename(file_name)
39
- local_path = os.path.join("downloads", safe_name)
40
- response = requests.get(dl_url, stream=True, timeout=60, verify=False)
41
- if response.status_code != 200:
42
- direct_url = f"https://api.telegram.org/file/bot{TOKEN}/{file_path}"
43
- response = requests.get(direct_url, stream=True, timeout=60, verify=False)
44
- response.raise_for_status()
45
- with open(local_path, "wb") as f:
46
- for chunk in response.iter_content(chunk_size=8192):
47
- if chunk:
48
- f.write(chunk)
49
- return local_path
50
- except Exception as e:
51
- time.sleep(2)
52
- return None
53
-
54
- def send_tg_file(chat_id, file_path):
55
- if not os.path.exists(file_path):
56
- send_tg(chat_id, f"File not found: {file_path}")
57
- return
58
- base_api = CF_URL if CF_URL else "https://api.telegram.org"
59
- url = f"{base_api}/bot{TOKEN}/sendDocument"
60
- send_tg(chat_id, f"Sending file: {os.path.basename(file_path)}...")
61
- try:
62
- with open(file_path, 'rb') as f:
63
- res = requests.post(url, data={'chat_id': chat_id}, files={'document': f}, verify=False, timeout=60)
64
- if res.status_code == 200:
65
- print(f"File {file_path} sent to TG!")
66
- else:
67
- send_tg(chat_id, "Failed to send file.")
68
- except Exception as e:
69
- send_tg(chat_id, f"Send error: {e}")