Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci: Send media groups directly using the Local Bot API Server #600

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion .github/workflows/build-kernel-a12.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -84,7 +84,20 @@ jobs:
- name: Setup mutex for uploading
uses: ben-z/[email protected]

- name: Setup Telegram Bot Server
id: setup-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
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
Expand Down
17 changes: 15 additions & 2 deletions .github/workflows/build-kernel-a13.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -98,7 +98,20 @@ jobs:
- name: Setup mutex for uploading
uses: ben-z/[email protected]

- name: Setup Telegram Bot Server
id: setup-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
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
Expand All @@ -110,7 +123,7 @@ jobs:
echo "VERSION: $VERSION"
cd -
bash $GITHUB_WORKSPACE/KernelSU/.github/scripts/build_a13.sh

- name: Display structure of boot files
run: ls -R

Expand Down
16 changes: 14 additions & 2 deletions .github/workflows/build-kernel-wsa.yml
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,17 @@ jobs:
name: kernel-WSA-${{ matrix.arch }}-${{ matrix.version }}
path: "${{ env.file_path }}"

- name: Setup Telegram Bot Server
id: setup-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:
Expand All @@ -135,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"
Expand All @@ -146,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
14 changes: 13 additions & 1 deletion .github/workflows/build-manager.yml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,17 @@ jobs:
uses: ben-z/[email protected]
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 }}
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:
Expand All @@ -117,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
15 changes: 14 additions & 1 deletion .github/workflows/build-su.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ jobs:
- name: Setup mutex for uploading
uses: ben-z/[email protected]
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 }}
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:
Expand All @@ -52,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
Expand Down
35 changes: 24 additions & 11 deletions scripts/ksubot.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import os
import sys
import asyncio
import validators
from validators import ValidationFailure
import telegram
from telegram import helpers

Expand All @@ -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}
Expand All @@ -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")
Expand All @@ -63,33 +75,35 @@ 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)
bot = telegram.Bot(BOT_TOKEN, base_url=API_SERVER_URL + '/bot')
files = []
paths = sys.argv[1:]
caption = get_caption()
print("[+] Caption: ")
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!")
Expand All @@ -98,4 +112,3 @@ async def main():
if __name__ == "__main__":
loops = asyncio.new_event_loop()
loops.run_until_complete(asyncio.wait([main()]))