From 7d216bc07c00cb0849fb0049593114c767025715 Mon Sep 17 00:00:00 2001 From: Howard Wu Date: Tue, 6 Jun 2023 13:46:53 +0800 Subject: [PATCH 1/3] ci: Send media groups directly --- scripts/ksubot.py | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/scripts/ksubot.py b/scripts/ksubot.py index 2839de7f62df..e895560daf26 100644 --- a/scripts/ksubot.py +++ b/scripts/ksubot.py @@ -77,19 +77,17 @@ async def main(): print("---") print(caption) print("---") - for one in paths: - if not os.path.exists(one): - print("[-] File not exist: " + one) + for file in paths: + if not os.path.exists(file): + print("[-] File not exist: " + file) continue - print("[+] Upload: " + one) - msg = await bot.send_document(CACHE_CHAT_ID, one, write_timeout=60, connect_timeout=30) - if one == paths[-1]: - files.append(telegram.InputMediaDocument(msg.document, + print("[+] Upload: " + file) + if file == paths[-1]: + files.append(telegram.InputMediaDocument(media=open(file, "rb"), caption=caption, parse_mode=telegram.constants.ParseMode.MARKDOWN_V2)) else: - files.append(telegram.InputMediaDocument(msg.document)) - await bot.delete_message(CACHE_CHAT_ID, msg.message_id) + files.append(telegram.InputMediaDocument(media=open(file, "rb"))) print("[+] Sending") await bot.send_media_group(CHAT_ID, files, message_thread_id=MESSAGE_THREAD_ID) print("[+] Done!") @@ -98,4 +96,3 @@ async def main(): if __name__ == "__main__": loops = asyncio.new_event_loop() loops.run_until_complete(asyncio.wait([main()])) - From 3d1ab8652c2b41be9656c8b7c595a181b2238c8c Mon Sep 17 00:00:00 2001 From: Howard Wu Date: Tue, 6 Jun 2023 14:28:32 +0800 Subject: [PATCH 2/3] Using the Local Bot API Server --- .github/workflows/build-kernel-a12.yml | 10 ++++++++++ .github/workflows/build-kernel-a13.yml | 12 +++++++++++- .github/workflows/build-kernel-wsa.yml | 10 ++++++++++ .github/workflows/build-manager.yml | 10 ++++++++++ .github/workflows/build-su.yml | 11 +++++++++++ scripts/ksubot.py | 2 +- 6 files changed, 53 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build-kernel-a12.yml b/.github/workflows/build-kernel-a12.yml index 32e94d6c18e0..4ed255f969e0 100644 --- a/.github/workflows/build-kernel-a12.yml +++ b/.github/workflows/build-kernel-a12.yml @@ -82,6 +82,16 @@ jobs: - name: Setup mutex for uploading uses: ben-z/gh-action-mutex@v1.0-alpha-7 + - name: Setup Telegram Bot Server + env: + API_ID: ${{ secrets.API_ID }} + API_HASH: ${{ secrets.API_HASH }} + if: ${{ ( github.event_name == 'push' && github.ref == 'refs/heads/main' ) || github.ref_type == 'tag' }} && env.API_ID != null && env.API_HASH != null + uses: zjns/telegram-bot-server@v1 + with: + api_id: ${{ env.API_ID }} + api_hash: ${{ env.API_HASH }} + - name: Build boot images run: | export AVBTOOL=$GITHUB_WORKSPACE/kernel-build-tools/linux-x86/bin/avbtool diff --git a/.github/workflows/build-kernel-a13.yml b/.github/workflows/build-kernel-a13.yml index d2a187ba201c..d1a1e2b55c82 100644 --- a/.github/workflows/build-kernel-a13.yml +++ b/.github/workflows/build-kernel-a13.yml @@ -97,6 +97,16 @@ jobs: - name: Setup mutex for uploading uses: ben-z/gh-action-mutex@v1.0-alpha-7 + - name: Setup Telegram Bot Server + env: + API_ID: ${{ secrets.API_ID }} + API_HASH: ${{ secrets.API_HASH }} + if: ${{ ( github.event_name == 'push' && github.ref == 'refs/heads/main' ) || github.ref_type == 'tag' }} && env.API_ID != null && env.API_HASH != null + uses: zjns/telegram-bot-server@v1 + with: + api_id: ${{ env.API_ID }} + api_hash: ${{ env.API_HASH }} + - name: Build boot images run: | export AVBTOOL=$GITHUB_WORKSPACE/kernel-build-tools/linux-x86/bin/avbtool @@ -109,7 +119,7 @@ jobs: echo "VERSION: $VERSION" cd - bash $GITHUB_WORKSPACE/KernelSU/.github/scripts/build_a13.sh - + - name: Display structure of boot files run: ls -R diff --git a/.github/workflows/build-kernel-wsa.yml b/.github/workflows/build-kernel-wsa.yml index 58745f813809..86ac35a09992 100644 --- a/.github/workflows/build-kernel-wsa.yml +++ b/.github/workflows/build-kernel-wsa.yml @@ -126,6 +126,16 @@ jobs: name: kernel-WSA-${{ matrix.arch }}-${{ matrix.version }} path: "${{ env.file_path }}" + - name: Setup Telegram Bot Server + env: + API_ID: ${{ secrets.API_ID }} + API_HASH: ${{ secrets.API_HASH }} + if: ${{ ( github.event_name == 'push' && github.ref == 'refs/heads/main' ) || github.ref_type == 'tag' }} && env.API_ID != null && env.API_HASH != null + uses: zjns/telegram-bot-server@v1 + with: + api_id: ${{ env.API_ID }} + api_hash: ${{ env.API_HASH }} + - name: Post to Telegram if: ${{ ( github.event_name == 'push' && github.ref == 'refs/heads/main' ) || github.ref_type == 'tag' }} env: diff --git a/.github/workflows/build-manager.yml b/.github/workflows/build-manager.yml index 12ffd7618cef..2b91c8f43b10 100644 --- a/.github/workflows/build-manager.yml +++ b/.github/workflows/build-manager.yml @@ -106,6 +106,16 @@ jobs: uses: ben-z/gh-action-mutex@v1.0-alpha-7 if: github.event_name != 'pull_request' && steps.need_upload.outputs.UPLOAD == 'true' + - name: Setup Telegram Bot Server + env: + API_ID: ${{ secrets.API_ID }} + API_HASH: ${{ secrets.API_HASH }} + if: ${{ ( github.event_name == 'push' && github.ref == 'refs/heads/main' ) || github.ref_type == 'tag' }} && env.API_ID != null && env.API_HASH != null + uses: zjns/telegram-bot-server@v1 + with: + api_id: ${{ env.API_ID }} + api_hash: ${{ env.API_HASH }} + - name: Upload to telegram if: github.event_name != 'pull_request' && steps.need_upload.outputs.UPLOAD == 'true' env: diff --git a/.github/workflows/build-su.yml b/.github/workflows/build-su.yml index d8c6d419f777..3ddef50662f7 100644 --- a/.github/workflows/build-su.yml +++ b/.github/workflows/build-su.yml @@ -41,6 +41,17 @@ jobs: - name: Setup mutex for uploading uses: ben-z/gh-action-mutex@v1.0-alpha-7 if: github.event_name != 'pull_request' && steps.need_upload.outputs.UPLOAD == 'true' + + - name: Setup Telegram Bot Server + env: + API_ID: ${{ secrets.API_ID }} + API_HASH: ${{ secrets.API_HASH }} + if: ${{ ( github.event_name == 'push' && github.ref == 'refs/heads/main' ) || github.ref_type == 'tag' }} && env.API_ID != null && env.API_HASH != null + uses: zjns/telegram-bot-server@v1 + with: + api_id: ${{ env.API_ID }} + api_hash: ${{ env.API_HASH }} + - name: Upload to telegram if: github.event_name != 'pull_request' && steps.need_upload.outputs.UPLOAD == 'true' env: diff --git a/scripts/ksubot.py b/scripts/ksubot.py index e895560daf26..68ce4df9aeb2 100644 --- a/scripts/ksubot.py +++ b/scripts/ksubot.py @@ -69,7 +69,7 @@ async def main(): print("[+] Uploading to telegram") check_environ() print("[+] Files:", sys.argv[1:]) - bot = telegram.Bot(BOT_TOKEN) + bot = telegram.Bot(BOT_TOKEN, base_url='http://127.0.0.1:8088/bot') files = [] paths = sys.argv[1:] caption = get_caption() From 0ca3eea6bd8f87afe313a5e4fecff37bae9838fd Mon Sep 17 00:00:00 2001 From: Howard Wu Date: Thu, 15 Jun 2023 16:34:39 +0800 Subject: [PATCH 3/3] Fallback to official server when local one not setup --- .github/workflows/build-kernel-a12.yml | 5 ++++- .github/workflows/build-kernel-a13.yml | 5 ++++- .github/workflows/build-kernel-wsa.yml | 6 ++++-- .github/workflows/build-manager.yml | 4 +++- .github/workflows/build-su.yml | 4 +++- scripts/ksubot.py | 18 +++++++++++++++++- 6 files changed, 35 insertions(+), 7 deletions(-) diff --git a/.github/workflows/build-kernel-a12.yml b/.github/workflows/build-kernel-a12.yml index adf5b32084cd..ac7e6291c03a 100644 --- a/.github/workflows/build-kernel-a12.yml +++ b/.github/workflows/build-kernel-a12.yml @@ -71,7 +71,7 @@ jobs: git clone $AOSP_MIRROR/platform/prebuilts/build-tools -b $BRANCH --depth 1 build-tools git clone $AOSP_MIRROR/kernel/prebuilts/build-tools -b $BRANCH --depth 1 kernel-build-tools git clone $AOSP_MIRROR/platform/system/tools/mkbootimg -b $BRANCH --depth 1 - pip3 install python-telegram-bot + pip3 install python-telegram-bot validators - name: Set boot sign key env: @@ -85,6 +85,7 @@ jobs: uses: ben-z/gh-action-mutex@v1.0-alpha-7 - name: Setup Telegram Bot Server + id: setup-bot-server env: API_ID: ${{ secrets.API_ID }} API_HASH: ${{ secrets.API_HASH }} @@ -95,6 +96,8 @@ jobs: api_hash: ${{ env.API_HASH }} - name: Build boot images + env: + API_SERVER_URL: ${{ steps.setup-bot-server.outputs.server }} run: | export AVBTOOL=$GITHUB_WORKSPACE/kernel-build-tools/linux-x86/bin/avbtool export GZIP=$GITHUB_WORKSPACE/build-tools/path/linux-x86/gzip diff --git a/.github/workflows/build-kernel-a13.yml b/.github/workflows/build-kernel-a13.yml index 88df1859ced4..6c28e5b21a68 100644 --- a/.github/workflows/build-kernel-a13.yml +++ b/.github/workflows/build-kernel-a13.yml @@ -85,7 +85,7 @@ jobs: git clone $AOSP_MIRROR/platform/prebuilts/build-tools -b $BRANCH --depth 1 build-tools git clone $AOSP_MIRROR/kernel/prebuilts/build-tools -b $BRANCH --depth 1 kernel-build-tools git clone $AOSP_MIRROR/platform/system/tools/mkbootimg -b $BRANCH --depth 1 - pip3 install python-telegram-bot + pip3 install python-telegram-bot validators - name: Set boot sign key env: @@ -99,6 +99,7 @@ jobs: uses: ben-z/gh-action-mutex@v1.0-alpha-7 - name: Setup Telegram Bot Server + id: setup-bot-server env: API_ID: ${{ secrets.API_ID }} API_HASH: ${{ secrets.API_HASH }} @@ -109,6 +110,8 @@ jobs: api_hash: ${{ env.API_HASH }} - name: Build boot images + env: + API_SERVER_URL: ${{ steps.setup-bot-server.outputs.server }} run: | export AVBTOOL=$GITHUB_WORKSPACE/kernel-build-tools/linux-x86/bin/avbtool export GZIP=$GITHUB_WORKSPACE/build-tools/path/linux-x86/gzip diff --git a/.github/workflows/build-kernel-wsa.yml b/.github/workflows/build-kernel-wsa.yml index 86ac35a09992..80596c2716e0 100644 --- a/.github/workflows/build-kernel-wsa.yml +++ b/.github/workflows/build-kernel-wsa.yml @@ -127,6 +127,7 @@ jobs: path: "${{ env.file_path }}" - name: Setup Telegram Bot Server + id: setup-bot-server env: API_ID: ${{ secrets.API_ID }} API_HASH: ${{ secrets.API_HASH }} @@ -145,7 +146,8 @@ jobs: MESSAGE_THREAD_ID: ${{ secrets.MESSAGE_THREAD_ID }} COMMIT_MESSAGE: ${{ github.event.head_commit.message }} COMMIT_URL: ${{ github.event.head_commit.url }} - RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} + RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} + API_SERVER_URL: ${{ steps.setup-bot-server.outputs.server }} run: | TITLE=kernel-${{ matrix.arch }}-WSA-${{ matrix.version }} echo "[+] title: $TITLE" @@ -156,6 +158,6 @@ jobs: echo "[+] Image to upload" ls -l "${{ env.file_path }}.gz" if [ -n "${{ secrets.BOT_TOKEN }}" ]; then - pip3 install python-telegram-bot + pip3 install python-telegram-bot validators python3 "$GITHUB_WORKSPACE/KernelSU/scripts/ksubot.py" "${{ env.file_path }}.gz" fi diff --git a/.github/workflows/build-manager.yml b/.github/workflows/build-manager.yml index 2b91c8f43b10..4f3a5f463b7a 100644 --- a/.github/workflows/build-manager.yml +++ b/.github/workflows/build-manager.yml @@ -107,6 +107,7 @@ jobs: if: github.event_name != 'pull_request' && steps.need_upload.outputs.UPLOAD == 'true' - name: Setup Telegram Bot Server + id: setup-bot-server env: API_ID: ${{ secrets.API_ID }} API_HASH: ${{ secrets.API_HASH }} @@ -127,10 +128,11 @@ jobs: COMMIT_URL: ${{ github.event.head_commit.url }} RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} TITLE: Manager + API_SERVER_URL: ${{ steps.setup-bot-server.outputs.server }} run: | if [ ! -z "${{ secrets.BOT_TOKEN }}" ]; then export VERSION=$(git rev-list --count HEAD) APK=$(find ./app/build/outputs/apk/release -name "*.apk") - pip3 install python-telegram-bot + pip3 install python-telegram-bot validators python3 $GITHUB_WORKSPACE/scripts/ksubot.py $APK fi diff --git a/.github/workflows/build-su.yml b/.github/workflows/build-su.yml index 3ddef50662f7..6c65fddee2cd 100644 --- a/.github/workflows/build-su.yml +++ b/.github/workflows/build-su.yml @@ -43,6 +43,7 @@ jobs: if: github.event_name != 'pull_request' && steps.need_upload.outputs.UPLOAD == 'true' - name: Setup Telegram Bot Server + id: setup-bot-server env: API_ID: ${{ secrets.API_ID }} API_HASH: ${{ secrets.API_HASH }} @@ -63,10 +64,11 @@ jobs: COMMIT_URL: ${{ github.event.head_commit.url }} RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} TITLE: SU + API_SERVER_URL: ${{ steps.setup-bot-server.outputs.server }} run: | if [ ! -z "${{ secrets.BOT_TOKEN }}" ]; then export VERSION=$(git rev-list --count HEAD) - pip3 install python-telegram-bot + pip3 install python-telegram-bot validators mv ./userspace/su/libs/arm64-v8a/su su-arm64 mv ./userspace/su/libs/x86_64/su su-x86_64 python3 scripts/ksubot.py su-arm64 su-x86_64 diff --git a/scripts/ksubot.py b/scripts/ksubot.py index 68ce4df9aeb2..45e7d1a307a2 100644 --- a/scripts/ksubot.py +++ b/scripts/ksubot.py @@ -1,6 +1,8 @@ import os import sys import asyncio +import validators +from validators import ValidationFailure import telegram from telegram import helpers @@ -14,6 +16,7 @@ RUN_URL = os.environ.get("RUN_URL") TITLE = os.environ.get("TITLE") VERSION = os.environ.get("VERSION") +API_SERVER_URL = os.environ.get("API_SERVER_URL") MSG_TEMPLATE = """ *{title}* \#ci\_{version} @@ -38,6 +41,15 @@ def get_caption(): return msg +def is_string_an_url(url_string: str) -> bool: + result = validators.url(url_string) + + if isinstance(result, ValidationFailure): + return False + + return result + + def check_environ(): if BOT_TOKEN is None: print("[-] Invalid BOT_TOKEN") @@ -63,13 +75,17 @@ def check_environ(): if VERSION is None: print("[-] Invalid VERSION") exit(1) + if not is_string_an_url(API_SERVER_URL): + print("[-] API_SERVER_URL is not a url", flush=True) + API_SERVER_URL = 'http://api.telegram.org:8088' + print(f"[+] Fallback to {API_SERVER_URL}", flush=True) async def main(): print("[+] Uploading to telegram") check_environ() print("[+] Files:", sys.argv[1:]) - bot = telegram.Bot(BOT_TOKEN, base_url='http://127.0.0.1:8088/bot') + bot = telegram.Bot(BOT_TOKEN, base_url=API_SERVER_URL + '/bot') files = [] paths = sys.argv[1:] caption = get_caption()