|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Post a CI-built Android APK to the project Telegram channel on each |
| 4 | +release tag, followed by a reply-threaded changelog message with |
| 5 | +Persian + English bullets in <blockquote> blocks. |
| 6 | +
|
| 7 | +Called from the `telegram:` job in `.github/workflows/release.yml`. |
| 8 | +Environment: |
| 9 | + BOT_TOKEN Telegram bot token (repo secret TELEGRAM_BOT_TOKEN) |
| 10 | + CHAT_ID Numeric chat id, e.g. -1002282061190 (repo secret |
| 11 | + TELEGRAM_CHAT_ID) |
| 12 | +Arguments: |
| 13 | + --apk path to the APK file to upload |
| 14 | + --version bare version string, e.g. "1.1.0" |
| 15 | + --repo "owner/repo" |
| 16 | + --changelog path to docs/changelog/vX.Y.Z.md; split on a line |
| 17 | + that is exactly "---" — anything before is Persian, |
| 18 | + anything after is English. Missing file = only the |
| 19 | + APK is posted (no reply). |
| 20 | +
|
| 21 | +Why Python over curl: curl's `-F name=value` multipart spec treats |
| 22 | +`<file` as "read from file" and `@file` as "upload file". Our HTML |
| 23 | +captions contain literal `<b>` tags, which triggers the file-read |
| 24 | +path and exits 26 "Failed to open/read local data". urllib has no |
| 25 | +such behavior. |
| 26 | +
|
| 27 | +Telegram quirks we deliberately handle: |
| 28 | + - Captions max out at 1024 chars, so the APK caption is short |
| 29 | + (title + sha256 + repo + release URL) and the real changelog |
| 30 | + goes in a reply-threaded message (sendMessage has no practical |
| 31 | + length limit). |
| 32 | + - sendDocument content-type defaults to application/octet-stream |
| 33 | + for unknown extensions — we pass .apk with |
| 34 | + application/vnd.android.package-archive so channel previews |
| 35 | + label it as an Android package, not a generic file. |
| 36 | +""" |
| 37 | +import argparse |
| 38 | +import hashlib |
| 39 | +import http.client |
| 40 | +import json |
| 41 | +import os |
| 42 | +import re |
| 43 | +import ssl |
| 44 | +import sys |
| 45 | +import uuid |
| 46 | +from pathlib import Path |
| 47 | + |
| 48 | + |
| 49 | +def parse_changelog(path: str) -> tuple[str, str]: |
| 50 | + """Return (persian_body, english_body). Blank strings if file missing.""" |
| 51 | + p = Path(path) |
| 52 | + if not p.is_file(): |
| 53 | + return "", "" |
| 54 | + body = p.read_text(encoding="utf-8") |
| 55 | + # Strip a leading HTML comment block if present — the changelog |
| 56 | + # template uses <!-- ... --> to document the format for editors; |
| 57 | + # we don't want that echoed to Telegram. |
| 58 | + body = re.sub(r"^\s*<!--.*?-->\s*", "", body, count=1, flags=re.S) |
| 59 | + fa, sep, en = body.partition("\n---\n") |
| 60 | + if not sep: |
| 61 | + # No separator — treat everything as Persian (content-language |
| 62 | + # is a project preference rather than a hard rule). |
| 63 | + return body.strip(), "" |
| 64 | + return fa.strip(), en.strip() |
| 65 | + |
| 66 | + |
| 67 | +def sha256_of(path: str) -> str: |
| 68 | + h = hashlib.sha256() |
| 69 | + with open(path, "rb") as f: |
| 70 | + for chunk in iter(lambda: f.read(1024 * 1024), b""): |
| 71 | + h.update(chunk) |
| 72 | + return h.hexdigest() |
| 73 | + |
| 74 | + |
| 75 | +def tg_request(method: str, token: str, *, body: bytes, content_type: str) -> dict: |
| 76 | + """POST `body` to https://api.telegram.org/bot<token>/<method>.""" |
| 77 | + conn = http.client.HTTPSConnection( |
| 78 | + "api.telegram.org", context=ssl.create_default_context() |
| 79 | + ) |
| 80 | + conn.request( |
| 81 | + "POST", |
| 82 | + f"/bot{token}/{method}", |
| 83 | + body=body, |
| 84 | + headers={"Content-Type": content_type, "Content-Length": str(len(body))}, |
| 85 | + ) |
| 86 | + resp = conn.getresponse() |
| 87 | + raw = resp.read() |
| 88 | + try: |
| 89 | + data = json.loads(raw) |
| 90 | + except json.JSONDecodeError: |
| 91 | + raise SystemExit(f"Telegram {method}: non-JSON response ({resp.status}): {raw!r}") |
| 92 | + if not data.get("ok"): |
| 93 | + raise SystemExit(f"Telegram {method} failed: {data}") |
| 94 | + return data["result"] |
| 95 | + |
| 96 | + |
| 97 | +def send_document(token: str, chat_id: str, apk_path: str, caption: str) -> int: |
| 98 | + """Upload the APK file with a short HTML caption. Returns message_id.""" |
| 99 | + boundary = "----" + uuid.uuid4().hex |
| 100 | + with open(apk_path, "rb") as f: |
| 101 | + file_bytes = f.read() |
| 102 | + |
| 103 | + def text_field(name: str, value: str) -> bytes: |
| 104 | + return ( |
| 105 | + f"--{boundary}\r\n" |
| 106 | + f'Content-Disposition: form-data; name="{name}"\r\n\r\n' |
| 107 | + f"{value}\r\n" |
| 108 | + ).encode("utf-8") |
| 109 | + |
| 110 | + def file_field(name: str, filename: str, content: bytes) -> bytes: |
| 111 | + head = ( |
| 112 | + f"--{boundary}\r\n" |
| 113 | + f'Content-Disposition: form-data; name="{name}"; filename="{filename}"\r\n' |
| 114 | + # Proper MIME type — makes the Telegram client show the APK |
| 115 | + # with the Android package icon and honour its size/name. |
| 116 | + f"Content-Type: application/vnd.android.package-archive\r\n\r\n" |
| 117 | + ).encode("utf-8") |
| 118 | + return head + content + b"\r\n" |
| 119 | + |
| 120 | + body = ( |
| 121 | + text_field("chat_id", chat_id) |
| 122 | + + text_field("caption", caption) |
| 123 | + + text_field("parse_mode", "HTML") |
| 124 | + + file_field("document", os.path.basename(apk_path), file_bytes) |
| 125 | + + f"--{boundary}--\r\n".encode("utf-8") |
| 126 | + ) |
| 127 | + |
| 128 | + result = tg_request( |
| 129 | + "sendDocument", |
| 130 | + token, |
| 131 | + body=body, |
| 132 | + content_type=f"multipart/form-data; boundary={boundary}", |
| 133 | + ) |
| 134 | + return int(result["message_id"]) |
| 135 | + |
| 136 | + |
| 137 | +def send_reply(token: str, chat_id: str, text: str, reply_to: int) -> None: |
| 138 | + """Post a text message as a reply to the APK message.""" |
| 139 | + from urllib.parse import urlencode |
| 140 | + |
| 141 | + body = urlencode( |
| 142 | + { |
| 143 | + "chat_id": chat_id, |
| 144 | + "text": text, |
| 145 | + "parse_mode": "HTML", |
| 146 | + "reply_to_message_id": str(reply_to), |
| 147 | + } |
| 148 | + ).encode() |
| 149 | + tg_request( |
| 150 | + "sendMessage", |
| 151 | + token, |
| 152 | + body=body, |
| 153 | + content_type="application/x-www-form-urlencoded", |
| 154 | + ) |
| 155 | + |
| 156 | + |
| 157 | +def main() -> int: |
| 158 | + ap = argparse.ArgumentParser() |
| 159 | + ap.add_argument("--apk", required=True) |
| 160 | + ap.add_argument("--version", required=True) |
| 161 | + ap.add_argument("--repo", required=True) |
| 162 | + ap.add_argument("--changelog", required=True) |
| 163 | + args = ap.parse_args() |
| 164 | + |
| 165 | + token = os.environ.get("BOT_TOKEN", "") |
| 166 | + chat_id = os.environ.get("CHAT_ID", "") |
| 167 | + if not token or not chat_id: |
| 168 | + print("TELEGRAM secrets not present, skipping post.") |
| 169 | + return 0 |
| 170 | + |
| 171 | + ver = args.version |
| 172 | + sha = sha256_of(args.apk) |
| 173 | + caption = ( |
| 174 | + f"<b>mhrv-rs Android v{ver}</b>\n\n" |
| 175 | + f"SHA-256: <code>{sha}</code>\n" |
| 176 | + f"https://github.com/{args.repo}\n" |
| 177 | + f"https://github.com/{args.repo}/releases/tag/v{ver}" |
| 178 | + ) |
| 179 | + |
| 180 | + doc_mid = send_document(token, chat_id, args.apk, caption) |
| 181 | + print(f"sendDocument OK, message_id={doc_mid}") |
| 182 | + |
| 183 | + fa, en = parse_changelog(args.changelog) |
| 184 | + if not fa and not en: |
| 185 | + print(f"No changelog at {args.changelog}, skipping reply.") |
| 186 | + return 0 |
| 187 | + |
| 188 | + parts = [] |
| 189 | + if fa: |
| 190 | + parts.append(f"<blockquote>{fa}</blockquote>") |
| 191 | + if en: |
| 192 | + parts.append(f"<blockquote>{en}</blockquote>") |
| 193 | + reply = "\n\n".join(parts) |
| 194 | + |
| 195 | + send_reply(token, chat_id, reply, doc_mid) |
| 196 | + print("Reply OK") |
| 197 | + return 0 |
| 198 | + |
| 199 | + |
| 200 | +if __name__ == "__main__": |
| 201 | + sys.exit(main()) |
0 commit comments