-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
273 lines (212 loc) · 7.91 KB
/
main.py
File metadata and controls
273 lines (212 loc) · 7.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import os
import re
import json
import requests
from config import BOT_TOKEN, ADMIN_ID
TEXT_FILE = "message.txt"
JSON_FILE = "message.json"
# ---------- helpers ----------
def read_text(path):
with open(path, "r", encoding="utf-8") as f:
return f.read().strip()
def read_json(path):
with open(path, "r", encoding="utf-8") as f:
return json.load(f)
def normalize_chat_target(raw: str):
"""
Accepts:
- https://t.me/BlueOrbitDevs
- t.me/BlueOrbitDevs
- @BlueOrbitDevs
- BlueOrbitDevs
- -1003523821626
- 123456789
Returns:
int (for numeric IDs) or '@username'
"""
s = (raw or "").strip()
# Extract username from URL
m = re.search(r"(?:https?://)?(?:t\.me|telegram\.me)/([A-Za-z0-9_]+)", s)
if m:
return "@" + m.group(1)
# @username
if s.startswith("@"):
name = re.sub(r"[^A-Za-z0-9_]", "", s[1:])
return "@" + name if name else s
# Numeric chat ID
if re.fullmatch(r"-?\d+", s):
return int(s)
# Bare username
name = re.sub(r"[^A-Za-z0-9_]", "", s)
return "@" + name if name else s
# ---------- BUTTON BUILDER (STACKED) ----------
def build_reply_markup(cfg):
if not cfg.get("buttons_enable", False):
return None
try:
count = int(cfg.get("button_count", 0))
except Exception:
return None
if count <= 0:
return None
buttons = cfg.get("buttons", [])
if not isinstance(buttons, list):
return None
buttons = buttons[:count]
if not buttons:
return None
# One button per row (stacked)
keyboard = []
for b in buttons:
keyboard.append([b])
return {"inline_keyboard": keyboard}
# ---------- TELEGRAM SEND ----------
def tg_send_message(chat_id, caption, cfg):
"""
Sends a message with optional media to chat_id using configuration in cfg.
cfg keys used:
- photo_enable (bool): whether to send an image/animation/document (default True)
- photo (str): path to media file (default "image.jpg")
- force_document (bool): if True, use sendDocument (uploads original file)
- send_as_animation (bool): if True, use sendAnimation regardless of extension
- has_spoiler (bool): included only when sending as photo (sendPhoto)
- parse_mode (str): HTML/Markdown etc. (default "HTML")
- protect_content (bool): if True, prevents forwarding/saving
- buttons_enable, button_count, buttons: used by build_reply_markup
"""
parse_mode = cfg.get("parse_mode", "HTML")
reply_markup = build_reply_markup(cfg)
def apply_common_flags(data: dict):
# protect_content is supported in sendMessage/sendPhoto/sendDocument/sendAnimation
if cfg.get("protect_content") is True:
data["protect_content"] = True
def _post(url, data, files=None, timeout=30):
try:
r = requests.post(url, data=data, files=files, timeout=timeout)
except requests.RequestException as e:
return False, {"error": "request_exception", "detail": str(e)}
try:
resp = r.json()
except Exception:
return False, {"error": "Invalid Telegram response", "status_code": getattr(r, "status_code", None)}
ok = r.status_code == 200 and resp.get("ok") is True
return ok, resp
if cfg.get("photo_enable", True):
media_path = cfg.get("photo", "image.jpg")
if not os.path.exists(media_path):
return False, {"error": f"Media file not found: {media_path}"}
_, ext = os.path.splitext(media_path)
ext = ext.lower()
# Option to always upload as document (keeps original file & metadata)
if cfg.get("force_document", False):
url = f"https://api.telegram.org/bot{BOT_TOKEN}/sendDocument"
data = {
"chat_id": chat_id,
"caption": caption,
"parse_mode": parse_mode,
}
apply_common_flags(data)
if reply_markup:
data["reply_markup"] = json.dumps(reply_markup)
with open(media_path, "rb") as f:
return _post(url, data, files={"document": f})
# If GIF or explicitly set to send as animation -> sendAnimation
if ext == ".gif" or cfg.get("send_as_animation", False):
url = f"https://api.telegram.org/bot{BOT_TOKEN}/sendAnimation"
data = {
"chat_id": chat_id,
"caption": caption,
"parse_mode": parse_mode,
}
apply_common_flags(data)
if reply_markup:
data["reply_markup"] = json.dumps(reply_markup)
with open(media_path, "rb") as f:
return _post(url, data, files={"animation": f})
# Default: send as photo
url = f"https://api.telegram.org/bot{BOT_TOKEN}/sendPhoto"
data = {
"chat_id": chat_id,
"caption": caption,
"parse_mode": parse_mode,
}
apply_common_flags(data)
# has_spoiler is supported for photos
if cfg.get("has_spoiler") is True:
data["has_spoiler"] = True
if reply_markup:
data["reply_markup"] = json.dumps(reply_markup)
with open(media_path, "rb") as img:
return _post(url, data, files={"photo": img})
# If no media, send plain text message
url = f"https://api.telegram.org/bot{BOT_TOKEN}/sendMessage"
data = {
"chat_id": chat_id,
"text": caption,
"parse_mode": parse_mode,
}
apply_common_flags(data)
if reply_markup:
data["reply_markup"] = json.dumps(reply_markup)
return _post(url, data)
# ---------- ERROR EXPLAIN ----------
def explain_common_errors(resp):
"""
Prints human-friendly tips based on Telegram response dict.
"""
if not isinstance(resp, dict):
print("\nUnexpected response:", resp)
return
code = resp.get("error_code")
desc = str(resp.get("description", "")).lower()
if code == 403:
print("\nFix (403 Forbidden):")
print("- Bot must be added to the group/channel")
print("- For channels, bot must be ADMIN with 'Post Messages' permission")
elif code == 400 and "chat not found" in desc:
print("\nFix (chat not found):")
print("- Username may be wrong or changed")
print("- Private channels require numeric -100... ID")
print("- Bot must be a member of the target chat")
elif code == 400 and "file is too big" in desc:
print("\nFix (file is too big):")
print("- Telegram enforces upload limits for bots. Try reducing file size.")
print("- Convert GIF to MP4 (video) and send as animation, or host externally.")
else:
if desc:
print(f"\nTelegram error ({code}): {resp.get('description')}")
# ---------- MAIN ----------
def main():
caption = ""
cfg = {}
try:
caption = read_text(TEXT_FILE)
except Exception as e:
print(f"Warning: could not read {TEXT_FILE}: {e}")
try:
cfg = read_json(JSON_FILE)
except Exception as e:
print(f"Warning: could not read {JSON_FILE}: {e}")
raw_target = input("Enter target (URL / @username / username / chat_id): ").strip()
target = normalize_chat_target(raw_target)
# Admin preview
print("Sending preview to admin...")
ok, resp = tg_send_message(ADMIN_ID, caption, cfg)
if not ok:
print("Admin preview failed:", resp)
explain_common_errors(resp)
return
print("Admin preview delivered.")
confirm = input('Type "Y" to send to target: ').strip().upper()
if confirm != "Y":
print("Cancelled.")
return
print(f"Posting to: {target}")
ok, resp = tg_send_message(target, caption, cfg)
if ok:
print("Post successful.")
else:
print("Post failed:", resp)
explain_common_errors(resp)
if __name__ == "__main__":
main()