gittobl1 / bot.py
o7c1's picture
Update bot.py
c64dc61 verified
Raw History Blame Contribute Delete
22.9 kB
# bot.py
import os
import re
import json
import asyncio
import sqlite3
import aiohttp
import httpx
import logging
import tempfile
import zipfile
import math
from urllib.parse import unquote
# ---------- Logging ----------
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
logger = logging.getLogger(__name__)
# ---------- Import tokens and allowed users ----------
# 1. Try environment variables (used on Hugging Face / production)
BALE_TOKEN = os.getenv("BALE_BOT_TOKEN")
GITHUB_TOKEN = os.getenv("GITHUB_TOKEN") # can be None
allowed_raw = os.getenv("ALLOWED_USERS") # comma-separated chat IDs, e.g. "447422307,123456789"
if allowed_raw:
ALLOWED_USERS = [int(uid.strip()) for uid in allowed_raw.split(",") if uid.strip()]
else:
ALLOWED_USERS = [] # empty = no restrictions (or fallback to config.py)
# 2. If BALE_TOKEN is missing, fall back to config.py (local development)
if not BALE_TOKEN:
try:
from config import BALE_BOT_TOKEN as BALE_TOKEN, GITHUB_TOKEN, ALLOWED_USERS
logger.info("Tokens and allowed users loaded from config.py")
except ImportError:
logger.error("❌ BALE_BOT_TOKEN not set in environment or config.py!")
raise ValueError("BALE_BOT_TOKEN missing")
# 3. Final safety check
if not BALE_TOKEN:
raise ValueError("❌ BALE_BOT_TOKEN not set!")
# 4. For GitHub token and allowed users, if still empty after the env check, try config.py as fallback
if not GITHUB_TOKEN:
try:
from config import GITHUB_TOKEN
except ImportError:
pass
if not ALLOWED_USERS:
try:
from config import ALLOWED_USERS
except ImportError:
pass
# 5. Log the final state
logger.info(f"Allowed users: {ALLOWED_USERS if ALLOWED_USERS else 'All users permitted'}")
GITHUB_API = "https://api.github.com"
BALE_API = f"https://tapi.bale.ai/bot{BALE_TOKEN}/"
DB_FILE = "subscriptions.db"
github_headers = {"Accept": "application/vnd.github.v3+json"}
if GITHUB_TOKEN:
github_headers["Authorization"] = f"Bearer {GITHUB_TOKEN}"
# ---------- Database ----------
def init_db():
conn = sqlite3.connect(DB_FILE)
c = conn.cursor()
c.execute("""
CREATE TABLE IF NOT EXISTS subscriptions (
chat_id TEXT NOT NULL,
repo_url TEXT NOT NULL,
last_tag TEXT,
PRIMARY KEY (chat_id, repo_url)
)
""")
conn.commit()
conn.close()
def add_subscription(chat_id: str, repo_url: str):
conn = sqlite3.connect(DB_FILE)
conn.execute(
"INSERT OR IGNORE INTO subscriptions (chat_id, repo_url) VALUES (?, ?)",
(chat_id, repo_url)
)
conn.commit()
conn.close()
def get_all_distinct_repos():
conn = sqlite3.connect(DB_FILE)
rows = conn.execute("SELECT DISTINCT repo_url FROM subscriptions").fetchall()
conn.close()
return [row[0] for row in rows]
def get_chats_to_notify(repo_url: str, new_tag: str):
conn = sqlite3.connect(DB_FILE)
rows = conn.execute(
"SELECT chat_id FROM subscriptions WHERE repo_url = ? AND (last_tag IS NULL OR last_tag != ?)",
(repo_url, new_tag)
).fetchall()
chat_ids = [row[0] for row in rows]
if chat_ids:
conn.executemany(
"UPDATE subscriptions SET last_tag = ? WHERE chat_id = ? AND repo_url = ?",
[(new_tag, cid, repo_url) for cid in chat_ids]
)
conn.commit()
conn.close()
return chat_ids
# ---------- GitHub helpers ----------
def parse_repo_url(url: str):
match = re.search(r"github\.com/(?P<owner>[^/]+)/(?P<repo>[^/]+)", url)
if not match:
return None, None
return match.group("owner"), match.group("repo")
def convert_github_url_to_direct(url: str) -> str:
"""
Identifies if a URL is a direct file or source code release asset.
Converts browser blob/raw paths to direct, downloadable raw usercontent links.
"""
# Already a direct raw link
if "raw.githubusercontent.com" in url:
return url
# Release download file (github.com/owner/repo/releases/download/tag/filename)
if "/releases/download/" in url:
return url
# Source archive archive file (github.com/owner/repo/archive/...)
if "/archive/" in url:
return url
# Browser blob link (github.com/owner/repo/blob/branch/path) -> convert to raw usercontent
if "/blob/" in url:
return url.replace("github.com", "raw.githubusercontent.com").replace("/blob/", "/")
# Browser raw link (github.com/owner/repo/raw/branch/path) -> convert to raw usercontent
if "/raw/" in url:
return url.replace("github.com", "raw.githubusercontent.com").replace("/raw/", "/")
return None
async def fetch_latest_release(owner: str, repo: str):
"""Fetches the latest release chronologically by creation or publication date."""
url = f"{GITHUB_API}/repos/{owner}/{repo}/releases"
async with aiohttp.ClientSession(headers=github_headers) as session:
async with session.get(url) as resp:
if resp.status != 200:
logger.error(f"GitHub API error {resp.status} for {url}")
return None
releases = await resp.json()
if not releases:
return None
# Sort strictly by publication date or creation date descending (newest build time first)
try:
releases.sort(
key=lambda r: r.get("published_at") or r.get("created_at") or "",
reverse=True
)
except Exception as e:
logger.warning(f"Could not sort releases chronologically: {e}")
return releases[0]
# ---------- Bale API helpers ----------
async def bale_request(method: str, params: dict = None):
url = BALE_API + method
async with aiohttp.ClientSession() as session:
async with session.post(url, data=params) as resp:
result = await resp.json()
logger.debug(f"API response for {method}: {result}")
return result
async def send_message(chat_id, text, reply_markup=None):
params = {
"chat_id": chat_id,
"text": text,
"parse_mode": "Markdown"
}
if reply_markup:
params["reply_markup"] = json.dumps(reply_markup)
return await bale_request("sendMessage", params)
async def send_document_file(chat_id, file_path, caption="", display_filename=None):
if display_filename is None:
display_filename = os.path.basename(file_path)
url = BALE_API + "sendDocument"
data = {"chat_id": str(chat_id)}
if caption:
data["caption"] = caption
with open(file_path, "rb") as f:
files = {"document": (display_filename, f, "application/octet-stream")}
logger.debug(f"Uploading file as: {display_filename}, local path: {file_path}")
async with httpx.AsyncClient() as client:
resp = await client.post(url, data=data, files=files, timeout=120)
if resp.status_code != 200:
logger.error(f"❌ Bale upload failed: {resp.status_code} {resp.text}")
return {"ok": False, "description": resp.text}
result = resp.json()
logger.info(f"πŸ“¨ sendDocument (file) response: {result}")
return result
async def answer_callback(callback_id, text=""):
params = {
"callback_query_id": callback_id,
"text": text
}
return await bale_request("answerCallbackQuery", params)
def build_inline_keyboard(release: dict):
assets = release.get("assets", [])
buttons = []
# 1. Add Source Code (ZIP) option
zipball_url = release.get("zipball_url")
tag_name = release.get("tag_name", "latest")
if zipball_url:
buttons.append([{
"text": f"πŸ“¦ Source Code ({tag_name}.zip)",
"callback_data": f"dl:{zipball_url}"
}])
# 2. Add other pre-compiled release assets
row = []
for asset in assets:
btn = {
"text": asset["name"][:30],
"callback_data": f"dl:{asset['browser_download_url']}"
}
row.append(btn)
if len(row) == 2:
buttons.append(row)
row = []
if row:
buttons.append(row)
return {"inline_keyboard": buttons} if buttons else None
# ---------- File download helper ----------
async def download_file(url, dest_path):
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"}
# If downloading from GitHub's API endpoints, include credentials if available
if "api.github.com" in url and GITHUB_TOKEN:
headers["Authorization"] = f"Bearer {GITHUB_TOKEN}"
headers["Accept"] = "application/vnd.github.v3+json"
async with aiohttp.ClientSession(headers=headers) as session:
async with session.get(url, allow_redirects=True) as resp:
if resp.status != 200:
raise Exception(f"Download failed with status {resp.status}")
with open(dest_path, "wb") as f:
while True:
chunk = await resp.content.read(8192)
if not chunk:
break
f.write(chunk)
logger.info(f"βœ… Downloaded file to {dest_path}")
# ---------- Compression helpers ----------
COMPRESSED_EXTENSIONS = {
'.zip', '.gz', '.tgz', '.tar.gz', '.rar', '.7z',
'.bz2', '.xz', '.tar', '.tar.bz2', '.tar.xz', '.zst', '.lz'
}
def is_compressed(filename: str) -> bool:
lower = filename.lower()
for ext in COMPRESSED_EXTENSIONS:
if lower.endswith(ext):
return True
return False
def compress_to_zip(file_path: str, original_filename: str, zip_path: str):
with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zf:
zf.write(file_path, arcname=original_filename)
logger.info(f"πŸ“¦ Compressed to ZIP: {zip_path}")
# ---------- File splitting ----------
CHUNK_SIZE = 19 * 1024 * 1024 # 19 MB per chunk
def split_file(source_path: str, base_name: str, parts_dir: str):
"""
Splits a file into chunks.
Yields (chunk_path, part_number, total_parts).
chunk_path is the raw binary chunk.
"""
file_size = os.path.getsize(source_path)
total_parts = math.ceil(file_size / CHUNK_SIZE)
part_num = 1
with open(source_path, 'rb') as f:
while True:
chunk_data = f.read(CHUNK_SIZE)
if not chunk_data:
break
chunk_name = f"{base_name}.part{part_num}"
chunk_path = os.path.join(parts_dir, chunk_name)
with open(chunk_path, 'wb') as chunk_file:
chunk_file.write(chunk_data)
logger.info(f"βœ‚οΈ Created part {part_num}/{total_parts}: {chunk_name}")
yield chunk_path, part_num, total_parts
part_num += 1
# ---------- File Processing Engine ----------
async def download_and_send_file(chat_id: int, url: str, status_msg_id_to_edit: int = None) -> bool:
"""Downloads a file from url, compresses it if needed, splits if large, and uploads to Bale."""
try:
raw_filename = url.split("/")[-1].split("?")[0]
original_filename = unquote(raw_filename)
# Ensure ZIP files downloaded from the API have their proper extension
if ("zipball" in url or "tarball" in url or "/archive/" in url) and not original_filename.endswith(".zip"):
original_filename = f"{original_filename}.zip"
except:
original_filename = "asset.tmp"
logger.info(f"⬇️ Processing download: {original_filename} from {url}")
status_message_id = status_msg_id_to_edit
if not status_message_id:
status_msg = await send_message(chat_id, "⏳ Downloading file...")
status_message_id = status_msg.get("result", {}).get("message_id") if status_msg.get("ok") else None
tmp = tempfile.NamedTemporaryFile(delete=False, suffix="_" + original_filename)
tmp_path = tmp.name
try:
await download_file(url, tmp_path)
file_size = os.path.getsize(tmp_path)
size_mb = file_size / (1024 * 1024)
logger.info(f"βœ… Downloaded: {size_mb:.2f} MB")
# Compress if not already compressed
need_compress = not is_compressed(original_filename)
if need_compress:
zip_filename = original_filename + ".zip"
zip_tmp = tempfile.NamedTemporaryFile(delete=False, suffix="_" + zip_filename)
zip_path = zip_tmp.name
zip_tmp.close()
compress_to_zip(tmp_path, original_filename, zip_path)
os.unlink(tmp_path) # remove the original
tmp_path = zip_path
upload_filename = zip_filename
caption_prefix = f"πŸ“¦ File compressed to ZIP for delivery.\nOriginal: `{original_filename}`"
else:
upload_filename = original_filename
caption_prefix = "Here you go!"
upload_size = os.path.getsize(tmp_path)
upload_size_mb = upload_size / (1024 * 1024)
# Decision: split or send directly
if upload_size > CHUNK_SIZE:
await send_message(chat_id, f"πŸ“¦ File is {upload_size_mb:.2f} MB – splitting into parts...")
if status_message_id:
await bale_request("deleteMessage", {"chat_id": chat_id, "message_id": status_message_id})
with tempfile.TemporaryDirectory() as parts_dir:
total_parts = math.ceil(upload_size / CHUNK_SIZE)
parts_sent = 0
for chunk_path, part_num, total in split_file(tmp_path, upload_filename, parts_dir):
combine_base = upload_filename if upload_filename else original_filename
caption = (
f"πŸ“¦ Part {part_num}/{total} of `{combine_base}`\n"
f"Original file: `{original_filename}`\n\n"
"After downloading all parts:\n"
f"1. Open **Command Prompt (cmd)** in the folder with the parts.\n"
f"2. Run:\n"
f" `copy /b {combine_base}.part* {combine_base}`\n"
f" (Linux/Mac: `cat {combine_base}.part* > {combine_base}`)\n"
"3. Extract the combined file to get the original asset."
)
result = await send_document_file(chat_id, chunk_path,
caption=caption,
display_filename=os.path.basename(chunk_path))
if not result.get("ok"):
await send_message(chat_id, f"❌ Failed to send part {part_num}. Error: {result.get('description')}")
break
parts_sent += 1
return parts_sent == total_parts
else:
# Small enough – send directly
if status_message_id:
await bale_request("editMessageText", {
"chat_id": chat_id,
"message_id": status_message_id,
"text": f"πŸ“₯ Downloaded {upload_size_mb:.2f} MB – uploading to Bale..."
})
else:
await send_message(chat_id, f"πŸ“₯ Downloaded {upload_size_mb:.2f} MB – uploading to Bale...")
result = await send_document_file(chat_id, tmp_path,
caption=caption_prefix,
display_filename=upload_filename)
if result.get("ok"):
if status_message_id:
await bale_request("deleteMessage", {"chat_id": chat_id, "message_id": status_message_id})
return True
else:
raw_error = result.get("description", str(result))
await send_message(chat_id, f"❌ Upload failed. Bale response:\n{raw_error}")
return False
except Exception as e:
logger.error(f"❌ Download/Upload error: {e}", exc_info=True)
await send_message(chat_id, f"❌ Error: {e}")
return False
finally:
try:
os.unlink(tmp_path)
except Exception:
pass
# ---------- Message / Callback handlers ----------
def is_authorized(chat_id: int) -> bool:
if not ALLOWED_USERS:
return True
return chat_id in ALLOWED_USERS
async def handle_message(message: dict):
chat_id = message["chat"]["id"]
if not is_authorized(chat_id):
await send_message(chat_id, "β›” You are not authorized to use this bot.")
return
text = message.get("text", "").strip()
# Identify if a user sent a GitHub link (either a repository page, raw link, or blob link)
if ("github.com/" in text or "raw.githubusercontent.com" in text) and not text.startswith("/"):
direct_url = convert_github_url_to_direct(text)
if direct_url:
logger.info(f"🎯 Direct link detected: {direct_url}. Bypassing menu.")
await download_and_send_file(chat_id, direct_url)
else:
# Standard on-demand release selection menu flow
await handle_on_demand(chat_id, text)
elif text.startswith("/watch"):
await handle_watch(chat_id, text)
else:
await send_message(chat_id,
"Send me a GitHub repository or release page URL (e.g., https://github.com/owner/repo) and I'll let you choose an asset.\n\n"
"Or send a direct link to a release asset, source archive, or single file from a repo, and I will download it directly.\n\n"
"Or use /watch <repo_url> to get notified of new releases."
)
async def handle_on_demand(chat_id: int, url: str):
if not is_authorized(chat_id):
return
owner, repo = parse_repo_url(url)
if not owner:
await send_message(chat_id, "That doesn't look like a valid GitHub URL.")
return
release = await fetch_latest_release(owner, repo)
if not release:
await send_message(chat_id, "Could not fetch releases. The repo may be private or empty.")
return
keyboard = build_inline_keyboard(release)
if keyboard:
await send_message(chat_id, f"πŸ“¦ *{release['name']}* – Choose an asset:", reply_markup=keyboard)
else:
await send_message(chat_id, "No assets found in the latest release.")
async def handle_watch(chat_id: int, text: str):
if not is_authorized(chat_id):
return
parts = text.split(maxsplit=1)
if len(parts) < 2:
await send_message(chat_id, "Usage: /watch <github_repo_url>")
return
repo_url = parts[1].strip()
owner, repo = parse_repo_url(repo_url)
if not owner:
await send_message(chat_id, "Invalid GitHub URL.")
return
rel = await fetch_latest_release(owner, repo)
if not rel:
await send_message(chat_id, "Could not access releases. The repo might be private or have no releases.")
return
add_subscription(str(chat_id), repo_url)
await send_message(chat_id, f"βœ… Now watching {owner}/{repo} for new releases.\nLatest release: {rel['name']}")
async def handle_callback(callback: dict):
callback_id = callback["id"]
chat_id = callback["message"]["chat"]["id"]
if not is_authorized(chat_id):
await answer_callback(callback_id, "Unauthorized.")
return
data = callback.get("data", "")
logger.info(f"πŸ”˜ Callback received: data='{data}', chat_id={chat_id}")
if data.startswith("dl:"):
url = data[3:]
success = await download_and_send_file(chat_id, url)
if success:
await answer_callback(callback_id, "Sent!")
else:
await answer_callback(callback_id, "Failed.")
else:
await answer_callback(callback_id, "Unknown action.")
# ---------- Polling ----------
async def poll_updates():
offset = 0
logger.info("πŸ” Starting update polling...")
while True:
try:
params = {"offset": offset, "timeout": 30}
async with aiohttp.ClientSession() as session:
async with session.post(BALE_API + "getUpdates", data=params) as resp:
result = await resp.json()
if not result.get("ok"):
logger.error(f"getUpdates failed: {result}")
await asyncio.sleep(1)
continue
for update in result["result"]:
offset = update["update_id"] + 1
if "message" in update:
logger.info(f"πŸ“© Message from {update['message']['chat']['id']}: {update['message'].get('text', '[no text]')[:100]}")
try:
await handle_message(update["message"])
except Exception as e:
logger.error(f"Error handling message: {e}", exc_info=True)
elif "callback_query" in update:
try:
await handle_callback(update["callback_query"])
except Exception as e:
logger.error(f"Error in callback: {e}", exc_info=True)
except Exception as e:
logger.error(f"Polling error: {e}", exc_info=True)
await asyncio.sleep(5)
# ---------- Release poller ----------
async def release_poller():
logger.info("πŸ” Release poller started")
while True:
try:
repos = get_all_distinct_repos()
if repos:
logger.debug(f"Checking {len(repos)} watched repos")
for repo_url in repos:
owner, repo = parse_repo_url(repo_url)
if not owner:
continue
release = await fetch_latest_release(owner, repo)
if not release:
continue
new_tag = release["tag_name"]
chat_ids = get_chats_to_notify(repo_url, new_tag)
if not chat_ids:
continue
keyboard = build_inline_keyboard(release)
for cid in chat_ids:
if not is_authorized(int(cid)):
continue
try:
await send_message(int(cid), f"πŸš€ New release for {owner}/{repo}: *{release['name']}*", reply_markup=keyboard)
logger.info(f"πŸ”” Notified {cid} about {repo_url}")
except Exception as e:
logger.error(f"Failed to notify {cid}: {e}")
except Exception as e:
logger.error(f"Release poller error: {e}", exc_info=True)
await asyncio.sleep(300)
async def bot_main():
init_db()
await asyncio.gather(
poll_updates(),
release_poller()
)