Spaces:
Running
Running
Upload 2 files
Browse files
app.py
CHANGED
|
@@ -1333,6 +1333,7 @@ def _build_video(vpath, cmb, mpath, ad, vd, crop, flip, col, wmk, out_file,
|
|
| 1333 |
f'-filter_complex "{filter_complex}" '
|
| 1334 |
f'-map "[v_final]" -map "[outa]" '
|
| 1335 |
f'-c:v libx264 -crf 24 -preset ultrafast -pix_fmt yuv420p '
|
|
|
|
| 1336 |
f'-c:a aac -ar 44100 -b:a 128k '
|
| 1337 |
f'-t {ad:.3f} -movflags +faststart "{out_file}"'
|
| 1338 |
)
|
|
|
|
| 1333 |
f'-filter_complex "{filter_complex}" '
|
| 1334 |
f'-map "[v_final]" -map "[outa]" '
|
| 1335 |
f'-c:v libx264 -crf 24 -preset ultrafast -pix_fmt yuv420p '
|
| 1336 |
+
f'-threads 0 '
|
| 1337 |
f'-c:a aac -ar 44100 -b:a 128k '
|
| 1338 |
f'-t {ad:.3f} -movflags +faststart "{out_file}"'
|
| 1339 |
)
|
bot.py
CHANGED
|
@@ -1,536 +1,968 @@
|
|
| 1 |
"""
|
| 2 |
Recap Studio โ Telegram Bot
|
| 3 |
-
Features:
|
| 4 |
-
โข User: Login, Auto-process, Voice, Settings, Coins, Payment submit, Payment history
|
| 5 |
-
โข Admin: Pending payments, Approve/Reject, Add/Set coins, User list, Broadcast
|
| 6 |
"""
|
| 7 |
-
|
| 8 |
-
import os, json, uuid, re, time, logging, threading
|
| 9 |
-
import urllib.request, urllib.parse
|
| 10 |
-
from datetime import datetime
|
| 11 |
from pathlib import Path
|
| 12 |
|
| 13 |
-
logging.basicConfig(
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
PACKAGES = [
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
{'coins': 100, 'price': '95,000 MMK'},
|
| 36 |
]
|
| 37 |
-
KBZ_NAME = 'PHOE SHAN'
|
| 38 |
-
KBZ_NUMBER = '09679871352'
|
| 39 |
-
|
| 40 |
-
# Per-user state: {'step': ..., 'data': {...}}
|
| 41 |
-
_state: dict = {}
|
| 42 |
-
_state_lock = threading.Lock()
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
-
|
| 53 |
-
with _state_lock:
|
| 54 |
-
_state.pop(str(cid), None)
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
| 60 |
try:
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
-
def
|
| 67 |
-
with open(DB_FILE, 'w', encoding='utf-8') as f:
|
| 68 |
-
json.dump(db, f, ensure_ascii=False, indent=2)
|
| 69 |
|
| 70 |
-
def load_payments():
|
| 71 |
-
try:
|
| 72 |
-
with open(PAYMENTS_DB_FILE, encoding='utf-8') as f:
|
| 73 |
-
return json.load(f)
|
| 74 |
-
except:
|
| 75 |
-
return {'payments': []}
|
| 76 |
-
|
| 77 |
-
def save_payments(db):
|
| 78 |
-
with open(PAYMENTS_DB_FILE, 'w', encoding='utf-8') as f:
|
| 79 |
-
json.dump(db, f, ensure_ascii=False, indent=2)
|
| 80 |
-
|
| 81 |
-
def get_tg_session(cid):
|
| 82 |
-
"""Return username linked to this telegram chat_id, or None."""
|
| 83 |
-
db = load_db()
|
| 84 |
-
cid_str = str(cid)
|
| 85 |
-
for uname, udata in db['users'].items():
|
| 86 |
-
if str(udata.get('tg_chat_id', '')) == cid_str:
|
| 87 |
-
return uname
|
| 88 |
-
return None
|
| 89 |
-
|
| 90 |
-
def link_tg(username, cid):
|
| 91 |
-
db = load_db()
|
| 92 |
-
if username in db['users']:
|
| 93 |
-
db['users'][username]['tg_chat_id'] = str(cid)
|
| 94 |
-
save_db(db)
|
| 95 |
-
|
| 96 |
-
def get_coins(username):
|
| 97 |
-
db = load_db()
|
| 98 |
-
return db['users'].get(username, {}).get('coins', 0)
|
| 99 |
-
|
| 100 |
-
def add_coins(username, n):
|
| 101 |
-
db = load_db()
|
| 102 |
-
if username not in db['users']:
|
| 103 |
-
return False, 0
|
| 104 |
-
db['users'][username]['coins'] = db['users'][username].get('coins', 0) + int(n)
|
| 105 |
-
save_db(db)
|
| 106 |
-
return True, db['users'][username]['coins']
|
| 107 |
-
|
| 108 |
-
def set_coins(username, n):
|
| 109 |
-
db = load_db()
|
| 110 |
-
if username not in db['users']:
|
| 111 |
-
return False, 0
|
| 112 |
-
db['users'][username]['coins'] = int(n)
|
| 113 |
-
save_db(db)
|
| 114 |
-
return True, int(n)
|
| 115 |
-
|
| 116 |
-
import hashlib
|
| 117 |
-
def hp(p): return hashlib.sha256(p.encode()).hexdigest()
|
| 118 |
-
|
| 119 |
-
def verify_login(username, password):
|
| 120 |
-
if username == ADMIN_USERNAME and password == ADMIN_PASSWORD:
|
| 121 |
-
return True, True # ok, is_admin
|
| 122 |
-
db = load_db()
|
| 123 |
-
if username not in db['users']:
|
| 124 |
-
return False, False
|
| 125 |
-
stored = db['users'][username].get('password', '')
|
| 126 |
-
if stored and stored != hp(password):
|
| 127 |
-
return False, False
|
| 128 |
-
return True, False
|
| 129 |
-
|
| 130 |
-
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 131 |
-
# TELEGRAM API HELPERS
|
| 132 |
-
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 133 |
-
def tg(method, **params):
|
| 134 |
-
url = f'{API}/{method}'
|
| 135 |
-
data = json.dumps(params).encode()
|
| 136 |
-
req = urllib.request.Request(url, data=data, headers={'Content-Type': 'application/json'})
|
| 137 |
try:
|
| 138 |
-
|
| 139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
except Exception as e:
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
def
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
f'--{boundary}\r\n'
|
| 155 |
-
f'Content-Disposition: form-data; name="chat_id"\r\n\r\n'
|
| 156 |
-
f'{cid}\r\n'
|
| 157 |
-
f'--{boundary}\r\n'
|
| 158 |
-
f'Content-Disposition: form-data; name="caption"\r\n\r\n'
|
| 159 |
-
f'{caption}\r\n'
|
| 160 |
-
f'--{boundary}\r\n'
|
| 161 |
-
f'Content-Disposition: form-data; name="parse_mode"\r\n\r\nHTML\r\n'
|
| 162 |
-
f'--{boundary}\r\n'
|
| 163 |
-
f'Content-Disposition: form-data; name="photo"; filename="slip.jpg"\r\n'
|
| 164 |
-
f'Content-Type: image/jpeg\r\n\r\n'
|
| 165 |
-
).encode() + photo_bytes + f'\r\n--{boundary}--\r\n'.encode()
|
| 166 |
-
if markup:
|
| 167 |
-
body = body[:-len(f'\r\n--{boundary}--\r\n'.encode())]
|
| 168 |
-
body += (
|
| 169 |
-
f'\r\n--{boundary}\r\n'
|
| 170 |
-
f'Content-Disposition: form-data; name="reply_markup"\r\n\r\n'
|
| 171 |
-
f'{json.dumps(markup)}\r\n'
|
| 172 |
-
f'--{boundary}--\r\n'
|
| 173 |
-
).encode()
|
| 174 |
-
req = urllib.request.Request(
|
| 175 |
-
f'{API}/sendPhoto',
|
| 176 |
-
data=body,
|
| 177 |
-
headers={'Content-Type': f'multipart/form-data; boundary={boundary}'}
|
| 178 |
-
)
|
| 179 |
try:
|
| 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 |
-
return txt
|
| 244 |
-
|
| 245 |
-
def notify_admin_payment(p, slip_bytes=None):
|
| 246 |
-
"""Send payment notification to admin."""
|
| 247 |
-
if not ADMIN_CHAT_ID:
|
| 248 |
-
return
|
| 249 |
-
markup = kb(
|
| 250 |
-
[(f'โ
Approve {p["coins"]} Coins', f'apv:{p["id"]}')],
|
| 251 |
-
[('โ Reject', f'rej:{p["id"]}')]
|
| 252 |
-
)
|
| 253 |
-
caption = (f'๐ฐ <b>New Payment Request</b>\n\n'
|
| 254 |
-
f'๐ค User: <code>{p["username"]}</code>\n'
|
| 255 |
-
f'๐ช {p["coins"]} Coins โ {p["price"]}\n'
|
| 256 |
-
f'๐ <code>{p["id"]}</code>\n'
|
| 257 |
-
f'๐
{p["created_at"][:16].replace("T"," ")}')
|
| 258 |
-
if slip_bytes:
|
| 259 |
-
send_photo(ADMIN_CHAT_ID, slip_bytes, caption=caption, markup=markup)
|
| 260 |
-
else:
|
| 261 |
-
send(ADMIN_CHAT_ID, caption, markup=markup)
|
| 262 |
-
|
| 263 |
-
def notify_user_payment(p, approved):
|
| 264 |
-
"""Notify user when payment is approved/rejected."""
|
| 265 |
-
db = load_db()
|
| 266 |
-
udata = db['users'].get(p['username'], {})
|
| 267 |
-
cid = udata.get('tg_chat_id')
|
| 268 |
-
if not cid:
|
| 269 |
return
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
f'๐ผ แแแบแแปแแบ Coins: <b>{coins}</b>\n\n'
|
| 275 |
-
f'แแปแฑแธแแฐแธแแแบแแซแแแบ ๐')
|
| 276 |
-
else:
|
| 277 |
-
note = p.get('admin_note', '')
|
| 278 |
-
send(cid, f'โ <b>Payment Rejected</b>\n\n'
|
| 279 |
-
f'๐ช {p["coins"]} Coins โ {p["price"]}\n'
|
| 280 |
-
+ (f'๐ แกแแผแฑแฌแแบแธแแผแแปแแบ: {note}\n' if note else '') +
|
| 281 |
-
f'\nแแแบแแถ แแแบแแฝแแบแแซ ๐')
|
| 282 |
-
|
| 283 |
-
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 284 |
-
|
| 285 |
-
def is_admin_chat(cid):
|
| 286 |
-
return str(cid) == str(ADMIN_CHAT_ID)
|
| 287 |
-
|
| 288 |
-
def show_pending(cid):
|
| 289 |
-
pdb = load_payments()
|
| 290 |
-
pending = [p for p in pdb['payments'] if p['status'] == 'pending']
|
| 291 |
-
if not pending:
|
| 292 |
-
send(cid, 'โ
Pending payments แแแพแญแแซ'); admin_menu(cid); return
|
| 293 |
-
send(cid, f'โณ <b>Pending Payments ({len(pending)})</b>')
|
| 294 |
-
for p in pending[:10]:
|
| 295 |
-
markup = kb(
|
| 296 |
-
[(f'โ
Approve {p["coins"]} Coins', f'apv:{p["id"]}')],
|
| 297 |
-
[('โ Reject', f'rej:{p["id"]}'), ('๐ผ Slip', f'slip:{p["id"]}')]
|
| 298 |
-
)
|
| 299 |
-
send(cid, fmt_payment(p), markup)
|
| 300 |
-
|
| 301 |
-
def show_payments(cid, status='all'):
|
| 302 |
-
pdb = load_payments()
|
| 303 |
-
pmts = pdb['payments'] if status == 'all' else [p for p in pdb['payments'] if p['status'] == status]
|
| 304 |
-
pmts = sorted(pmts, key=lambda x: x['created_at'], reverse=True)[:10]
|
| 305 |
-
if not pmts:
|
| 306 |
-
send(cid, f'๐ญ {status} payments แแแพแญแแซ'); admin_menu(cid); return
|
| 307 |
-
for p in pmts:
|
| 308 |
-
send(cid, fmt_payment(p))
|
| 309 |
-
|
| 310 |
-
def show_users(cid):
|
| 311 |
-
db = load_db()
|
| 312 |
-
users = sorted(db['users'].items(), key=lambda x: x[1].get('coins', 0), reverse=True)[:20]
|
| 313 |
-
lines = ['๐ฅ <b>User List</b>\n']
|
| 314 |
-
for uname_, udata in users:
|
| 315 |
-
c = udata.get('coins', 0); v = udata.get('total_videos', 0)
|
| 316 |
-
lines.append(f'โข <code>{uname_}</code> โ ๐ช{c} | ๐ฌ{v}')
|
| 317 |
-
send(cid, '\n'.join(lines))
|
| 318 |
-
|
| 319 |
-
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 320 |
-
# ADMIN-ONLY MESSAGE HANDLER
|
| 321 |
-
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 322 |
-
def handle_message(msg):
|
| 323 |
-
cid = msg['chat']['id']
|
| 324 |
-
text = msg.get('text', '').strip()
|
| 325 |
-
|
| 326 |
-
if not is_admin_chat(cid):
|
| 327 |
-
send(cid, '๐ แค Bot แแแบ Admin แกแแฝแแบแแฌ แแผแ
แบแแซแแแบ')
|
| 328 |
return
|
| 329 |
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
# โโ Add Coins โโ
|
| 348 |
-
if text in ('โ Add Coins', '/addcoins'):
|
| 349 |
-
set_state(cid, 'adm_add_user')
|
| 350 |
-
send(cid, 'โ <b>Add Coins</b>\n\nUsername แแแทแบแแซ:', rkb(['โ Cancel'])); return
|
| 351 |
-
|
| 352 |
-
if step == 'adm_add_user':
|
| 353 |
-
if text == 'โ Cancel': clear_state(cid); admin_menu(cid); return
|
| 354 |
-
set_state(cid, 'adm_add_n', target=text)
|
| 355 |
-
send(cid, f'๐ค <code>{text}</code>\n\nCoins แกแแฑแกแแฝแแบ:', rkb(['โ Cancel'])); return
|
| 356 |
-
|
| 357 |
-
if step == 'adm_add_n':
|
| 358 |
-
if text == 'โ Cancel': clear_state(cid); admin_menu(cid); return
|
| 359 |
-
target = sdata.get('target', '')
|
| 360 |
-
try: n = int(text)
|
| 361 |
-
except: send(cid, 'โ แแแแบแธ แแแทแบแแซ'); return
|
| 362 |
-
ok, total = add_coins(target, n)
|
| 363 |
-
clear_state(cid)
|
| 364 |
-
send(cid, f'โ
<code>{target}</code> โ +{n} ๐ช\n๐ผ แแแบแแปแแบ: <b>{total}</b>' if ok else 'โ User แแแฝแฑแทแแซ')
|
| 365 |
-
admin_menu(cid); return
|
| 366 |
-
|
| 367 |
-
# โโ Set Coins โโ
|
| 368 |
-
if text in ('๐ง Set Coins', '/setcoins'):
|
| 369 |
-
set_state(cid, 'adm_set_user')
|
| 370 |
-
send(cid, '๐ง <b>Set Coins</b>\n\nUsername แแแทแบแแซ:', rkb(['โ Cancel'])); return
|
| 371 |
-
|
| 372 |
-
if step == 'adm_set_user':
|
| 373 |
-
if text == 'โ Cancel': clear_state(cid); admin_menu(cid); return
|
| 374 |
-
set_state(cid, 'adm_set_n', target=text)
|
| 375 |
-
send(cid, f'๐ค <code>{text}</code>\n\nCoins แแแบแแญแฏแธ (แแแทแบแแแบแแป):', rkb(['โ Cancel'])); return
|
| 376 |
-
|
| 377 |
-
if step == 'adm_set_n':
|
| 378 |
-
if text == 'โ Cancel': clear_state(cid); admin_menu(cid); return
|
| 379 |
-
target = sdata.get('target', '')
|
| 380 |
-
try: n = int(text)
|
| 381 |
-
except: send(cid, 'โ แแแแบแธ แแแทแบแแซ'); return
|
| 382 |
-
ok, total = set_coins(target, n)
|
| 383 |
-
clear_state(cid)
|
| 384 |
-
send(cid, f'โ
<code>{target}</code> Coins = <b>{total}</b>' if ok else 'โ User แแแฝแฑแทแแซ')
|
| 385 |
-
admin_menu(cid); return
|
| 386 |
-
|
| 387 |
-
# โโ Delete User โโ
|
| 388 |
-
if text in ('๐ Delete User', '/deluser'):
|
| 389 |
-
set_state(cid, 'adm_del_user')
|
| 390 |
-
send(cid, '๐ <b>Delete User</b>\n\nUsername แแแทแบแแซ:', rkb(['โ Cancel'])); return
|
| 391 |
-
|
| 392 |
-
if step == 'adm_del_user':
|
| 393 |
-
if text == 'โ Cancel': clear_state(cid); admin_menu(cid); return
|
| 394 |
-
db = load_db()
|
| 395 |
-
if text not in db['users']: send(cid, 'โ User แแแฝแฑแทแแซ'); clear_state(cid); return
|
| 396 |
-
set_state(cid, 'adm_del_confirm', target=text)
|
| 397 |
-
send(cid, f'โ ๏ธ <code>{text}</code> แแญแฏ แแปแแบแแพแฌ แแฑแแปแฌแแซแแแฌแธ?',
|
| 398 |
-
rkb([f'โ
Yes, Delete', 'โ Cancel'])); return
|
| 399 |
-
|
| 400 |
-
if step == 'adm_del_confirm':
|
| 401 |
-
if text == 'โ Cancel': clear_state(cid); admin_menu(cid); return
|
| 402 |
-
target = sdata.get('target', '')
|
| 403 |
-
db = load_db()
|
| 404 |
-
if target in db['users']:
|
| 405 |
-
del db['users'][target]; save_db(db)
|
| 406 |
-
send(cid, f'โ
<code>{target}</code> แแปแแบแแผแฎแธแแซแแผแฎ')
|
| 407 |
-
else:
|
| 408 |
-
send(cid, 'โ User แแแฝแฑแทแแซ')
|
| 409 |
-
clear_state(cid); admin_menu(cid); return
|
| 410 |
-
|
| 411 |
-
# โโ Broadcast โโ
|
| 412 |
-
if text in ('๐ข Broadcast', '/broadcast'):
|
| 413 |
-
set_state(cid, 'adm_broadcast')
|
| 414 |
-
send(cid, '๐ข <b>Broadcast</b>\n\nMessage แแญแฏแแบแแซ:', rkb(['โ Cancel'])); return
|
| 415 |
-
|
| 416 |
-
if step == 'adm_broadcast':
|
| 417 |
-
if text == 'โ Cancel': clear_state(cid); admin_menu(cid); return
|
| 418 |
-
clear_state(cid)
|
| 419 |
-
db = load_db(); sent = 0
|
| 420 |
-
for udata in db['users'].values():
|
| 421 |
-
tcid = udata.get('tg_chat_id')
|
| 422 |
-
if tcid:
|
| 423 |
-
try: send(tcid, f'๐ข <b>Recap Studio</b>\n\n{text}'); sent += 1; time.sleep(0.1)
|
| 424 |
-
except: pass
|
| 425 |
-
send(cid, f'โ
{sent} users แแถ แแญแฏแทแแผแฎแธแแซแแผแฎ')
|
| 426 |
-
admin_menu(cid); return
|
| 427 |
-
|
| 428 |
-
# โโ Approve/Reject note flows โโ
|
| 429 |
-
if step == 'adm_approve_note':
|
| 430 |
-
pid = sdata.get('pid', '')
|
| 431 |
-
_do_approve(cid, pid, text if text.lower() != 'skip' else ''); return
|
| 432 |
-
|
| 433 |
-
if step == 'adm_reject_note':
|
| 434 |
-
pid = sdata.get('pid', '')
|
| 435 |
-
_do_reject(cid, pid, text if text.lower() != 'skip' else ''); return
|
| 436 |
-
|
| 437 |
-
admin_menu(cid)
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 441 |
-
# CALLBACK QUERY HANDLER
|
| 442 |
-
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 443 |
-
def handle_callback(cb):
|
| 444 |
-
cid = cb['message']['chat']['id']
|
| 445 |
-
mid = cb['message']['message_id']
|
| 446 |
-
cb_id = cb['id']
|
| 447 |
-
data = cb.get('data', '')
|
| 448 |
-
|
| 449 |
-
answer_cb(cb_id)
|
| 450 |
-
|
| 451 |
-
if not is_admin_chat(cid):
|
| 452 |
return
|
| 453 |
|
| 454 |
-
if data.startswith('
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
|
| 458 |
return
|
| 459 |
|
| 460 |
-
if data.startswith('
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 464 |
return
|
| 465 |
|
| 466 |
-
if data.startswith('
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
|
| 471 |
-
|
| 472 |
-
|
| 473 |
-
|
| 474 |
-
|
| 475 |
-
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
[('
|
| 479 |
-
|
| 480 |
-
|
| 481 |
-
|
| 482 |
-
|
| 483 |
-
|
| 484 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 485 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 486 |
|
| 487 |
-
|
| 488 |
-
# POLLING LOOP (409 Conflict safe)
|
| 489 |
-
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 490 |
-
def poll():
|
| 491 |
-
for attempt in range(5):
|
| 492 |
try:
|
| 493 |
-
|
| 494 |
-
|
| 495 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 496 |
except Exception as e:
|
| 497 |
-
|
| 498 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 499 |
|
| 500 |
-
|
| 501 |
-
|
| 502 |
-
|
| 503 |
-
|
| 504 |
-
|
| 505 |
-
|
| 506 |
-
|
| 507 |
-
|
| 508 |
-
|
| 509 |
-
|
| 510 |
-
|
| 511 |
-
|
| 512 |
-
|
| 513 |
-
|
| 514 |
-
|
| 515 |
-
|
| 516 |
-
|
| 517 |
-
|
| 518 |
-
|
| 519 |
-
|
| 520 |
-
|
| 521 |
-
|
| 522 |
-
|
| 523 |
-
|
| 524 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 525 |
|
|
|
|
| 526 |
|
| 527 |
def main():
|
| 528 |
-
"""Entry point โ called by run_bot.py"""
|
| 529 |
if not BOT_TOKEN:
|
| 530 |
-
|
| 531 |
-
|
| 532 |
-
|
| 533 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 534 |
|
| 535 |
if __name__ == '__main__':
|
| 536 |
main()
|
|
|
|
| 1 |
"""
|
| 2 |
Recap Studio โ Telegram Bot
|
|
|
|
|
|
|
|
|
|
| 3 |
"""
|
| 4 |
+
import os, sys, json, uuid, glob, shutil, logging, threading, time, re, subprocess
|
|
|
|
|
|
|
|
|
|
| 5 |
from pathlib import Path
|
| 6 |
|
| 7 |
+
logging.basicConfig(format='%(asctime)s [%(levelname)s] %(name)s: %(message)s', level=logging.INFO)
|
| 8 |
+
logger = logging.getLogger(__name__)
|
| 9 |
+
|
| 10 |
+
from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup, ReplyKeyboardMarkup, KeyboardButton
|
| 11 |
+
from telegram.ext import Application, CommandHandler, MessageHandler, CallbackQueryHandler, ConversationHandler, filters, ContextTypes
|
| 12 |
+
from telegram.constants import ParseMode
|
| 13 |
+
|
| 14 |
+
sys.path.insert(0, str(Path(__file__).parent))
|
| 15 |
+
from app import (
|
| 16 |
+
login_user, get_coins, deduct, load_db, save_db,
|
| 17 |
+
gen_uname, create_user_fn, add_coins_fn, set_coins_fn,
|
| 18 |
+
call_api, parse_out, split_txt, dur,
|
| 19 |
+
run_tts_sync, run_gemini_tts_sync,
|
| 20 |
+
#ytdlp_download, cpu_queue_wait, upd_stat,
|
| 21 |
+
_build_video, ADMIN_U, SYS_MOVIE, SYS_MED, BASE_DIR, OUTPUT_DIR,
|
| 22 |
+
NUM_TO_MM_RULE, run_stage, ytdlp_download, upd_stat,
|
| 23 |
+
load_payments_db, save_payments_db,
|
| 24 |
+
TELEGRAM_BOT_TOKEN, ADMIN_TELEGRAM_CHAT_ID,
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
try:
|
| 28 |
+
import whisper as whisper_mod
|
| 29 |
+
except ImportError:
|
| 30 |
+
whisper_mod = None
|
| 31 |
+
|
| 32 |
+
BOT_TOKEN = os.getenv('TELEGRAM_BOT_TOKEN', '')
|
| 33 |
+
ADMIN_TG_USERNAME = os.getenv('ADMIN_TG_USERNAME', 'PhoeShan2001')
|
| 34 |
+
WEB_BASE_URL = os.getenv('WEB_BASE_URL', 'https://recap.psonline.shop')
|
| 35 |
+
TG_MAX_FILE_BYTES = 49 * 1024 * 1024 # 49 MB โ Telegram bot upload limit
|
| 36 |
+
|
| 37 |
+
(ST_MAIN, ST_LOGIN_USER, ST_LOGIN_PASS, ST_AWAIT_VIDEO) = range(4)
|
| 38 |
+
|
| 39 |
+
sessions = {}
|
| 40 |
+
_whisper_model = [None]
|
| 41 |
+
_wm_lock = threading.Lock()
|
| 42 |
+
cancel_flags = {}
|
| 43 |
+
|
| 44 |
+
# โโ Per-user busy flag (one job at a time) โโ
|
| 45 |
+
user_busy = {} # cid -> bool
|
| 46 |
+
_busy_lock = threading.Lock()
|
| 47 |
+
|
| 48 |
+
def is_processing(cid):
|
| 49 |
+
with _busy_lock:
|
| 50 |
+
return user_busy.get(cid, False)
|
| 51 |
+
|
| 52 |
+
def set_processing(cid, val):
|
| 53 |
+
with _busy_lock:
|
| 54 |
+
user_busy[cid] = val
|
| 55 |
+
|
| 56 |
+
def start_job(bot, cid, pending_url, pending_file_id, prog_msg_id):
|
| 57 |
+
"""Start processing in background thread."""
|
| 58 |
+
threading.Thread(
|
| 59 |
+
target=_process_thread,
|
| 60 |
+
args=(bot, cid, prog_msg_id, pending_url, pending_file_id),
|
| 61 |
+
daemon=True
|
| 62 |
+
).start()
|
| 63 |
+
|
| 64 |
+
MS_VOICES = [
|
| 65 |
+
('Thiha (แแปแฌแธ)', 'my-MM-ThihaNeural', 'ms'),
|
| 66 |
+
('Nilar (แแญแแบแธ)', 'my-MM-NilarNeural', 'ms'),
|
| 67 |
+
]
|
| 68 |
|
| 69 |
+
GEMINI_VOICES = [
|
| 70 |
+
('Kore','Kore','gemini'),('Charon','Charon','gemini'),('Fenrir','Fenrir','gemini'),
|
| 71 |
+
('Leda','Leda','gemini'),('Orus','Orus','gemini'),('Puck','Puck','gemini'),
|
| 72 |
+
('Aoede','Aoede','gemini'),('Zephyr','Zephyr','gemini'),('Achelois','Achelois','gemini'),
|
| 73 |
+
('Pegasus','Pegasus','gemini'),('Perseus','Perseus','gemini'),('Schedar','Schedar','gemini'),
|
| 74 |
+
]
|
| 75 |
|
| 76 |
PACKAGES = [
|
| 77 |
+
(10, 10000, 'Process 5 แแผแญแแบ'),
|
| 78 |
+
(20, 18000, 'Process 10 แแผแญแแบ โ แกแแฑแฌแแบแธแแฏแถแธ'),
|
| 79 |
+
(30, 28000, 'Process 15 แแผแญแแบ'),
|
|
|
|
| 80 |
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
+
# pending_payment[cid] = {'coins': int, 'price': int} โ slip แ
แฑแฌแแทแบแแฑแ
แแบ
|
| 83 |
+
pending_payment = {}
|
| 84 |
+
|
| 85 |
+
# โโ HELPERS โโ
|
| 86 |
+
|
| 87 |
+
def sess(cid):
|
| 88 |
+
if cid not in sessions:
|
| 89 |
+
sessions[cid] = {
|
| 90 |
+
'username': None, 'coins': 0, 'is_admin': False,
|
| 91 |
+
'voice': 'my-MM-ThihaNeural', 'engine': 'ms',
|
| 92 |
+
'speed': 30, 'crop': 'original',
|
| 93 |
+
'flip': False, 'color': False,
|
| 94 |
+
'watermark': '', 'content_type': 'Movie Recap',
|
| 95 |
+
'ai_model': 'Gemini',
|
| 96 |
+
'pending_url': None, 'pending_file_id': None,
|
| 97 |
+
'music_file_id': None,
|
| 98 |
+
}
|
| 99 |
+
s = sessions[cid]
|
| 100 |
+
# Auto-sync coins from DB so admin top-ups reflect without re-login
|
| 101 |
+
if s.get('username'):
|
| 102 |
+
try:
|
| 103 |
+
fresh = get_coins(s['username'])
|
| 104 |
+
if fresh is not None:
|
| 105 |
+
s['coins'] = fresh
|
| 106 |
+
except Exception:
|
| 107 |
+
pass
|
| 108 |
+
return s
|
| 109 |
+
|
| 110 |
+
def is_logged(cid): return sess(cid).get('username') is not None
|
| 111 |
+
def fmt_coins(c): return 'โ' if c == -1 else str(c)
|
| 112 |
+
|
| 113 |
+
def main_kb(cid):
|
| 114 |
+
s = sess(cid)
|
| 115 |
+
rows = [
|
| 116 |
+
[KeyboardButton('๐ฌ Auto Process'), KeyboardButton('๐ แกแแถ')],
|
| 117 |
+
[KeyboardButton('โ๏ธ Settings'), KeyboardButton('๐ค แกแแฑแฌแแทแบ')],
|
| 118 |
+
[KeyboardButton('๐ Coins แแแบแแแบ'), KeyboardButton('๐ Reset')],
|
| 119 |
+
]
|
| 120 |
+
if s.get('is_admin'):
|
| 121 |
+
rows.append([KeyboardButton('๐ Admin')])
|
| 122 |
+
rows.append([KeyboardButton('๐ช Logout')])
|
| 123 |
+
return ReplyKeyboardMarkup(rows, resize_keyboard=True)
|
| 124 |
+
|
| 125 |
+
def ensure_whisper():
|
| 126 |
+
with _wm_lock:
|
| 127 |
+
if _whisper_model[0] is None and whisper_mod:
|
| 128 |
+
_whisper_model[0] = whisper_mod.load_model('tiny', device='cpu')
|
| 129 |
+
return _whisper_model[0]
|
| 130 |
+
|
| 131 |
+
def settings_kb(cid):
|
| 132 |
+
s = sess(cid)
|
| 133 |
+
crop_labels = {'original':'๐ฌ Original','9:16':'๐ฑ 9:16','16:9':'๐ฅ๏ธ 16:9','1:1':'โฌ 1:1'}
|
| 134 |
+
return InlineKeyboardMarkup([
|
| 135 |
+
[InlineKeyboardButton(f"๐ Crop: {crop_labels.get(s['crop'],s['crop'])}", callback_data='set|crop')],
|
| 136 |
+
[InlineKeyboardButton(f"๐ค AI: {s['ai_model']}", callback_data='set|ai'),
|
| 137 |
+
InlineKeyboardButton(f"๐บ {s['content_type'].split('/')[0]}", callback_data='set|ct')],
|
| 138 |
+
[InlineKeyboardButton(f"Flip: {'ON' if s['flip'] else 'OFF'}", callback_data='set|flip'),
|
| 139 |
+
InlineKeyboardButton(f"Color: {'ON' if s['color'] else 'OFF'}", callback_data='set|color')],
|
| 140 |
+
[InlineKeyboardButton(f"Speed: {s['speed']}%", callback_data='set|speed')],
|
| 141 |
+
[InlineKeyboardButton(f"Watermark: {s['watermark'] or 'แแแพแญ'}", callback_data='set|wmk')],
|
| 142 |
+
[InlineKeyboardButton(f"๐ต BG Music: {'โ
แแซแแแบ' if s.get('music_file_id') else 'โ แแแซ'}", callback_data='set|music'),
|
| 143 |
+
InlineKeyboardButton('๐๏ธ Music แแปแแบ', callback_data='set|music_del')],
|
| 144 |
+
[InlineKeyboardButton('โ
แแญแแบแธแแแบ', callback_data='set|done')],
|
| 145 |
+
])
|
| 146 |
+
|
| 147 |
+
def voice_kb():
|
| 148 |
+
btns = [[InlineKeyboardButton('โโ Microsoft TTS โโ', callback_data='noop')]]
|
| 149 |
+
row = []
|
| 150 |
+
for name, vid, eng in MS_VOICES:
|
| 151 |
+
row.append(InlineKeyboardButton(name, callback_data=f'voice|{vid}|{eng}'))
|
| 152 |
+
btns.append(row)
|
| 153 |
+
btns.append([InlineKeyboardButton('โโ Gemini TTS โโ', callback_data='noop')])
|
| 154 |
+
row = []
|
| 155 |
+
for name, vid, eng in GEMINI_VOICES:
|
| 156 |
+
row.append(InlineKeyboardButton(name, callback_data=f'voice|{vid}|{eng}'))
|
| 157 |
+
if len(row) == 3:
|
| 158 |
+
btns.append(row); row = []
|
| 159 |
+
if row: btns.append(row)
|
| 160 |
+
return InlineKeyboardMarkup(btns)
|
| 161 |
+
|
| 162 |
+
def package_kb():
|
| 163 |
+
btns = []
|
| 164 |
+
for coins, price, desc in PACKAGES:
|
| 165 |
+
btns.append([InlineKeyboardButton(
|
| 166 |
+
f"๐ช {coins} Coins โ {price:,} MMK ({desc})",
|
| 167 |
+
callback_data=f'pkg|{coins}|{price}'
|
| 168 |
+
)])
|
| 169 |
+
return InlineKeyboardMarkup(btns)
|
| 170 |
+
|
| 171 |
+
PAYMENT_INFO = (
|
| 172 |
+
"๐ณ *แแฝแฑแแฝแฒแแแบแธ*\n\n"
|
| 173 |
+
"๐ฑ KBZPay / Wave / AYA Pay\n\n"
|
| 174 |
+
"๐ค *Phoe Shan*\n"
|
| 175 |
+
"๐ *09679871352* (Kapy)\n\n"
|
| 176 |
+
"แแฝแฑแแฝแฒแแผแฎแธแแฑแฌแแบ slip แแญแฏ Admin แแถ แแญแฏแทแแฑแธแแซ"
|
| 177 |
+
)
|
| 178 |
+
|
| 179 |
+
def cancel_kb():
|
| 180 |
+
return InlineKeyboardMarkup([[InlineKeyboardButton('โ แแปแแบแแญแแบแธแแแบ', callback_data='cancel|process')]])
|
| 181 |
+
|
| 182 |
+
# โโ AUTH โโ
|
| 183 |
+
|
| 184 |
+
async def cmd_start(update: Update, ctx: ContextTypes.DEFAULT_TYPE):
|
| 185 |
+
cid = update.effective_chat.id
|
| 186 |
+
if is_logged(cid):
|
| 187 |
+
s = sess(cid)
|
| 188 |
+
await update.message.reply_text(
|
| 189 |
+
f"๐ แแผแแบแแฌแแแทแบแกแแฝแแบ แแผแญแฏแแญแฏแแซแแแบ *{s['username']}*!\n๐ช Coins: *{fmt_coins(s['coins'])}*",
|
| 190 |
+
parse_mode=ParseMode.MARKDOWN, reply_markup=main_kb(cid))
|
| 191 |
+
return ST_MAIN
|
| 192 |
+
await update.message.reply_text(
|
| 193 |
+
"๐ฌ *Recap Studio Bot*\n\n"
|
| 194 |
+
"AI-Powered Video Recap Tool\n\n"
|
| 195 |
+
"โโโโโโโโโโโโโโโโโโโ\n"
|
| 196 |
+
"๐จโ๐ป *Developer* โ @PhoeShan2001\n"
|
| 197 |
+
"โโโโโโโโโโโโโโโโโโโ\n\n"
|
| 198 |
+
"แแแทแบ *username* แแแทแบแแซ โ",
|
| 199 |
+
parse_mode=ParseMode.MARKDOWN,
|
| 200 |
+
reply_markup=ReplyKeyboardMarkup(
|
| 201 |
+
[[KeyboardButton('/start')]],
|
| 202 |
+
resize_keyboard=True
|
| 203 |
+
))
|
| 204 |
+
return ST_LOGIN_USER
|
| 205 |
+
|
| 206 |
+
async def recv_login_user(update: Update, ctx: ContextTypes.DEFAULT_TYPE):
|
| 207 |
+
ctx.user_data['login_user'] = update.message.text.strip()
|
| 208 |
+
await update.message.reply_text("๐ *Password* แแแทแบแแซ\n_(แแแพแญแแแบ `-` แแญแฏแทแแซ)_", parse_mode=ParseMode.MARKDOWN)
|
| 209 |
+
return ST_LOGIN_PASS
|
| 210 |
+
|
| 211 |
+
async def recv_login_pass(update: Update, ctx: ContextTypes.DEFAULT_TYPE):
|
| 212 |
+
cid = update.effective_chat.id
|
| 213 |
+
u = ctx.user_data.get('login_user', '')
|
| 214 |
+
p = update.message.text.strip()
|
| 215 |
+
if p == '-': p = ''
|
| 216 |
+
ok, msg, coins = login_user(u, p)
|
| 217 |
+
if not ok:
|
| 218 |
+
await update.message.reply_text(f"โ {msg}\n\n*Username* แแแบแแแทแบแแซ โ", parse_mode=ParseMode.MARKDOWN)
|
| 219 |
+
return ST_LOGIN_USER
|
| 220 |
+
s = sess(cid)
|
| 221 |
+
s['username'] = u; s['coins'] = coins; s['is_admin'] = (u == ADMIN_U)
|
| 222 |
+
# Save Telegram chat ID for payment approve notifications
|
| 223 |
+
try:
|
| 224 |
+
db = load_db(); db['users'][u]['tg_chat_id'] = cid; save_db(db)
|
| 225 |
+
except: pass
|
| 226 |
+
await update.message.reply_text(
|
| 227 |
+
f"โ
*{u}* แกแแฑแแผแแทแบ แแแบแแฑแฌแแบแแผแฎแธแแซแแผแฎ\n๐ช Coins: *{fmt_coins(coins)}*",
|
| 228 |
+
parse_mode=ParseMode.MARKDOWN, reply_markup=main_kb(cid))
|
| 229 |
+
return ST_MAIN
|
| 230 |
+
|
| 231 |
+
async def cmd_logout(update: Update, ctx: ContextTypes.DEFAULT_TYPE):
|
| 232 |
+
sessions.pop(update.effective_chat.id, None)
|
| 233 |
+
await update.message.reply_text("๐ Logout แแฏแแบแแผแฎแธแแซแแผแฎ\n/start แแพแญแแบแแผแฎแธ แแผแแบแแแบแแซ",
|
| 234 |
+
reply_markup=ReplyKeyboardMarkup([[]], resize_keyboard=True))
|
| 235 |
+
return ConversationHandler.END
|
| 236 |
+
|
| 237 |
+
# โโ MENU BUTTONS โโ
|
| 238 |
+
|
| 239 |
+
async def btn_auto_process(update: Update, ctx: ContextTypes.DEFAULT_TYPE):
|
| 240 |
+
cid = update.effective_chat.id
|
| 241 |
+
if not is_logged(cid):
|
| 242 |
+
await update.message.reply_text("โ /start แแพแญแแบแแผแฎแธ แฆแธแ
แฝแฌ login แแแบแแซ")
|
| 243 |
+
return ST_MAIN
|
| 244 |
+
s = sess(cid)
|
| 245 |
+
if s['coins'] != -1 and s['coins'] < 2:
|
| 246 |
+
await update.message.reply_text(
|
| 247 |
+
f"โ Coins แแแฏแถแแฑแฌแแบแแฐแธ\nแแแทแบแแพแฌ *{s['coins']}* แแพแญแแแบ โ *2* แแญแฏแแแบ\n\n๐ Coins แแแบแแแบ แแแฏแแบแแพแญแแบแแซ",
|
| 248 |
+
parse_mode=ParseMode.MARKDOWN, reply_markup=main_kb(cid))
|
| 249 |
+
return ST_MAIN
|
| 250 |
+
cancel_flags[cid] = False
|
| 251 |
+
await update.message.reply_text(
|
| 252 |
+
"๐ฅ *Auto Process* โ 2 Coins แแฏแแบแแแบ\n\n"
|
| 253 |
+
"แกแฑแฌแแบแแซ link แแ
แบแแฏแแฏ แแญแฏแทแแซ แแญแฏแทแแแฏแแบ Video File แแแบแแซ โ\n\n"
|
| 254 |
+
"โข YouTube\nโข TikTok\nโข Facebook\nโข Instagram\n\n"
|
| 255 |
+
"_(แแปแแบแแญแแบแธแแญแฏแแแบ โ แแพแญแแบแแซ)_",
|
| 256 |
+
parse_mode=ParseMode.MARKDOWN,
|
| 257 |
+
reply_markup=InlineKeyboardMarkup([[InlineKeyboardButton('โ แแปแแบแแญแแบแธแแแบ', callback_data='cancel|await')]]))
|
| 258 |
+
return ST_AWAIT_VIDEO
|
| 259 |
+
|
| 260 |
+
async def btn_voice(update: Update, ctx: ContextTypes.DEFAULT_TYPE):
|
| 261 |
+
cid = update.effective_chat.id
|
| 262 |
+
s = sess(cid)
|
| 263 |
+
await update.message.reply_text(
|
| 264 |
+
f"๐ *แกแแถ แแฝแฑแธแแปแแบแแแบ*\n\nแแแบแแพแญ โ *{s['voice']}* ({s['engine'].upper()})",
|
| 265 |
+
parse_mode=ParseMode.MARKDOWN, reply_markup=voice_kb())
|
| 266 |
+
return ST_MAIN
|
| 267 |
+
|
| 268 |
+
async def btn_settings(update: Update, ctx: ContextTypes.DEFAULT_TYPE):
|
| 269 |
+
cid = update.effective_chat.id
|
| 270 |
+
await update.message.reply_text("โ๏ธ *Settings*", parse_mode=ParseMode.MARKDOWN, reply_markup=settings_kb(cid))
|
| 271 |
+
return ST_MAIN
|
| 272 |
+
|
| 273 |
+
async def btn_account(update: Update, ctx: ContextTypes.DEFAULT_TYPE):
|
| 274 |
+
cid = update.effective_chat.id
|
| 275 |
+
if not is_logged(cid):
|
| 276 |
+
await update.message.reply_text("โ /start แแพแญแแบแแซ")
|
| 277 |
+
return ST_MAIN
|
| 278 |
+
s = sess(cid); db = load_db(); u = db['users'].get(s['username'], {})
|
| 279 |
+
await update.message.reply_text(
|
| 280 |
+
f"๐ค *แแญแฏแแทแบแกแแฑแฌแแทแบ*\n\nUsername: `{s['username']}`\n๐ช Coins: *{fmt_coins(s['coins'])}*\n"
|
| 281 |
+
f"๐ฌ Video: {u.get('total_videos',0)} แแผแญแแบ\n๐ Transcript: {u.get('total_transcripts',0)} แแผแญแแบ\n"
|
| 282 |
+
f"๐
แ
แแแบแแฑแฌแแฑแท: {u.get('created_at','')[:10]}",
|
| 283 |
+
parse_mode=ParseMode.MARKDOWN, reply_markup=main_kb(cid))
|
| 284 |
+
return ST_MAIN
|
| 285 |
+
|
| 286 |
+
async def btn_reset(update: Update, ctx: ContextTypes.DEFAULT_TYPE):
|
| 287 |
+
cid = update.effective_chat.id
|
| 288 |
+
# Cancel any running process
|
| 289 |
+
if is_processing(cid):
|
| 290 |
+
cancel_flags[cid] = True
|
| 291 |
+
# Clear session state but keep login
|
| 292 |
+
s = sess(cid)
|
| 293 |
+
s['pending_url'] = None
|
| 294 |
+
s['pending_file_id'] = None
|
| 295 |
+
await update.message.reply_text(
|
| 296 |
+
"๐ *Reset แแฏแแบแแผแฎแธแแซแแผแฎ*\n\nแแฌแแฏแแบแแแฒ?",
|
| 297 |
+
parse_mode=ParseMode.MARKDOWN,
|
| 298 |
+
reply_markup=main_kb(cid)
|
| 299 |
+
)
|
| 300 |
+
return ST_MAIN
|
| 301 |
+
|
| 302 |
+
async def btn_buy(update: Update, ctx: ContextTypes.DEFAULT_TYPE):
|
| 303 |
+
cid = update.effective_chat.id
|
| 304 |
+
s = sess(cid)
|
| 305 |
+
await update.message.reply_text(
|
| 306 |
+
f"๐ *Coins แแแบแแแบ*\n\n"
|
| 307 |
+
f"แแแบแแพแญ Coins: *{fmt_coins(s['coins'])}*\n\n"
|
| 308 |
+
f"{PAYMENT_INFO}\n\n"
|
| 309 |
+
f"Package แแ
แบแแฏ แแฝแฑแธแแซ โ",
|
| 310 |
+
parse_mode=ParseMode.MARKDOWN, reply_markup=package_kb())
|
| 311 |
+
return ST_MAIN
|
| 312 |
+
|
| 313 |
+
async def btn_admin(update: Update, ctx: ContextTypes.DEFAULT_TYPE):
|
| 314 |
+
cid = update.effective_chat.id
|
| 315 |
+
s = sess(cid)
|
| 316 |
+
if not s.get('is_admin'):
|
| 317 |
+
await update.message.reply_text("โ Admin แแฌ แแแบแแฝแแทแบแแพแญแแแบ")
|
| 318 |
+
return ST_MAIN
|
| 319 |
+
db = load_db(); users = db.get('users', {})
|
| 320 |
+
lines = [f"๐ *Admin Panel* โ {len(users)} แแฑแฌแแบ\n"]
|
| 321 |
+
for uname, udata in list(users.items())[:20]:
|
| 322 |
+
lines.append(f"โข `{uname}` โ ๐ช{udata.get('coins',0)}")
|
| 323 |
+
await update.message.reply_text('\n'.join(lines), parse_mode=ParseMode.MARKDOWN,
|
| 324 |
+
reply_markup=InlineKeyboardMarkup([
|
| 325 |
+
[InlineKeyboardButton('โ User แแแบแแฎแธ', callback_data='adm|create'),
|
| 326 |
+
InlineKeyboardButton('๐ช Coins แแแทแบ', callback_data='adm|coins')],
|
| 327 |
+
[InlineKeyboardButton('๐๏ธ User แแปแแบ', callback_data='adm|delete'),
|
| 328 |
+
InlineKeyboardButton('๐ Refresh', callback_data='adm|refresh')],
|
| 329 |
+
]))
|
| 330 |
+
return ST_MAIN
|
| 331 |
+
|
| 332 |
+
# โโ VIDEO INPUT โโ
|
| 333 |
+
|
| 334 |
+
async def recv_video_input(update: Update, ctx: ContextTypes.DEFAULT_TYPE):
|
| 335 |
+
cid = update.effective_chat.id
|
| 336 |
+
s = sess(cid)
|
| 337 |
+
if cancel_flags.get(cid):
|
| 338 |
+
await update.message.reply_text("โ แแปแแบแแญแแบแธแแผแฎแธแแซแแผแฎ", reply_markup=main_kb(cid))
|
| 339 |
+
return ST_MAIN
|
| 340 |
+
if update.message.text:
|
| 341 |
+
url = update.message.text.strip()
|
| 342 |
+
if not re.match(r'https?://', url):
|
| 343 |
+
await update.message.reply_text("โ URL แแแพแแบแแฐแธ\nYouTube / TikTok / Facebook / Instagram link แแญแฏแทแแซ")
|
| 344 |
+
return ST_AWAIT_VIDEO
|
| 345 |
+
s['pending_url'] = url; s['pending_file_id'] = None
|
| 346 |
+
elif update.message.video or update.message.document:
|
| 347 |
+
media = update.message.video or update.message.document
|
| 348 |
+
s['pending_file_id'] = media.file_id; s['pending_url'] = None
|
| 349 |
+
else:
|
| 350 |
+
await update.message.reply_text("โ URL แแญแฏแทแแแฏแแบ Video File แแญแฏแทแแซ")
|
| 351 |
+
return ST_AWAIT_VIDEO
|
| 352 |
+
# โโ Reject if already processing โโ
|
| 353 |
+
if is_processing(cid):
|
| 354 |
+
await update.message.reply_text(
|
| 355 |
+
"โณ *แแฏแแบแแฑแฌแแบแแฑแแฒแแผแ
แบแแแบ*\n\nแแผแฎแธแแพ แแแบแแญแฏแทแแซ",
|
| 356 |
+
parse_mode=ParseMode.MARKDOWN
|
| 357 |
+
)
|
| 358 |
+
return ST_MAIN
|
| 359 |
+
|
| 360 |
+
# โโ Coin check before starting โโ
|
| 361 |
+
fresh_coins = get_coins(s['username'])
|
| 362 |
+
if fresh_coins is not None:
|
| 363 |
+
s['coins'] = fresh_coins
|
| 364 |
+
if not s['is_admin'] and s['coins'] != -1 and s['coins'] < 2:
|
| 365 |
+
await update.message.reply_text(
|
| 366 |
+
f"โ *Coins แแแฏแถแแฑแฌแแบแแฐแธ*\n\n"
|
| 367 |
+
f"แแแทแบแแพแฌ *{s['coins']}* แแพแญแแแบ โ *2* แแญแฏแแแบ\n\n"
|
| 368 |
+
f"๐ Coins แแแบแแแบ แแแฏแแบแแพแญแแบแแซ",
|
| 369 |
+
parse_mode=ParseMode.MARKDOWN, reply_markup=main_kb(cid)
|
| 370 |
+
)
|
| 371 |
+
return ST_MAIN
|
| 372 |
|
| 373 |
+
cancel_flags[cid] = False
|
| 374 |
+
prog_msg = await update.message.reply_text(
|
| 375 |
+
"โณ *แ
แแแบแแฑแแซแแแบโฆ* แแแ
แฑแฌแแทแบแแซ",
|
| 376 |
+
parse_mode=ParseMode.MARKDOWN, reply_markup=cancel_kb()
|
| 377 |
+
)
|
| 378 |
+
start_job(ctx.bot, cid, s.get('pending_url'), s.get('pending_file_id'), prog_msg.message_id)
|
| 379 |
+
return ST_MAIN
|
| 380 |
|
| 381 |
+
# โโ PROCESSING โโ
|
|
|
|
|
|
|
| 382 |
|
| 383 |
+
def _process_thread(bot, cid, prog_msg_id, pending_url=None, pending_file_id=None):
|
| 384 |
+
set_processing(cid, True)
|
| 385 |
+
import asyncio as _asyncio
|
| 386 |
+
loop = _asyncio.new_event_loop()
|
| 387 |
+
_asyncio.set_event_loop(loop)
|
| 388 |
try:
|
| 389 |
+
loop.run_until_complete(_do_process(bot, cid, prog_msg_id, pending_url, pending_file_id))
|
| 390 |
+
finally:
|
| 391 |
+
loop.close()
|
| 392 |
+
set_processing(cid, False)
|
| 393 |
+
|
| 394 |
+
async def _do_process(bot, cid, prog_msg_id, pending_url=None, pending_file_id=None):
|
| 395 |
+
s = sess(cid)
|
| 396 |
+
tid = uuid.uuid4().hex[:8]
|
| 397 |
+
tmp_dir = str(BASE_DIR / f'temp_{tid}')
|
| 398 |
+
out_file = str(OUTPUT_DIR / f'final_{tid}.mp4')
|
| 399 |
+
os.makedirs(tmp_dir, exist_ok=True)
|
| 400 |
+
|
| 401 |
+
async def prog(text, show_cancel=False):
|
| 402 |
+
try:
|
| 403 |
+
await bot.edit_message_text(chat_id=cid, message_id=prog_msg_id, text=text,
|
| 404 |
+
parse_mode=ParseMode.MARKDOWN, reply_markup=cancel_kb() if show_cancel else None)
|
| 405 |
+
except Exception: pass
|
| 406 |
|
| 407 |
+
def is_cancelled(): return cancel_flags.get(cid, False)
|
|
|
|
|
|
|
| 408 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 409 |
try:
|
| 410 |
+
# โโ Coin check at start of each job (important for queued jobs) โโ
|
| 411 |
+
if not s['is_admin']:
|
| 412 |
+
fresh = get_coins(s['username'])
|
| 413 |
+
if fresh is not None:
|
| 414 |
+
s['coins'] = fresh
|
| 415 |
+
if s['coins'] != -1 and s['coins'] < 2:
|
| 416 |
+
await prog(
|
| 417 |
+
f"โ *Coins แแแฏแถแแฑแฌแแบแแฐแธ*\n\n"
|
| 418 |
+
f"แแแทแบแแพแฌ *{fmt_coins(s['coins'])}* แแพแญแแแบ โ *2* แแญแฏแแแบ\n"
|
| 419 |
+
f"๐ Coins แแแบแแผแฎแธแแฑแฌแแบ แแแบแแผแญแฏแธแ
แฌแธแแซ"
|
| 420 |
+
)
|
| 421 |
+
return
|
| 422 |
+
|
| 423 |
+
await prog("๐ฅ *แกแแแทแบ 1/5* โ Video แแฑแซแแบแธแแฏแแบ แแฏแแบแแฑแแแบโฆ", show_cancel=True)
|
| 424 |
+
vpath = None
|
| 425 |
+
if pending_file_id:
|
| 426 |
+
file_obj = await bot.get_file(pending_file_id)
|
| 427 |
+
vpath = f'{tmp_dir}/input.mp4'
|
| 428 |
+
await file_obj.download_to_drive(vpath)
|
| 429 |
+
elif pending_url:
|
| 430 |
+
# cpu_queue_wait()
|
| 431 |
+
if is_cancelled(): await prog("โ แแปแแบแแญแแบแธแแผแฎแธแแซแแผแฎ"); return
|
| 432 |
+
ytdlp_download(f'{tmp_dir}/input.%(ext)s', pending_url)
|
| 433 |
+
found = glob.glob(f'{tmp_dir}/input.*')
|
| 434 |
+
if found: vpath = found[0]
|
| 435 |
+
if not vpath or not os.path.exists(vpath):
|
| 436 |
+
await prog("โ Video แแฑแซแแบแธแแฏแแบ แแกแฑแฌแแบแแผแแบแแซ"); return
|
| 437 |
+
if is_cancelled(): await prog("โ แแปแแบแแญแแบแธแแผแฎแธแแซแแผแฎ"); return
|
| 438 |
+
|
| 439 |
+
# Download background music if set
|
| 440 |
+
mpath = None
|
| 441 |
+
if s.get('music_file_id'):
|
| 442 |
+
try:
|
| 443 |
+
mfile = await bot.get_file(s['music_file_id'])
|
| 444 |
+
mpath = f'{tmp_dir}/bgmusic.mp3'
|
| 445 |
+
await mfile.download_to_drive(mpath)
|
| 446 |
+
except Exception:
|
| 447 |
+
mpath = None
|
| 448 |
+
|
| 449 |
+
await prog("๐๏ธ *แกแแแทแบ 2/5* โ Whisper แแผแแทแบ แแฐแธแแฐแแฑแแแบโฆ", show_cancel=True)
|
| 450 |
+
if whisper_mod is None: await prog("โ Whisper แ install แแแฑแธแแซ"); return
|
| 451 |
+
model = ensure_whisper()
|
| 452 |
+
res = run_stage('whisper', model.transcribe, 'bot', lambda p,m: None, 'โณ Whisper แ
แฑแฌแแทแบแแฑแแแบ', '๐๏ธ Transcribingโฆ', vpath, fp16=False)
|
| 453 |
+
tr = res['text']; src_lang = res.get('language', 'en')
|
| 454 |
+
if is_cancelled(): await prog("โ แแปแแบแแญแแบแธแแผแฎแธแแซแแผแฎ"); return
|
| 455 |
+
|
| 456 |
+
await prog(f"๐ค *แกแแแทแบ 3/5* โ {s['ai_model']} แแผแแทแบ Script แแฑแธแแฑแแแบโฆ", show_cancel=True)
|
| 457 |
+
sys_p = SYS_MED if s['content_type'] == 'Medical/Health' else SYS_MOVIE
|
| 458 |
+
sys_p = sys_p + '\n' + NUM_TO_MM_RULE
|
| 459 |
+
out_txt, _ = run_stage('ai', call_api, 'bot', lambda p,m: None, 'โณ AI แ
แฑแฌแแทแบแแฑแแแบ', '๐ค AI Scriptโฆ', [{'role':'system','content':sys_p},{'role':'user','content':f'Language:{src_lang}\n\n{tr}'}], api=s['ai_model'])
|
| 460 |
+
sc, caption_text, hashtags = parse_out(out_txt)
|
| 461 |
+
if is_cancelled(): await prog("โ แแปแแบแแญแแบแธแแผแฎแธแแซแแผแฎ"); return
|
| 462 |
+
|
| 463 |
+
await prog("๐ *แกแแแทแบ 4/5* โ แกแแถ แแฏแแบแแฑแแแบโฆ", show_cancel=True)
|
| 464 |
+
sentences = split_txt(sc)
|
| 465 |
+
import asyncio as _aio, functools as _ft
|
| 466 |
+
loop = _aio.get_running_loop()
|
| 467 |
+
if s['engine'] == 'gemini':
|
| 468 |
+
parts = await loop.run_in_executor(None, _ft.partial(
|
| 469 |
+
run_stage, 'tts', run_gemini_tts_sync, 'bot', lambda p,m: None, 'โณ TTS แ
แฑแฌแแทแบแแฑแแแบ', '๐ TTSโฆ',
|
| 470 |
+
sentences, s['voice'], tmp_dir, speed=s['speed']))
|
| 471 |
+
else:
|
| 472 |
+
parts = await loop.run_in_executor(None, _ft.partial(
|
| 473 |
+
run_stage, 'tts', run_tts_sync, 'bot', lambda p,m: None, 'โณ TTS แ
แฑแฌแแทแบแแฑแแแบ', '๐ TTSโฆ',
|
| 474 |
+
sentences, s['voice'], f'+{s["speed"]}%', tmp_dir))
|
| 475 |
+
cmb = f'{tmp_dir}/combined.mp3'; lst = f'{tmp_dir}/list.txt'
|
| 476 |
+
with open(lst, 'w') as lf:
|
| 477 |
+
for a in parts: lf.write(f"file '{os.path.abspath(a)}'\n")
|
| 478 |
+
subprocess.run(f'ffmpeg -y -f concat -safe 0 -i "{lst}" -af "silenceremove=start_periods=1:stop_periods=-1:stop_duration=0.1:stop_threshold=-50dB" -c:a libmp3lame -q:a 2 "{cmb}"', shell=True, check=True)
|
| 479 |
+
if is_cancelled(): await prog("โ แแปแแบแแญแแบแธแแผแฎแธแแซแแผแฎ"); return
|
| 480 |
+
|
| 481 |
+
await prog("๐ฌ *แกแแแทแบ 5/5* โ Video แแฑแซแแบแธแ
แแบแแฑแแแบโฆ")
|
| 482 |
+
vd = dur(vpath); ad = dur(cmb)
|
| 483 |
+
if vd <= 0: await prog("โ Video duration แแแบแแแแซ"); return
|
| 484 |
+
if ad <= 0: await prog("โ Audio duration แแแบแแแแซ"); return
|
| 485 |
+
_build_video(vpath, cmb, mpath, ad, vd, s['crop'], s['flip'], s['color'], s['watermark'], out_file)
|
| 486 |
+
|
| 487 |
+
if not s['is_admin']:
|
| 488 |
+
ok2, rem = deduct(s['username'], 2)
|
| 489 |
+
if ok2:
|
| 490 |
+
s['coins'] = rem; upd_stat(s['username'], 'tr'); upd_stat(s['username'], 'vd')
|
| 491 |
+
|
| 492 |
+
await prog("โ
แแผแฎแธแแซแแผแฎ! แแญแฏแทแแฑแแแบโฆ")
|
| 493 |
+
file_size = os.path.getsize(out_file)
|
| 494 |
+
caption = (
|
| 495 |
+
f"๐ฌ *{caption_text}*\n\n{hashtags}\n\n"
|
| 496 |
+
f"๐ช แแปแแบ Coins: *{fmt_coins(s['coins'])}*"
|
| 497 |
+
)
|
| 498 |
+
|
| 499 |
+
send_file = out_file # file to actually send
|
| 500 |
+
|
| 501 |
+
if file_size > TG_MAX_FILE_BYTES:
|
| 502 |
+
size_mb = file_size / (1024 * 1024)
|
| 503 |
+
await prog(f"๐ฆ File แแผแฎแธแแฑแแแบ ({size_mb:.1f} MB) โ Compress แแฏแแบแแฑแแแบโฆ")
|
| 504 |
+
compressed_file = out_file.replace('.mp4', '_compressed.mp4')
|
| 505 |
+
try:
|
| 506 |
+
# Two-pass target: aim for 45 MB to stay safely under 49 MB limit
|
| 507 |
+
target_size_kb = 45 * 1024
|
| 508 |
+
# Get video duration for bitrate calculation
|
| 509 |
+
probe = subprocess.run(
|
| 510 |
+
['ffprobe', '-v', 'error', '-show_entries', 'format=duration',
|
| 511 |
+
'-of', 'default=noprint_wrappers=1:nokey=1', out_file],
|
| 512 |
+
capture_output=True, text=True
|
| 513 |
+
)
|
| 514 |
+
video_duration = float(probe.stdout.strip()) if probe.stdout.strip() else 60.0
|
| 515 |
+
# Target total bitrate (kbps), reserve 128kbps for audio
|
| 516 |
+
target_total_kbps = int((target_size_kb * 8) / video_duration)
|
| 517 |
+
video_kbps = max(target_total_kbps - 128, 200)
|
| 518 |
+
compress_cmd = (
|
| 519 |
+
f'ffmpeg -y -i "{out_file}" '
|
| 520 |
+
f'-c:v libx264 -b:v {video_kbps}k -preset fast -maxrate {video_kbps*2}k -bufsize {video_kbps*4}k '
|
| 521 |
+
f'-c:a aac -b:a 128k '
|
| 522 |
+
f'"{compressed_file}"'
|
| 523 |
+
)
|
| 524 |
+
subprocess.run(compress_cmd, shell=True, check=True,
|
| 525 |
+
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
| 526 |
+
compressed_size = os.path.getsize(compressed_file)
|
| 527 |
+
if compressed_size <= TG_MAX_FILE_BYTES:
|
| 528 |
+
send_file = compressed_file
|
| 529 |
+
logger.info(f"[{tid}] Compressed {size_mb:.1f}MB โ {compressed_size/1024/1024:.1f}MB")
|
| 530 |
+
else:
|
| 531 |
+
logger.warning(f"[{tid}] Compress แ๏ฟฝ๏ฟฝแฑแฌแแบแแผแแบ โ {compressed_size/1024/1024:.1f}MB แแปแแบแแฑแแฑแธแแแบ")
|
| 532 |
+
send_file = None # fall through to link
|
| 533 |
+
except Exception as ce:
|
| 534 |
+
logger.warning(f"[{tid}] Compress error: {ce}")
|
| 535 |
+
send_file = None
|
| 536 |
+
|
| 537 |
+
if send_file is None:
|
| 538 |
+
# Compression failed or still too large โ send link
|
| 539 |
+
file_name = Path(out_file).name
|
| 540 |
+
dl_url = f"{WEB_BASE_URL}/outputs/{file_name}"
|
| 541 |
+
await bot.send_message(
|
| 542 |
+
chat_id=cid,
|
| 543 |
+
text=(
|
| 544 |
+
f"๐ฌ *{caption_text}*\n\n"
|
| 545 |
+
f"{hashtags}\n\n"
|
| 546 |
+
f"๐ฆ File แแผแฎแธแแฑแฌแแผแฑแฌแแทแบ ({size_mb:.1f} MB) แแญแฏแแบแแญแฏแแบ แแญแฏแทแแแแแซ\n\n"
|
| 547 |
+
f"๐ *Download Link:*\n`{dl_url}`\n\n"
|
| 548 |
+
f"๐ช แแปแแบ Coins: *{fmt_coins(s['coins'])}*"
|
| 549 |
+
),
|
| 550 |
+
parse_mode=ParseMode.MARKDOWN,
|
| 551 |
+
)
|
| 552 |
+
send_file = None # skip video send below
|
| 553 |
+
|
| 554 |
+
if send_file is not None:
|
| 555 |
+
with open(send_file, 'rb') as vf:
|
| 556 |
+
await bot.send_video(
|
| 557 |
+
chat_id=cid, video=vf, caption=caption,
|
| 558 |
+
parse_mode=ParseMode.MARKDOWN, supports_streaming=True,
|
| 559 |
+
read_timeout=300, write_timeout=300,
|
| 560 |
+
)
|
| 561 |
+
await bot.delete_message(chat_id=cid, message_id=prog_msg_id)
|
| 562 |
except Exception as e:
|
| 563 |
+
logger.exception(f'[{tid}] Error: {e}')
|
| 564 |
+
await prog(f"โ Error: {e}")
|
| 565 |
+
finally:
|
| 566 |
+
shutil.rmtree(tmp_dir, ignore_errors=True)
|
| 567 |
+
s['pending_url'] = None; s['pending_file_id'] = None
|
| 568 |
+
cancel_flags.pop(cid, None)
|
| 569 |
+
|
| 570 |
+
# โโ CALLBACKS โโ
|
| 571 |
+
|
| 572 |
+
async def on_callback(update: Update, ctx: ContextTypes.DEFAULT_TYPE):
|
| 573 |
+
q = update.callback_query
|
| 574 |
+
cid = q.message.chat_id
|
| 575 |
+
s = sess(cid)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 576 |
try:
|
| 577 |
+
await q.answer()
|
| 578 |
+
except Exception:
|
| 579 |
+
pass
|
| 580 |
+
data = q.data
|
| 581 |
+
|
| 582 |
+
if data == 'noop': return
|
| 583 |
+
|
| 584 |
+
# โโ Admin payment approve / reject (from admin chat) โโ
|
| 585 |
+
if data.startswith('adm_pay|'):
|
| 586 |
+
parts = data.split('|')
|
| 587 |
+
action = parts[1] # approve / reject
|
| 588 |
+
payment_id = parts[2]
|
| 589 |
+
username = parts[3]
|
| 590 |
+
from datetime import datetime as _dt
|
| 591 |
+
pdb = load_payments_db()
|
| 592 |
+
pay = next((p for p in pdb['payments'] if p['id'] == payment_id), None)
|
| 593 |
+
if not pay:
|
| 594 |
+
await q.answer("โ Payment แแแฝแฑแทแแซ", show_alert=True)
|
| 595 |
+
return
|
| 596 |
+
if pay['status'] != 'pending':
|
| 597 |
+
await q.answer(f"โ ๏ธ แแฎ payment {pay['status']} แแผแ
แบแแผแฎแธแแผแฎ", show_alert=True)
|
| 598 |
+
return
|
| 599 |
+
if action == 'approve':
|
| 600 |
+
coins = int(parts[4])
|
| 601 |
+
pay['status'] = 'approved'
|
| 602 |
+
pay['updated_at'] = _dt.now().isoformat()
|
| 603 |
+
save_payments_db(pdb)
|
| 604 |
+
# Add coins to user
|
| 605 |
+
add_coins_fn(username, coins, 'bot_admin')
|
| 606 |
+
await q.edit_message_caption(
|
| 607 |
+
caption=q.message.caption + f"\n\nโ
<b>Approved!</b> +{coins} coins โ {username}",
|
| 608 |
+
parse_mode=ParseMode.HTML
|
| 609 |
+
)
|
| 610 |
+
# Notify user if online (best effort)
|
| 611 |
+
try:
|
| 612 |
+
db = load_db()
|
| 613 |
+
tg_cid = db['users'].get(username, {}).get('tg_chat_id')
|
| 614 |
+
if tg_cid:
|
| 615 |
+
await ctx.bot.send_message(
|
| 616 |
+
chat_id=tg_cid,
|
| 617 |
+
text=f"โ
*Coins แแแทแบแแผแฎแธแแซแแผแฎ!*\n\n๐ช *+{coins} Coins* แแแทแบแกแแฑแฌแแทแบแแฒ แแฑแฌแแบแแผแฎ\n๐ ID: `{payment_id}`",
|
| 618 |
+
parse_mode=ParseMode.MARKDOWN
|
| 619 |
+
)
|
| 620 |
+
except: pass
|
| 621 |
+
else:
|
| 622 |
+
pay['status'] = 'rejected'
|
| 623 |
+
pay['updated_at'] = _dt.now().isoformat()
|
| 624 |
+
save_payments_db(pdb)
|
| 625 |
+
await q.edit_message_caption(
|
| 626 |
+
caption=q.message.caption + f"\n\nโ <b>Rejected</b>",
|
| 627 |
+
parse_mode=ParseMode.HTML
|
| 628 |
+
)
|
| 629 |
+
try:
|
| 630 |
+
db = load_db()
|
| 631 |
+
tg_cid = db['users'].get(username, {}).get('tg_chat_id')
|
| 632 |
+
if tg_cid:
|
| 633 |
+
await ctx.bot.send_message(
|
| 634 |
+
chat_id=tg_cid,
|
| 635 |
+
text=f"โ *Payment แแผแแบแธแแแบแแผแแบแธแแถแแแแบ*\n\n๐ ID: `{payment_id}`\n\nแแฑแธแแผแแบแธแแแบ โ @{ADMIN_TG_USERNAME}",
|
| 636 |
+
parse_mode=ParseMode.MARKDOWN
|
| 637 |
+
)
|
| 638 |
+
except: pass
|
| 639 |
+
await q.answer()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 640 |
return
|
| 641 |
+
|
| 642 |
+
if data.startswith('cancel|'):
|
| 643 |
+
cancel_flags[cid] = True
|
| 644 |
+
await q.edit_message_text("โ แแปแแบแแญแแบแธแแฑแแแบโฆ" if data == 'cancel|process' else "โ แแปแแบแแญแแบแธแแผแฎแธแแซแแผแฎ")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 645 |
return
|
| 646 |
|
| 647 |
+
if data.startswith('pkg|'):
|
| 648 |
+
if data == 'pkg|back':
|
| 649 |
+
await q.edit_message_text(f"๐ *Coins แแแบแแแบ*\n\nPackage แแฝแฑแธแแซ โ", parse_mode=ParseMode.MARKDOWN, reply_markup=package_kb())
|
| 650 |
+
return
|
| 651 |
+
_, coins, price = data.split('|')
|
| 652 |
+
coins = int(coins); price = int(price)
|
| 653 |
+
# Save selected package for slip upload
|
| 654 |
+
pending_payment[cid] = {'coins': coins, 'price': price}
|
| 655 |
+
ctx.user_data['await_slip'] = True
|
| 656 |
+
await q.edit_message_text(
|
| 657 |
+
f"๐ช *{coins} Coins* โ {price:,} MMK\n\n"
|
| 658 |
+
f"{PAYMENT_INFO}\n\n"
|
| 659 |
+
f"๐ธ แแฝแฑแแฝแฒแแผแฎแธแแฑแฌแแบ *Slip แแฌแแบแแฏแถ* แแฎ chat แแฒ แแญแฏแแบแแญแฏแแบแแญแฏแทแแซ",
|
| 660 |
+
parse_mode=ParseMode.MARKDOWN,
|
| 661 |
+
reply_markup=InlineKeyboardMarkup([
|
| 662 |
+
[InlineKeyboardButton('๐ แแผแแบแแฝแฌแธแแแบ', callback_data='pkg|back')],
|
| 663 |
+
]))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 664 |
return
|
| 665 |
|
| 666 |
+
if data.startswith('voice|'):
|
| 667 |
+
_, vid, eng = data.split('|')
|
| 668 |
+
s['voice'] = vid; s['engine'] = eng
|
| 669 |
+
await q.edit_message_text(f"โ
แกแแถ แแแบแแพแแบแแผแฎแธ โ *{vid}* ({'Microsoft' if eng=='ms' else 'Gemini'})", parse_mode=ParseMode.MARKDOWN)
|
| 670 |
return
|
| 671 |
|
| 672 |
+
if data.startswith('set|'):
|
| 673 |
+
key = data.split('|')[1]
|
| 674 |
+
if key == 'flip': s['flip'] = not s['flip']
|
| 675 |
+
elif key == 'color': s['color'] = not s['color']
|
| 676 |
+
elif key == 'crop':
|
| 677 |
+
crops = ['original','9:16','16:9','1:1']
|
| 678 |
+
s['crop'] = crops[(crops.index(s['crop'])+1) % len(crops)] if s['crop'] in crops else 'original'
|
| 679 |
+
elif key == 'ai': s['ai_model'] = 'DeepSeek' if s['ai_model'] == 'Gemini' else 'Gemini'
|
| 680 |
+
elif key == 'ct': s['content_type'] = 'Medical/Health' if s['content_type'] == 'Movie Recap' else 'Movie Recap'
|
| 681 |
+
elif key == 'speed':
|
| 682 |
+
speeds = [0,10,20,30,40,50,60,80]
|
| 683 |
+
cur = s['speed']
|
| 684 |
+
s['speed'] = speeds[(speeds.index(cur)+1) % len(speeds)] if cur in speeds else 30
|
| 685 |
+
elif key == 'wmk':
|
| 686 |
+
ctx.user_data['await_wmk'] = True
|
| 687 |
+
await q.edit_message_text("๐ง Watermark แ
แฌแแฌแธแแญแฏแทแแซ (แฅแแแฌ โ @username):")
|
| 688 |
+
return
|
| 689 |
+
elif key == 'music':
|
| 690 |
+
ctx.user_data['await_music'] = True
|
| 691 |
+
await q.edit_message_text(
|
| 692 |
+
"๐ต *Background Music*\n\n"
|
| 693 |
+
"MP3 / Audio file แแแบแแซ\n"
|
| 694 |
+
"_(แแญแฏแแบแแแบแแผแฎแธแแฑแฌแแบ auto แแญแแบแธแแแบ)_",
|
| 695 |
+
parse_mode=ParseMode.MARKDOWN)
|
| 696 |
+
return
|
| 697 |
+
elif key == 'music_del':
|
| 698 |
+
s['music_file_id'] = None
|
| 699 |
+
await q.answer("๐๏ธ Music แแปแแบแแผแฎแธ", show_alert=False)
|
| 700 |
+
elif key == 'done':
|
| 701 |
+
await q.edit_message_text("โ
Settings แแญแแบแธแแผแฎแธแแซแแผแฎ"); return
|
| 702 |
+
await q.edit_message_reply_markup(reply_markup=settings_kb(cid))
|
| 703 |
return
|
| 704 |
|
| 705 |
+
if data.startswith('adm|'):
|
| 706 |
+
if not s.get('is_admin'): await q.edit_message_text("โ Admin only"); return
|
| 707 |
+
action = data.split('|')[1]
|
| 708 |
+
if action == 'refresh':
|
| 709 |
+
db = load_db(); users = db.get('users', {})
|
| 710 |
+
lines = [f"๐ *Admin Panel* โ {len(users)} แแฑแฌแแบ\n"]
|
| 711 |
+
for uname, udata in list(users.items())[:20]:
|
| 712 |
+
lines.append(f"โข `{uname}` โ ๐ช{udata.get('coins',0)}")
|
| 713 |
+
await q.edit_message_text('\n'.join(lines), parse_mode=ParseMode.MARKDOWN,
|
| 714 |
+
reply_markup=InlineKeyboardMarkup([
|
| 715 |
+
[InlineKeyboardButton('โ User แแแบแแฎแธ', callback_data='adm|create'),
|
| 716 |
+
InlineKeyboardButton('๐ช Coins แแแทแบ', callback_data='adm|coins')],
|
| 717 |
+
[InlineKeyboardButton('๐๏ธ User แแปแแบ', callback_data='adm|delete'),
|
| 718 |
+
InlineKeyboardButton('๐ Refresh', callback_data='adm|refresh')],
|
| 719 |
+
]))
|
| 720 |
+
elif action == 'create':
|
| 721 |
+
ctx.user_data['adm_action'] = 'create'
|
| 722 |
+
await q.edit_message_text("โ *User แแแบแแฎแธแแแบ*\n\n`username coins` แแญแฏแทแแซ\nแฅแแแฌ โ `CoolUser 10`", parse_mode=ParseMode.MARKDOWN)
|
| 723 |
+
elif action == 'coins':
|
| 724 |
+
ctx.user_data['adm_action'] = 'coins'
|
| 725 |
+
await q.edit_message_text("๐ช *Coins แ
แฎแแถแแแบ*\n\n`username amount add|set` แแญแฏแทแแซ\nแฅแแแฌ โ `CoolUser 20 add`", parse_mode=ParseMode.MARKDOWN)
|
| 726 |
+
elif action == 'delete':
|
| 727 |
+
ctx.user_data['adm_action'] = 'delete'
|
| 728 |
+
await q.edit_message_text("๐๏ธ แแปแแบแแแทแบ *username* แแญแฏแทแแซ โ", parse_mode=ParseMode.MARKDOWN)
|
| 729 |
+
return
|
| 730 |
|
| 731 |
+
# โโ TEXT HANDLER โโ
|
| 732 |
+
|
| 733 |
+
# โโ MUSIC INPUT HANDLER โโ
|
| 734 |
+
|
| 735 |
+
async def recv_music_input(update: Update, ctx: ContextTypes.DEFAULT_TYPE):
|
| 736 |
+
cid = update.effective_chat.id
|
| 737 |
+
s = sess(cid)
|
| 738 |
+
if not ctx.user_data.get('await_music'):
|
| 739 |
+
# Not expecting music โ ignore audio uploads
|
| 740 |
+
return ST_MAIN
|
| 741 |
+
media = update.message.audio or update.message.document
|
| 742 |
+
if not media:
|
| 743 |
+
await update.message.reply_text("โ Audio file แแแฏแแบแแซ")
|
| 744 |
+
return ST_MAIN
|
| 745 |
+
s['music_file_id'] = media.file_id
|
| 746 |
+
ctx.user_data['await_music'] = False
|
| 747 |
+
await update.message.reply_text(
|
| 748 |
+
"โ
Background music แแญแแบแธแแผแฎแธแแซแแผแฎ\n"
|
| 749 |
+
"โ๏ธ Settings แแฒแแพแฌ แแผแแฑแแแบ",
|
| 750 |
+
reply_markup=main_kb(cid)
|
| 751 |
+
)
|
| 752 |
+
return ST_MAIN
|
| 753 |
+
|
| 754 |
+
|
| 755 |
+
async def recv_slip_photo(update: Update, ctx: ContextTypes.DEFAULT_TYPE):
|
| 756 |
+
"""Handle payment slip photo upload โ save pending payment โ notify admin."""
|
| 757 |
+
cid = update.effective_chat.id
|
| 758 |
+
s = sess(cid)
|
| 759 |
+
|
| 760 |
+
if not ctx.user_data.get('await_slip'):
|
| 761 |
+
# Not expecting slip โ treat as video if it's a photo? Just ignore.
|
| 762 |
+
await update.message.reply_text("โ Slip แแแปแพแฑแฌแบแแแทแบแแฌแธแแซแ Package แกแแแบแแฝแฑแธแแซ ๐", reply_markup=main_kb(cid))
|
| 763 |
+
return ST_MAIN
|
| 764 |
+
|
| 765 |
+
if not update.message.photo:
|
| 766 |
+
await update.message.reply_text("โ แแฌแแบแแฏแถ (Photo) แแญแฏแทแแซ โ Document แแแฏแแบแแซแแฒแท", reply_markup=main_kb(cid))
|
| 767 |
+
return ST_MAIN
|
| 768 |
+
|
| 769 |
+
pkg = pending_payment.get(cid)
|
| 770 |
+
if not pkg:
|
| 771 |
+
await update.message.reply_text("โ Package แแแฝแฑแธแแแฑแธแแซ โ Coins แแแบแแแบ แแญแฏแแพแญแแบแแซ", reply_markup=main_kb(cid))
|
| 772 |
+
ctx.user_data['await_slip'] = False
|
| 773 |
+
return ST_MAIN
|
| 774 |
+
|
| 775 |
+
coins = pkg['coins']
|
| 776 |
+
price = pkg['price']
|
| 777 |
+
username = s.get('username', '')
|
| 778 |
+
|
| 779 |
+
# Download slip photo (largest size)
|
| 780 |
+
photo = update.message.photo[-1]
|
| 781 |
+
file = await ctx.bot.get_file(photo.file_id)
|
| 782 |
+
|
| 783 |
+
import io, base64, urllib.request as _ur
|
| 784 |
+
buf = io.BytesIO()
|
| 785 |
+
await file.download_to_memory(buf)
|
| 786 |
+
slip_b64 = base64.b64encode(buf.getvalue()).decode()
|
| 787 |
+
slip_data_url = f"data:image/jpeg;base64,{slip_b64}"
|
| 788 |
+
|
| 789 |
+
# Save to payments_db
|
| 790 |
+
import uuid as _uuid
|
| 791 |
+
from datetime import datetime as _dt
|
| 792 |
+
payment_id = _uuid.uuid4().hex[:10]
|
| 793 |
+
now = _dt.now().isoformat()
|
| 794 |
+
pdb = load_payments_db()
|
| 795 |
+
pdb['payments'].append({
|
| 796 |
+
'id': payment_id,
|
| 797 |
+
'username': username,
|
| 798 |
+
'coins': coins,
|
| 799 |
+
'price': price,
|
| 800 |
+
'status': 'pending',
|
| 801 |
+
'created_at': now,
|
| 802 |
+
'updated_at': now,
|
| 803 |
+
'slip_image': slip_data_url,
|
| 804 |
+
'admin_note': '',
|
| 805 |
+
})
|
| 806 |
+
save_payments_db(pdb)
|
| 807 |
+
|
| 808 |
+
ctx.user_data['await_slip'] = False
|
| 809 |
+
pending_payment.pop(cid, None)
|
| 810 |
+
|
| 811 |
+
# Notify admin via Telegram (send slip photo directly)
|
| 812 |
+
caption = (
|
| 813 |
+
f"๐ฐ <b>Payment Request</b>\n"
|
| 814 |
+
f"๐ค User: <code>{username}</code>\n"
|
| 815 |
+
f"๐ช Coins: {coins}\n"
|
| 816 |
+
f"๐ต Price: {price:,} MMK\n"
|
| 817 |
+
f"๐ ID: <code>{payment_id}</code>\n"
|
| 818 |
+
f"โฐ {now[:19]}\n\n"
|
| 819 |
+
f"โ
Approve:\n<code>/approve {payment_id} {username} {coins}</code>\n"
|
| 820 |
+
f"โ Reject:\n<code>/reject {payment_id}</code>"
|
| 821 |
+
)
|
| 822 |
|
| 823 |
+
if ADMIN_TELEGRAM_CHAT_ID and TELEGRAM_BOT_TOKEN:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 824 |
try:
|
| 825 |
+
buf.seek(0)
|
| 826 |
+
await ctx.bot.send_photo(
|
| 827 |
+
chat_id=ADMIN_TELEGRAM_CHAT_ID,
|
| 828 |
+
photo=buf,
|
| 829 |
+
caption=caption,
|
| 830 |
+
parse_mode=ParseMode.HTML,
|
| 831 |
+
reply_markup=InlineKeyboardMarkup([
|
| 832 |
+
[InlineKeyboardButton(f"โ
Approve {coins} coins", callback_data=f"adm_pay|approve|{payment_id}|{username}|{coins}")],
|
| 833 |
+
[InlineKeyboardButton("โ Reject", callback_data=f"adm_pay|reject|{payment_id}|{username}")],
|
| 834 |
+
])
|
| 835 |
+
)
|
| 836 |
except Exception as e:
|
| 837 |
+
logger.error(f"Admin notify failed: {e}")
|
| 838 |
+
|
| 839 |
+
await update.message.reply_text(
|
| 840 |
+
f"โ
*Slip แแแบแแถแแผแฎแธแแซแแผแฎ!*\n\n"
|
| 841 |
+
f"๐ช Package: *{coins} Coins* โ {price:,} MMK\n"
|
| 842 |
+
f"๐ ID: `{payment_id}`\n\n"
|
| 843 |
+
f"Admin แ
แ
แบแแฑแธแแผแฎแธ Coins แแแทแบแแฑแธแแซแแแบ โณ",
|
| 844 |
+
parse_mode=ParseMode.MARKDOWN,
|
| 845 |
+
reply_markup=main_kb(cid)
|
| 846 |
+
)
|
| 847 |
+
return ST_MAIN
|
| 848 |
+
|
| 849 |
+
|
| 850 |
+
async def recv_text(update: Update, ctx: ContextTypes.DEFAULT_TYPE):
|
| 851 |
+
cid = update.effective_chat.id
|
| 852 |
+
text = update.message.text.strip()
|
| 853 |
+
s = sess(cid)
|
| 854 |
+
|
| 855 |
+
# Music file handled via recv_music_input โ text fallback
|
| 856 |
+
if ctx.user_data.get('await_wmk'):
|
| 857 |
+
s['watermark'] = text; ctx.user_data['await_wmk'] = False
|
| 858 |
+
await update.message.reply_text(f"โ
Watermark โ `{text}`", parse_mode=ParseMode.MARKDOWN, reply_markup=main_kb(cid))
|
| 859 |
+
return ST_MAIN
|
| 860 |
+
|
| 861 |
+
if ctx.user_data.get('adm_action') == 'create' and s.get('is_admin'):
|
| 862 |
+
parts = text.split()
|
| 863 |
+
uname = parts[0] if parts else ''
|
| 864 |
+
coins = int(parts[1]) if len(parts) > 1 and parts[1].isdigit() else 10
|
| 865 |
+
msg, created = create_user_fn(uname, coins, s['username'])
|
| 866 |
+
ctx.user_data.pop('adm_action', None)
|
| 867 |
+
await update.message.reply_text(
|
| 868 |
+
f"โ
User แแแบแแฎแธแแผแฎแธ!\nUsername: `{created}`\nCoins: {coins}" if created else f"โ {msg}",
|
| 869 |
+
parse_mode=ParseMode.MARKDOWN, reply_markup=main_kb(cid))
|
| 870 |
+
return ST_MAIN
|
| 871 |
+
|
| 872 |
+
if ctx.user_data.get('adm_action') == 'coins' and s.get('is_admin'):
|
| 873 |
+
parts = text.split()
|
| 874 |
+
if len(parts) >= 3:
|
| 875 |
+
msg = set_coins_fn(parts[0], int(parts[1])) if parts[2] == 'set' else add_coins_fn(parts[0], int(parts[1]))
|
| 876 |
+
ctx.user_data.pop('adm_action', None)
|
| 877 |
+
await update.message.reply_text(f"โ
{msg}", reply_markup=main_kb(cid))
|
| 878 |
+
else:
|
| 879 |
+
await update.message.reply_text("โ Format: `username amount add|set`", parse_mode=ParseMode.MARKDOWN)
|
| 880 |
+
return ST_MAIN
|
| 881 |
|
| 882 |
+
if ctx.user_data.get('adm_action') == 'delete' and s.get('is_admin'):
|
| 883 |
+
db = load_db()
|
| 884 |
+
if text in db['users']:
|
| 885 |
+
del db['users'][text]; save_db(db)
|
| 886 |
+
await update.message.reply_text(f"โ
`{text}` แแปแแบแแผแฎแธแแซแแผแฎ", parse_mode=ParseMode.MARKDOWN, reply_markup=main_kb(cid))
|
| 887 |
+
else:
|
| 888 |
+
await update.message.reply_text(f"โ `{text}` แแแฝแฑแทแแซ", parse_mode=ParseMode.MARKDOWN, reply_markup=main_kb(cid))
|
| 889 |
+
ctx.user_data.pop('adm_action', None)
|
| 890 |
+
return ST_MAIN
|
| 891 |
+
|
| 892 |
+
if re.match(r'https?://', text):
|
| 893 |
+
s['pending_url'] = text; s['pending_file_id'] = None
|
| 894 |
+
if s['coins'] != -1 and s['coins'] < 2:
|
| 895 |
+
await update.message.reply_text(f"โ Coins แแแฏแถแแฑแฌแแบแแฐแธ ({s['coins']}) โ 2 แแญแฏแแแบ")
|
| 896 |
+
return ST_MAIN
|
| 897 |
+
# Busy check
|
| 898 |
+
if is_processing(cid):
|
| 899 |
+
await update.message.reply_text(
|
| 900 |
+
"โ *Process แแฏแแบแแฑแแฒแแผแ
แบแแแบ*\n\n"
|
| 901 |
+
"แแแบแแพแญ video แแผแฎแธแแพ แแฑแฌแแบแแ
แบแแฏ แแญแฏแทแแซ\n"
|
| 902 |
+
"_(แแปแแบแแญแแบแธแแญแฏแแแบ โ แแพแญแแบแแซ)_",
|
| 903 |
+
parse_mode=ParseMode.MARKDOWN,
|
| 904 |
+
reply_markup=InlineKeyboardMarkup([[InlineKeyboardButton('โ แแปแแบแแญแแบแธแแแบ', callback_data='cancel|process')]])
|
| 905 |
+
)
|
| 906 |
+
return ST_MAIN
|
| 907 |
+
# Coin check
|
| 908 |
+
fresh_coins2 = get_coins(s['username'])
|
| 909 |
+
if fresh_coins2 is not None:
|
| 910 |
+
s['coins'] = fresh_coins2
|
| 911 |
+
if not s['is_admin'] and s['coins'] != -1 and s['coins'] < 2:
|
| 912 |
+
await update.message.reply_text(
|
| 913 |
+
f"โ Coins แแแฏแถแแฑแฌแแบแแฐแธ ({s['coins']}) โ 2 แแญแฏแแแบ"
|
| 914 |
+
)
|
| 915 |
+
return ST_MAIN
|
| 916 |
+
cancel_flags[cid] = False
|
| 917 |
+
prog_msg = await update.message.reply_text(
|
| 918 |
+
"โณ *แ
แแแบแแฑแแซแแแบโฆ*",
|
| 919 |
+
parse_mode=ParseMode.MARKDOWN, reply_markup=cancel_kb()
|
| 920 |
+
)
|
| 921 |
+
start_job(ctx.bot, cid, text, None, prog_msg.message_id)
|
| 922 |
+
return ST_MAIN
|
| 923 |
+
|
| 924 |
+
await update.message.reply_text("แแฌแแฏแแบแแแฒ?", reply_markup=main_kb(cid))
|
| 925 |
+
return ST_MAIN
|
| 926 |
|
| 927 |
+
# โโ MAIN โโ
|
| 928 |
|
| 929 |
def main():
|
|
|
|
| 930 |
if not BOT_TOKEN:
|
| 931 |
+
logger.error('โ TELEGRAM_BOT_TOKEN แแแแบ๏ฟฝ๏ฟฝแพแแบแแแฑแธแแซ!'); return
|
| 932 |
+
|
| 933 |
+
application = Application.builder().token(BOT_TOKEN).build()
|
| 934 |
+
conv = ConversationHandler(
|
| 935 |
+
entry_points=[CommandHandler('start', cmd_start)],
|
| 936 |
+
states={
|
| 937 |
+
ST_LOGIN_USER: [MessageHandler(filters.TEXT & ~filters.COMMAND, recv_login_user)],
|
| 938 |
+
ST_LOGIN_PASS: [MessageHandler(filters.TEXT & ~filters.COMMAND, recv_login_pass)],
|
| 939 |
+
ST_MAIN: [
|
| 940 |
+
MessageHandler(filters.Regex('^๐ฌ Auto Process$'), btn_auto_process),
|
| 941 |
+
MessageHandler(filters.Regex('^๐ แกแแถ$'), btn_voice),
|
| 942 |
+
MessageHandler(filters.Regex('^โ๏ธ Settings$'), btn_settings),
|
| 943 |
+
MessageHandler(filters.Regex('^๐ค แกแแฑแฌแแทแบ$'), btn_account),
|
| 944 |
+
MessageHandler(filters.Regex('^๐ Coins แแแบแแแบ$'), btn_buy),
|
| 945 |
+
MessageHandler(filters.Regex('^๐ Reset$'), btn_reset),
|
| 946 |
+
MessageHandler(filters.Regex('^๐ Admin$'), btn_admin),
|
| 947 |
+
MessageHandler(filters.Regex('^๐ช Logout$'), cmd_logout),
|
| 948 |
+
MessageHandler(filters.VIDEO | filters.Document.VIDEO, recv_video_input),
|
| 949 |
+
MessageHandler(filters.AUDIO | filters.Document.AUDIO, recv_music_input),
|
| 950 |
+
MessageHandler(filters.PHOTO, recv_slip_photo),
|
| 951 |
+
MessageHandler(filters.TEXT & ~filters.COMMAND, recv_text),
|
| 952 |
+
CallbackQueryHandler(on_callback),
|
| 953 |
+
],
|
| 954 |
+
ST_AWAIT_VIDEO: [
|
| 955 |
+
MessageHandler(filters.TEXT & ~filters.COMMAND, recv_video_input),
|
| 956 |
+
MessageHandler(filters.VIDEO | filters.Document.VIDEO, recv_video_input),
|
| 957 |
+
CallbackQueryHandler(on_callback),
|
| 958 |
+
],
|
| 959 |
+
},
|
| 960 |
+
fallbacks=[CommandHandler('start', cmd_start), CommandHandler('logout', cmd_logout)],
|
| 961 |
+
per_chat=True, allow_reentry=True,
|
| 962 |
+
)
|
| 963 |
+
application.add_handler(conv)
|
| 964 |
+
logger.info('๐ค Recap Studio Bot แกแแแทแบแแผแ
แบแแผแฎ!')
|
| 965 |
+
application.run_polling(drop_pending_updates=True)
|
| 966 |
|
| 967 |
if __name__ == '__main__':
|
| 968 |
main()
|