From 1cef59e984ba95758c8954cb71d02d125372b65d Mon Sep 17 00:00:00 2001 From: ShiftHackZ Date: Thu, 19 Nov 2026 22:34:42 +0200 Subject: [PATCH 1/3] Announce nightly builds in Telegram post comments --- .github/NIGHTLY.md | 86 +++++++++++++++++++++ .github/workflows/nightly_android.yml | 106 ++++++++++++++++++++++---- 2 files changed, 176 insertions(+), 16 deletions(-) create mode 100644 .github/NIGHTLY.md diff --git a/.github/NIGHTLY.md b/.github/NIGHTLY.md new file mode 100644 index 000000000..f70a66558 --- /dev/null +++ b/.github/NIGHTLY.md @@ -0,0 +1,86 @@ +# Nightly announcements + +The Android Full Nightly workflow publishes the FullDev APK from `develop`, then +announces it on Discord and in the comments of a Telegram channel post. Both +destinations use the same title, version, recent changes, links and disclaimer. +Discord uses an embed; Telegram uses HTML text with link previews disabled. +Announcements are sent only when a build is published, including manual runs with +`publish: true`. Skipped builds and runs with `publish: false` send nothing. + +## Telegram setup + +1. Create a dedicated bot through [@BotFather](https://t.me/BotFather) using + `/newbot` and save its token. +2. Add the bot to the channel's **linked discussion group** and allow it to send + text messages. Channel administrator access is unnecessary for posting group + comments. Keep the existing nightly announcement and its comments enabled. +3. Obtain the discussion group ID and the message ID of the automatically + forwarded nightly announcement **inside that group**. The channel post ID is + a different ID and must not be used. +4. Add the following settings in GitHub under **Settings → Environments → + android-release**: + +| Type | Name | Value | +| -------- | -------------------------------------- | ----------------------------------------------------------------------- | +| Secret | `TELEGRAM_NIGHTLY_BOT_TOKEN` | Token issued by BotFather | +| Variable | `TELEGRAM_NIGHTLY_CHAT_ID` | Numeric discussion supergroup ID, including its `-100` prefix | +| Variable | `TELEGRAM_NIGHTLY_REPLY_TO_MESSAGE_ID` | Positive message ID of the forwarded nightly announcement in that group | + +Repository Actions secrets and variables with these names also work. Environment +settings take precedence over repository settings of the same type. The two IDs +can also be stored as secrets with the same names; a nonempty variable takes +precedence over the corresponding secret. The existing +`DISCORD_NIGHTLY_WEBHOOK_URL` secret continues to control Discord delivery. + +### Finding the two IDs + +For a new dedicated bot without a webhook or another update consumer: + +1. Open the existing nightly post's comments and send + `/nightly_setup@YourBotUsername` as a direct reply to the announcement. An + explicitly addressed command is delivered even with bot privacy mode enabled. +2. In a local terminal, set `TELEGRAM_NIGHTLY_BOT_TOKEN` to the bot token, then read + the incoming update: + + ```sh + curl --fail --silent --show-error \ + "https://api.telegram.org/bot${TELEGRAM_NIGHTLY_BOT_TOKEN}/getUpdates" | + jq '.result[] | select((.message.text? // "") | startswith("/nightly_setup@")) | + {chat_id: .message.chat.id, + reply_to_message_id: .message.reply_to_message.message_id, + is_announcement: .message.reply_to_message.is_automatic_forward}' + ``` + +3. Use `chat_id` and `reply_to_message_id` from the matching update, verifying + `is_announcement` is `true`. Do not use the setup command's own message ID. + The setup command can then be deleted. No bot server needs to remain running. + +Telegram retains updates for at most 24 hours. If using a bot with an existing +webhook or polling service, obtain these fields from that service's received +update instead of competing with its update consumer. + +See the Telegram documentation for [channel comments](https://core.telegram.org/api/discussion), +[incoming updates](https://core.telegram.org/bots/api#getupdates) and +[bot privacy](https://core.telegram.org/bots/faq#what-messages-will-my-bot-get). + +## Activation and delivery + +Merge the workflow change into `master`, the default branch used by scheduled +Actions. Its checkout still builds `develop`; synchronizing the two branches is +unnecessary for activation. Announcement preparation stays inline in the workflow +so it does not depend on helper scripts being present in the checked-out branch. + +With all three Telegram settings absent, Telegram delivery is skipped. Partial +configuration, invalid numeric IDs, or rejected delivery fails the notification +step. A missing reply target causes an error instead of an unrelated group +message. Discord and Telegram delivery are attempted independently once the +shared announcement is prepared; a Discord failure does not prevent Telegram. + +The next published nightly verifies delivery through the normal workflow. A +manual run with `publish: true` also sends announcements; `force: true` additionally +rebuilds even if no functional changes exist. The `android-release` environment's +branch restrictions still apply to manual runs. + +Neither destination retries its POST automatically because a lost response can +mean the message was already delivered. Manually rerunning a publishing workflow +can post another announcement. diff --git a/.github/workflows/nightly_android.yml b/.github/workflows/nightly_android.yml index 17c3fc839..b39c46285 100644 --- a/.github/workflows/nightly_android.yml +++ b/.github/workflows/nightly_android.yml @@ -333,22 +333,17 @@ jobs: --verify-tag fi - - name: Announce nightly on Discord + - name: Prepare nightly announcements + id: announcement if: steps.plan.outputs.should_build == 'true' && env.PUBLISH_NIGHTLY == 'true' shell: bash env: - DISCORD_WEBHOOK_URL: ${{ secrets.DISCORD_NIGHTLY_WEBHOOK_URL }} NIGHTLY_VERSION_NAME: ${{ steps.version.outputs.version_name }} NIGHTLY_BUILD_DATE: ${{ steps.version.outputs.build_date }} NIGHTLY_COMMIT: ${{ steps.plan.outputs.target_sha }} run: | set -euo pipefail - if [[ -z "${DISCORD_WEBHOOK_URL}" ]]; then - echo "::notice::Discord notification skipped: DISCORD_NIGHTLY_WEBHOOK_URL is not configured." - exit 0 - fi - output_dir="${RUNNER_TEMP}/nightly-output" release_url="https://github.com/${GITHUB_REPOSITORY}/releases/tag/${NIGHTLY_TAG}" download_url="https://github.com/${GITHUB_REPOSITORY}/releases/download/${NIGHTLY_TAG}/${APK_NAME}" @@ -362,16 +357,49 @@ jobs: --arg run_url "https://github.com/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}" \ --arg feedback_url "https://github.com/${GITHUB_REPOSITORY}/issues" \ --rawfile changes "${output_dir}/changes.txt" \ - '{allowed_mentions: {parse: []}, embeds: [{ + '{ title: ("SDAI Dev · Nightly · " + $date), - url: $release_url, - color: 7684269, - description: ("**" + $version + "**\nInstalls alongside stable SDAI with separate app data.\n\n" + - $changes[:1000] + "\n[Download latest APK](" + $download_url + ") · [Release details](" + $release_url + - ")\n[Commit " + $commit[:8] + "](" + $commit_url + ") · [Build](" + $run_url + - ") · [Report an issue](" + $feedback_url + ")"), - footer: {text: "Preview build for testing. The download link always serves the latest nightly."} - }]}' > "${output_dir}/discord.json" + version: $version, + installation_note: "Installs alongside stable SDAI with separate app data.", + changes: $changes[:1000], + links: [ + [{text: "Download latest APK", url: $download_url}, {text: "Release details", url: $release_url}], + [{text: ("Commit " + $commit[:8]), url: $commit_url}, {text: "Build", url: $run_url}, + {text: "Report an issue", url: $feedback_url}] + ], + footer: "Preview build for testing. The download link always serves the latest nightly." + }' > "${output_dir}/announcement.json" + + jq '{allowed_mentions: {parse: []}, embeds: [{ + title: .title, + url: .links[0][1].url, + color: 7684269, + description: ("**" + .version + "**\n" + .installation_note + "\n\n" + .changes + "\n" + + (.links | map(map("[" + .text + "](" + .url + ")") | join(" · ")) | join("\n"))), + footer: {text: .footer} + }]}' "${output_dir}/announcement.json" > "${output_dir}/discord.json" + + # Escape all dynamic content for Telegram HTML, including commit subjects. + jq -r '"" + (.title | @html) + "\n\n" + (.version | @html) + "\n" + + (.installation_note | @html) + "\n\n" + (.changes | @html) + "\n" + + (.links | map(map("" + (.text | @html) + "") | + join(" · ")) | join("\n")) + "\n\n" + (.footer | @html)' \ + "${output_dir}/announcement.json" > "${output_dir}/telegram.html" + + - name: Announce nightly on Discord + if: steps.plan.outputs.should_build == 'true' && env.PUBLISH_NIGHTLY == 'true' + shell: bash + env: + DISCORD_WEBHOOK_URL: ${{ secrets.DISCORD_NIGHTLY_WEBHOOK_URL }} + run: | + set -euo pipefail + + if [[ -z "${DISCORD_WEBHOOK_URL}" ]]; then + echo "::notice::Discord notification skipped: DISCORD_NIGHTLY_WEBHOOK_URL is not configured." + exit 0 + fi + + output_dir="${RUNNER_TEMP}/nightly-output" # Do not retry POST automatically: a lost response could duplicate the announcement. curl --fail --silent --show-error --connect-timeout 15 --max-time 60 \ @@ -380,3 +408,49 @@ jobs: --output "${output_dir}/discord-response.json" \ --url-query 'wait=true' \ "${DISCORD_WEBHOOK_URL}" + + - name: Announce nightly in Telegram comments + # Attempt both destinations even if Discord delivery fails. + if: ${{ !cancelled() && steps.announcement.outcome == 'success' && steps.plan.outputs.should_build == 'true' && env.PUBLISH_NIGHTLY == 'true' }} + shell: bash + env: + TELEGRAM_BOT_TOKEN: ${{ secrets.TELEGRAM_NIGHTLY_BOT_TOKEN }} + TELEGRAM_CHAT_ID: ${{ vars.TELEGRAM_NIGHTLY_CHAT_ID || secrets.TELEGRAM_NIGHTLY_CHAT_ID }} + TELEGRAM_REPLY_TO_MESSAGE_ID: ${{ vars.TELEGRAM_NIGHTLY_REPLY_TO_MESSAGE_ID || secrets.TELEGRAM_NIGHTLY_REPLY_TO_MESSAGE_ID }} + run: | + set -euo pipefail + + if [[ -z "${TELEGRAM_BOT_TOKEN}${TELEGRAM_CHAT_ID}${TELEGRAM_REPLY_TO_MESSAGE_ID}" ]]; then + echo "::notice::Telegram notification skipped: nightly Telegram settings are not configured." + exit 0 + fi + + : "${TELEGRAM_BOT_TOKEN:?Missing TELEGRAM_NIGHTLY_BOT_TOKEN secret.}" + : "${TELEGRAM_CHAT_ID:?Missing TELEGRAM_NIGHTLY_CHAT_ID variable or secret.}" + : "${TELEGRAM_REPLY_TO_MESSAGE_ID:?Missing TELEGRAM_NIGHTLY_REPLY_TO_MESSAGE_ID variable or secret.}" + if [[ ! "${TELEGRAM_CHAT_ID}" =~ ^-[0-9]+$ || ! "${TELEGRAM_REPLY_TO_MESSAGE_ID}" =~ ^[1-9][0-9]*$ ]]; then + echo "::error::Telegram requires a negative discussion group ID and a positive reply message ID." + exit 1 + fi + + output_dir="${RUNNER_TEMP}/nightly-output" + jq -n \ + --arg chat_id "${TELEGRAM_CHAT_ID}" \ + --argjson reply_id "${TELEGRAM_REPLY_TO_MESSAGE_ID}" \ + --rawfile text "${output_dir}/telegram.html" \ + '{chat_id: $chat_id, text: $text, parse_mode: "HTML", + link_preview_options: {is_disabled: true}, + reply_parameters: {message_id: $reply_id, allow_sending_without_reply: false}}' \ + > "${output_dir}/telegram.json" + + # Do not retry POST automatically: a lost response could duplicate the comment. + curl --fail --silent --show-error --connect-timeout 15 --max-time 60 \ + --header 'Content-Type: application/json' \ + --data-binary "@${output_dir}/telegram.json" \ + --output "${output_dir}/telegram-response.json" \ + "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage" + + if ! jq -e '.ok == true' "${output_dir}/telegram-response.json" >/dev/null; then + echo "::error::Telegram did not confirm the nightly comment." + exit 1 + fi From e24e7daf3056c9d914e2a18d31c3ec0a2ad0b7d4 Mon Sep 17 00:00:00 2001 From: ShiftHackZ Date: Thu, 19 Nov 2026 22:38:49 +0200 Subject: [PATCH 2/3] Keep Telegram behavior in existing nightly documentation --- .github/NIGHTLY.md | 86 ---------------------------------------------- NIGHTLY_BUILDS.md | 6 ++++ 2 files changed, 6 insertions(+), 86 deletions(-) delete mode 100644 .github/NIGHTLY.md diff --git a/.github/NIGHTLY.md b/.github/NIGHTLY.md deleted file mode 100644 index f70a66558..000000000 --- a/.github/NIGHTLY.md +++ /dev/null @@ -1,86 +0,0 @@ -# Nightly announcements - -The Android Full Nightly workflow publishes the FullDev APK from `develop`, then -announces it on Discord and in the comments of a Telegram channel post. Both -destinations use the same title, version, recent changes, links and disclaimer. -Discord uses an embed; Telegram uses HTML text with link previews disabled. -Announcements are sent only when a build is published, including manual runs with -`publish: true`. Skipped builds and runs with `publish: false` send nothing. - -## Telegram setup - -1. Create a dedicated bot through [@BotFather](https://t.me/BotFather) using - `/newbot` and save its token. -2. Add the bot to the channel's **linked discussion group** and allow it to send - text messages. Channel administrator access is unnecessary for posting group - comments. Keep the existing nightly announcement and its comments enabled. -3. Obtain the discussion group ID and the message ID of the automatically - forwarded nightly announcement **inside that group**. The channel post ID is - a different ID and must not be used. -4. Add the following settings in GitHub under **Settings → Environments → - android-release**: - -| Type | Name | Value | -| -------- | -------------------------------------- | ----------------------------------------------------------------------- | -| Secret | `TELEGRAM_NIGHTLY_BOT_TOKEN` | Token issued by BotFather | -| Variable | `TELEGRAM_NIGHTLY_CHAT_ID` | Numeric discussion supergroup ID, including its `-100` prefix | -| Variable | `TELEGRAM_NIGHTLY_REPLY_TO_MESSAGE_ID` | Positive message ID of the forwarded nightly announcement in that group | - -Repository Actions secrets and variables with these names also work. Environment -settings take precedence over repository settings of the same type. The two IDs -can also be stored as secrets with the same names; a nonempty variable takes -precedence over the corresponding secret. The existing -`DISCORD_NIGHTLY_WEBHOOK_URL` secret continues to control Discord delivery. - -### Finding the two IDs - -For a new dedicated bot without a webhook or another update consumer: - -1. Open the existing nightly post's comments and send - `/nightly_setup@YourBotUsername` as a direct reply to the announcement. An - explicitly addressed command is delivered even with bot privacy mode enabled. -2. In a local terminal, set `TELEGRAM_NIGHTLY_BOT_TOKEN` to the bot token, then read - the incoming update: - - ```sh - curl --fail --silent --show-error \ - "https://api.telegram.org/bot${TELEGRAM_NIGHTLY_BOT_TOKEN}/getUpdates" | - jq '.result[] | select((.message.text? // "") | startswith("/nightly_setup@")) | - {chat_id: .message.chat.id, - reply_to_message_id: .message.reply_to_message.message_id, - is_announcement: .message.reply_to_message.is_automatic_forward}' - ``` - -3. Use `chat_id` and `reply_to_message_id` from the matching update, verifying - `is_announcement` is `true`. Do not use the setup command's own message ID. - The setup command can then be deleted. No bot server needs to remain running. - -Telegram retains updates for at most 24 hours. If using a bot with an existing -webhook or polling service, obtain these fields from that service's received -update instead of competing with its update consumer. - -See the Telegram documentation for [channel comments](https://core.telegram.org/api/discussion), -[incoming updates](https://core.telegram.org/bots/api#getupdates) and -[bot privacy](https://core.telegram.org/bots/faq#what-messages-will-my-bot-get). - -## Activation and delivery - -Merge the workflow change into `master`, the default branch used by scheduled -Actions. Its checkout still builds `develop`; synchronizing the two branches is -unnecessary for activation. Announcement preparation stays inline in the workflow -so it does not depend on helper scripts being present in the checked-out branch. - -With all three Telegram settings absent, Telegram delivery is skipped. Partial -configuration, invalid numeric IDs, or rejected delivery fails the notification -step. A missing reply target causes an error instead of an unrelated group -message. Discord and Telegram delivery are attempted independently once the -shared announcement is prepared; a Discord failure does not prevent Telegram. - -The next published nightly verifies delivery through the normal workflow. A -manual run with `publish: true` also sends announcements; `force: true` additionally -rebuilds even if no functional changes exist. The `android-release` environment's -branch restrictions still apply to manual runs. - -Neither destination retries its POST automatically because a lost response can -mean the message was already delivered. Manually rerunning a publishing workflow -can post another announcement. diff --git a/NIGHTLY_BUILDS.md b/NIGHTLY_BUILDS.md index 59ca6cdab..c278bf461 100644 --- a/NIGHTLY_BUILDS.md +++ b/NIGHTLY_BUILDS.md @@ -59,6 +59,12 @@ Give ordinary members View Channel and Read Message History, deny Send Messages, The APK is hosted on GitHub rather than attached to Discord because Full APKs exceed Discord's default upload limit. The fixed download URL always serves the latest nightly, including when opened from an older announcement. Mention parsing is disabled so commit subjects cannot notify roles or everyone. +## Telegram Notifications + +After successfully publishing a nightly, the workflow posts a comment under the configured Telegram announcement in the channel's linked discussion group. It uses the same title, version, recent changes, links and disclaimer as Discord, rendered as HTML text with link previews disabled. The APK is linked from GitHub, not attached as a file. + +Skipped builds and runs with `publish=false` do not post. A missing reply target fails delivery instead of posting an unrelated group message. Telegram delivery is attempted even if Discord delivery fails. Neither destination retries automatically, since a lost response could duplicate an announcement. + ## Manual Run GitHub only exposes `workflow_dispatch` for workflow files that exist on the repository default branch. Keep `.github/workflows/nightly_android.yml` on `master`; the workflow still builds `develop` through the `target_ref` input. From 84a635a5231dccfacc40983b07bddfcb897e630e Mon Sep 17 00:00:00 2001 From: ShiftHackZ Date: Thu, 19 Nov 2026 22:52:49 +0200 Subject: [PATCH 3/3] Attach signed nightly APKs to Telegram announcements --- .github/workflows/nightly_android.yml | 63 ++++----- NIGHTLY_BUILDS.md | 4 +- scripts/nightly/build_telegram_bot_api.sh | 23 ++++ scripts/nightly/telegram_announce.py | 161 ++++++++++++++++++++++ 4 files changed, 213 insertions(+), 38 deletions(-) create mode 100644 scripts/nightly/build_telegram_bot_api.sh create mode 100644 scripts/nightly/telegram_announce.py diff --git a/.github/workflows/nightly_android.yml b/.github/workflows/nightly_android.yml index b39c46285..d3f69fd79 100644 --- a/.github/workflows/nightly_android.yml +++ b/.github/workflows/nightly_android.yml @@ -31,6 +31,7 @@ env: APK_NAME: sdai-full-nightly.apk NIGHTLY_TAG: nightly NIGHTLY_RELEASE_NAME: Android Full Nightly + TELEGRAM_BOT_API_REVISION: 7232881b39dfcd048a0d569f1983c5184249ee05 jobs: nightly: @@ -86,7 +87,7 @@ jobs: git diff --name-only "${NIGHTLY_TAG}..HEAD" > "${RUNNER_TEMP}/nightly-changed-files.txt" - functional_pattern='^(app/|core/|data/|demo/|domain/|feature/|network/|presentation/|storage/|build-logic/|gradle/|build\.gradle\.kts$|settings\.gradle\.kts$|gradle\.properties$|gradlew$|gradlew\.bat$|\.github/workflows/nightly_android\.yml$)' + functional_pattern='^(app/|core/|data/|demo/|domain/|feature/|network/|presentation/|storage/|build-logic/|gradle/|scripts/nightly/|build\.gradle\.kts$|settings\.gradle\.kts$|gradle\.properties$|gradlew$|gradlew\.bat$|\.github/workflows/nightly_android\.yml$)' if grep -Eq "${functional_pattern}" "${RUNNER_TEMP}/nightly-changed-files.txt"; then echo "should_build=true" >> "${GITHUB_OUTPUT}" echo "reason=Functional files changed since the previous nightly." >> "${GITHUB_OUTPUT}" @@ -409,48 +410,36 @@ jobs: --url-query 'wait=true' \ "${DISCORD_WEBHOOK_URL}" - - name: Announce nightly in Telegram comments + - name: Checkout nightly notification tools + id: telegram_tools # Attempt both destinations even if Discord delivery fails. if: ${{ !cancelled() && steps.announcement.outcome == 'success' && steps.plan.outputs.should_build == 'true' && env.PUBLISH_NIGHTLY == 'true' }} + uses: actions/checkout@v4 + with: + # Notification tools belong to the workflow revision, not the APK's target_ref. + ref: ${{ github.workflow_sha }} + path: .nightly-ci + sparse-checkout: scripts/nightly + persist-credentials: false + + - name: Cache Telegram Bot API binary + id: telegram_cache + if: ${{ !cancelled() && steps.telegram_tools.outcome == 'success' }} + uses: actions/cache@v4 + with: + path: ${{ runner.temp }}/telegram-bot-api/bin + key: telegram-bot-api-ubuntu24-${{ runner.arch }}-${{ env.TELEGRAM_BOT_API_REVISION }}-${{ hashFiles('.nightly-ci/scripts/nightly/build_telegram_bot_api.sh') }} + + - name: Announce nightly and attach APK in Telegram comments + if: ${{ !cancelled() && steps.telegram_tools.outcome == 'success' && steps.telegram_cache.outcome == 'success' }} shell: bash env: TELEGRAM_BOT_TOKEN: ${{ secrets.TELEGRAM_NIGHTLY_BOT_TOKEN }} TELEGRAM_CHAT_ID: ${{ vars.TELEGRAM_NIGHTLY_CHAT_ID || secrets.TELEGRAM_NIGHTLY_CHAT_ID }} TELEGRAM_REPLY_TO_MESSAGE_ID: ${{ vars.TELEGRAM_NIGHTLY_REPLY_TO_MESSAGE_ID || secrets.TELEGRAM_NIGHTLY_REPLY_TO_MESSAGE_ID }} + TELEGRAM_API_ID: ${{ secrets.TELEGRAM_API_ID }} + TELEGRAM_API_HASH: ${{ secrets.TELEGRAM_API_HASH }} + NIGHTLY_SIGNED_APK: ${{ steps.sign.outputs.signed_apk }} run: | set -euo pipefail - - if [[ -z "${TELEGRAM_BOT_TOKEN}${TELEGRAM_CHAT_ID}${TELEGRAM_REPLY_TO_MESSAGE_ID}" ]]; then - echo "::notice::Telegram notification skipped: nightly Telegram settings are not configured." - exit 0 - fi - - : "${TELEGRAM_BOT_TOKEN:?Missing TELEGRAM_NIGHTLY_BOT_TOKEN secret.}" - : "${TELEGRAM_CHAT_ID:?Missing TELEGRAM_NIGHTLY_CHAT_ID variable or secret.}" - : "${TELEGRAM_REPLY_TO_MESSAGE_ID:?Missing TELEGRAM_NIGHTLY_REPLY_TO_MESSAGE_ID variable or secret.}" - if [[ ! "${TELEGRAM_CHAT_ID}" =~ ^-[0-9]+$ || ! "${TELEGRAM_REPLY_TO_MESSAGE_ID}" =~ ^[1-9][0-9]*$ ]]; then - echo "::error::Telegram requires a negative discussion group ID and a positive reply message ID." - exit 1 - fi - - output_dir="${RUNNER_TEMP}/nightly-output" - jq -n \ - --arg chat_id "${TELEGRAM_CHAT_ID}" \ - --argjson reply_id "${TELEGRAM_REPLY_TO_MESSAGE_ID}" \ - --rawfile text "${output_dir}/telegram.html" \ - '{chat_id: $chat_id, text: $text, parse_mode: "HTML", - link_preview_options: {is_disabled: true}, - reply_parameters: {message_id: $reply_id, allow_sending_without_reply: false}}' \ - > "${output_dir}/telegram.json" - - # Do not retry POST automatically: a lost response could duplicate the comment. - curl --fail --silent --show-error --connect-timeout 15 --max-time 60 \ - --header 'Content-Type: application/json' \ - --data-binary "@${output_dir}/telegram.json" \ - --output "${output_dir}/telegram-response.json" \ - "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage" - - if ! jq -e '.ok == true' "${output_dir}/telegram-response.json" >/dev/null; then - echo "::error::Telegram did not confirm the nightly comment." - exit 1 - fi + python3 .nightly-ci/scripts/nightly/telegram_announce.py diff --git a/NIGHTLY_BUILDS.md b/NIGHTLY_BUILDS.md index c278bf461..04a607149 100644 --- a/NIGHTLY_BUILDS.md +++ b/NIGHTLY_BUILDS.md @@ -61,7 +61,9 @@ The APK is hosted on GitHub rather than attached to Discord because Full APKs ex ## Telegram Notifications -After successfully publishing a nightly, the workflow posts a comment under the configured Telegram announcement in the channel's linked discussion group. It uses the same title, version, recent changes, links and disclaimer as Discord, rendered as HTML text with link previews disabled. The APK is linked from GitHub, not attached as a file. +After successfully publishing a nightly, the workflow posts a comment under the configured Telegram announcement in the channel's linked discussion group. It uses the same title, version, recent changes, links and disclaimer as Discord, rendered as HTML text with link previews disabled. The exact signed APK published to GitHub is sent as a file in a reply to that comment, with its version as the caption. The full announcement stays in a separate text message to avoid Telegram's shorter document caption limit. + +Uploads use a temporary local Bot API server on the runner, supporting files up to 2 GB. Its executable is built from pinned Telegram source and cached separately from bot session data. The server listens only on loopback, logs out the bot before discarding its session and stops when delivery finishes or the step is cancelled. Notification tools come from the workflow's commit, independently of the APK's build target. Skipped builds and runs with `publish=false` do not post. A missing reply target fails delivery instead of posting an unrelated group message. Telegram delivery is attempted even if Discord delivery fails. Neither destination retries automatically, since a lost response could duplicate an announcement. diff --git a/scripts/nightly/build_telegram_bot_api.sh b/scripts/nightly/build_telegram_bot_api.sh new file mode 100644 index 000000000..91be341d8 --- /dev/null +++ b/scripts/nightly/build_telegram_bot_api.sh @@ -0,0 +1,23 @@ +#!/usr/bin/env bash +# Build only the pinned upstream executable; bot sessions never enter this directory. +set -euo pipefail + +: "${RUNNER_TEMP:?Missing runner temporary directory.}" +: "${TELEGRAM_BOT_API_REVISION:?Missing Telegram Bot API source revision.}" +if [[ ! "${TELEGRAM_BOT_API_REVISION}" =~ ^[0-9a-f]{40}$ ]]; then + echo "Telegram Bot API revision must be a full commit SHA." >&2 + exit 1 +fi + +source_dir="${RUNNER_TEMP}/telegram-bot-api/source" +binary_dir="${RUNNER_TEMP}/telegram-bot-api/bin" +sudo apt-get update +sudo apt-get install --yes --no-install-recommends cmake g++ make gperf libssl-dev zlib1g-dev +git init "${source_dir}" +git -C "${source_dir}" fetch --depth 1 https://github.com/tdlib/telegram-bot-api.git "${TELEGRAM_BOT_API_REVISION}" +git -C "${source_dir}" checkout --detach FETCH_HEAD +git -C "${source_dir}" submodule update --init --recursive --depth 1 +cmake -S "${source_dir}" -B "${source_dir}/build" -DCMAKE_BUILD_TYPE=Release +cmake --build "${source_dir}/build" --target telegram-bot-api --parallel 2 +install -d "${binary_dir}" +install -m 755 "${source_dir}/build/telegram-bot-api" "${binary_dir}/telegram-bot-api" diff --git a/scripts/nightly/telegram_announce.py b/scripts/nightly/telegram_announce.py new file mode 100644 index 000000000..3cd28320c --- /dev/null +++ b/scripts/nightly/telegram_announce.py @@ -0,0 +1,161 @@ +#!/usr/bin/env python3 +"""Publish the nightly announcement and its signed APK through a temporary local Bot API.""" + +import json +import os +from pathlib import Path +import re +import signal +import socket +import subprocess +import sys +import tempfile +import time +import urllib.error +import urllib.request + + +def request(base_url, token, method, payload=None, timeout=30): + """Call Telegram without retries or exceptions that disclose credential-bearing URLs.""" + data = None if payload is None else json.dumps(payload).encode() + query = urllib.request.Request( + f"{base_url}/bot{token}/{method}", + data=data, + headers={"Content-Type": "application/json"}, + ) + try: + with urllib.request.urlopen(query, timeout=timeout) as response: + result = json.load(response) + except urllib.error.HTTPError as error: + raise RuntimeError(f"Telegram {method} failed (HTTP {error.code}).") from None + except (OSError, ValueError, urllib.error.URLError): + raise RuntimeError(f"Telegram {method} failed; delivery may be unconfirmed.") from None + if result.get("ok") is not True: + raise RuntimeError(f"Telegram {method} was not confirmed.") + return result["result"] + + +def cancel(signum, _frame): + """Unwind the owned server and session when the runner cancels this step.""" + raise SystemExit(128 + signum) + + +def main(): + """Validate configuration before building, then send both comments and clean up the session.""" + names = ( + "TELEGRAM_BOT_TOKEN", + "TELEGRAM_CHAT_ID", + "TELEGRAM_REPLY_TO_MESSAGE_ID", + "TELEGRAM_API_ID", + "TELEGRAM_API_HASH", + ) + settings = {name: os.environ.get(name, "") for name in names} + if not any(settings.values()): + print("::notice::Telegram notification skipped: nightly Telegram settings are not configured.") + return + if not all(settings.values()): + raise RuntimeError("Telegram requires the bot token, chat ID, reply ID, API ID and API hash.") + patterns = { + "TELEGRAM_CHAT_ID": r"-[0-9]+", + "TELEGRAM_REPLY_TO_MESSAGE_ID": r"[1-9][0-9]*", + "TELEGRAM_API_ID": r"[1-9][0-9]*", + "TELEGRAM_API_HASH": r"[0-9a-fA-F]{32}", + } + if any(not re.fullmatch(pattern, settings[name]) for name, pattern in patterns.items()): + raise RuntimeError("Telegram IDs or API hash have an invalid format.") + + apk = Path(os.environ["NIGHTLY_SIGNED_APK"]).resolve(strict=True) + apk_size = apk.stat().st_size + if not apk.is_file() or not 0 < apk_size <= 2_000_000_000: + raise RuntimeError("The signed nightly APK must be a nonempty file within Telegram's 2 GB limit.") + runner_temp = Path(os.environ["RUNNER_TEMP"]) + output_dir = runner_temp / "nightly-output" + text = (output_dir / "telegram.html").read_text() + announcement = json.loads((output_dir / "announcement.json").read_text()) + binary = runner_temp / "telegram-bot-api/bin/telegram-bot-api" + if not binary.is_file(): + build_env = {key: value for key, value in os.environ.items() if key not in names} + subprocess.run( + ["bash", str(Path(__file__).with_name("build_telegram_bot_api.sh"))], + env=build_env, + check=True, + ) + + token = settings["TELEGRAM_BOT_TOKEN"] + chat_id = int(settings["TELEGRAM_CHAT_ID"]) + reply_id = int(settings["TELEGRAM_REPLY_TO_MESSAGE_ID"]) + # Choose an available loopback port for this owned server. + with socket.socket() as reservation: + reservation.bind(("127.0.0.1", 0)) + port = reservation.getsockname()[1] + base_url = f"http://127.0.0.1:{port}" + signal.signal(signal.SIGTERM, cancel) + signal.signal(signal.SIGINT, cancel) + request("https://api.telegram.org", token, "logOut", {}) + with tempfile.TemporaryDirectory(prefix="sdai-telegram-session-", dir=runner_temp) as session_dir: + server = subprocess.Popen( + [str(binary), "--local", "--http-ip-address=127.0.0.1", f"--http-port={port}", + f"--dir={session_dir}", f"--temp-dir={session_dir}", "--verbosity=0"], + stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL, + ) + try: + deadline = time.monotonic() + 120 + while True: + if server.poll() is not None: + raise RuntimeError("The local Telegram Bot API server exited before becoming ready.") + try: + request(base_url, token, "getMe", timeout=5) + break + except RuntimeError: + if time.monotonic() >= deadline: + raise RuntimeError("The local Telegram Bot API server did not become ready.") from None + time.sleep(2) + + # Preserve the full text rather than truncate it to Telegram's document caption limit. + message = request(base_url, token, "sendMessage", { + "chat_id": chat_id, + "text": text, + "parse_mode": "HTML", + "link_preview_options": {"is_disabled": True}, + "reply_parameters": {"message_id": reply_id, "allow_sending_without_reply": False}, + }) + # Local mode reads the exact signed APK from disk without a second download or memory copy. + document = request(base_url, token, "sendDocument", { + "chat_id": chat_id, + "document": apk.as_uri(), + "caption": announcement["version"], + "reply_parameters": { + "message_id": message["message_id"], + "allow_sending_without_reply": False, + }, + }, timeout=900) + if document.get("document", {}).get("file_size") != apk_size: + raise RuntimeError("Telegram did not confirm the expected APK file size.") + print("Telegram confirmed the nightly announcement and signed APK attachment.") + finally: + has_delivery_failed = sys.exc_info()[0] is not None + try: + # Invalidate this session before the disposable runner removes its state. + request(base_url, token, "logOut", {}, timeout=30) + except RuntimeError: + print("::error::Telegram session logout failed.", file=sys.stderr) + if not has_delivery_failed: + raise + finally: + server.terminate() + try: + server.wait(timeout=30) + except subprocess.TimeoutExpired: + server.kill() + server.wait() + + +if __name__ == "__main__": + try: + main() + except (RuntimeError, OSError, KeyError, ValueError, subprocess.CalledProcessError) as error: + # Only deliberate errors are displayed; library diagnostics may contain a token or API hash. + message = str(error) if isinstance(error, RuntimeError) else type(error).__name__ + print(f"::error::{message}", file=sys.stderr) + sys.exit(1)