Skip to content

Commit

Permalink
ci: replace python-telegram-bot with telethon (#1076)
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonKhew96 authored Oct 23, 2023
1 parent bf823a2 commit f349507
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 117 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/build-kernel-a12.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ jobs:
if: ${{ ( github.event_name != 'pull_request' && github.ref == 'refs/heads/main' ) || github.ref_type == 'tag' || github.ref == 'refs/heads/ci' }}
env:
CHAT_ID: ${{ secrets.CHAT_ID }}
CACHE_CHAT_ID: ${{ secrets.CACHE_CHAT_ID }}
BOT_TOKEN: ${{ secrets.BOT_TOKEN }}
MESSAGE_THREAD_ID: ${{ secrets.MESSAGE_THREAD_ID }}
COMMIT_MESSAGE: ${{ github.event.head_commit.message }}
Expand All @@ -83,7 +82,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 telethon==1.31.1
- name: Set boot sign key
env:
Expand Down
3 changes: 1 addition & 2 deletions .github/workflows/build-kernel-a13.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ jobs:
if: ${{ ( github.event_name != 'pull_request' && github.ref == 'refs/heads/main' ) || github.ref_type == 'tag' || github.ref == 'refs/heads/ci' }}
env:
CHAT_ID: ${{ secrets.CHAT_ID }}
CACHE_CHAT_ID: ${{ secrets.CACHE_CHAT_ID }}
BOT_TOKEN: ${{ secrets.BOT_TOKEN }}
MESSAGE_THREAD_ID: ${{ secrets.MESSAGE_THREAD_ID }}
COMMIT_MESSAGE: ${{ github.event.head_commit.message }}
Expand All @@ -106,7 +105,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 telethon==1.31.1
- name: Set boot sign key
env:
Expand Down
3 changes: 1 addition & 2 deletions .github/workflows/build-kernel-a14.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ jobs:
if: ${{ ( github.event_name != 'pull_request' && github.ref == 'refs/heads/main' ) || github.ref_type == 'tag' || github.ref == 'refs/heads/ci' }}
env:
CHAT_ID: ${{ secrets.CHAT_ID }}
CACHE_CHAT_ID: ${{ secrets.CACHE_CHAT_ID }}
BOT_TOKEN: ${{ secrets.BOT_TOKEN }}
MESSAGE_THREAD_ID: ${{ secrets.MESSAGE_THREAD_ID }}
COMMIT_MESSAGE: ${{ github.event.head_commit.message }}
Expand All @@ -67,7 +66,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 telethon==1.31.1
- name: Set boot sign key
env:
Expand Down
3 changes: 1 addition & 2 deletions .github/workflows/build-kernel-arcvm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ jobs:
if: ${{ ( github.event_name == 'push' && github.ref == 'refs/heads/main' ) || github.ref_type == 'tag' }}
env:
CHAT_ID: ${{ secrets.CHAT_ID }}
CACHE_CHAT_ID: ${{ secrets.CACHE_CHAT_ID }}
BOT_TOKEN: ${{ secrets.BOT_TOKEN }}
MESSAGE_THREAD_ID: ${{ secrets.MESSAGE_THREAD_ID }}
COMMIT_MESSAGE: ${{ github.event.head_commit.message }}
Expand All @@ -135,6 +134,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 telethon==1.31.1
python3 "$GITHUB_WORKSPACE/KernelSU/scripts/ksubot.py" "${{ env.file_path }}.gz"
fi
3 changes: 1 addition & 2 deletions .github/workflows/build-kernel-wsa.yml
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ jobs:
if: github.event_name == 'push' && github.ref == 'refs/heads/main' || github.ref_type == 'tag'
env:
CHAT_ID: ${{ secrets.CHAT_ID }}
CACHE_CHAT_ID: ${{ secrets.CACHE_CHAT_ID }}
BOT_TOKEN: ${{ secrets.BOT_TOKEN }}
MESSAGE_THREAD_ID: ${{ secrets.MESSAGE_THREAD_ID }}
COMMIT_MESSAGE: ${{ github.event.head_commit.message }}
Expand All @@ -129,6 +128,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 telethon==1.31.1
python3 "$GITHUB_WORKSPACE/KernelSU/scripts/ksubot.py" "${{ env.file_path }}.gz"
fi
3 changes: 1 addition & 2 deletions .github/workflows/build-manager.yml
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ jobs:
if: github.event_name != 'pull_request' && steps.need_upload.outputs.UPLOAD == 'true'
env:
CHAT_ID: ${{ secrets.CHAT_ID }}
CACHE_CHAT_ID: ${{ secrets.CACHE_CHAT_ID }}
BOT_TOKEN: ${{ secrets.BOT_TOKEN }}
MESSAGE_THREAD_ID: ${{ secrets.MESSAGE_THREAD_ID }}
COMMIT_MESSAGE: ${{ github.event.head_commit.message }}
Expand All @@ -121,6 +120,6 @@ jobs:
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 telethon==1.31.1
python3 $GITHUB_WORKSPACE/scripts/ksubot.py $APK
fi
3 changes: 1 addition & 2 deletions .github/workflows/build-su.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ jobs:
if: github.event_name != 'pull_request' && steps.need_upload.outputs.UPLOAD == 'true'
env:
CHAT_ID: ${{ secrets.CHAT_ID }}
CACHE_CHAT_ID: ${{ secrets.CACHE_CHAT_ID }}
BOT_TOKEN: ${{ secrets.BOT_TOKEN }}
MESSAGE_THREAD_ID: ${{ secrets.MESSAGE_THREAD_ID }}
COMMIT_MESSAGE: ${{ github.event.head_commit.message }}
Expand All @@ -55,7 +54,7 @@ jobs:
run: |
if [ ! -z "${{ secrets.BOT_TOKEN }}" ]; then
export VERSION=$(git rev-list --count HEAD)
pip3 install python-telegram-bot
pip3 install telethon==1.31.1
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
2 changes: 0 additions & 2 deletions .github/workflows/gki-kernel.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ on:
required: false
CHAT_ID:
required: false
CACHE_CHAT_ID:
required: false
BOT_TOKEN:
required: false
MESSAGE_THREAD_ID:
Expand Down
190 changes: 89 additions & 101 deletions scripts/ksubot.py
Original file line number Diff line number Diff line change
@@ -1,101 +1,89 @@
import os
import sys
import asyncio
import telegram
from telegram import helpers


BOT_TOKEN = os.environ.get("BOT_TOKEN")
CHAT_ID = os.environ.get("CHAT_ID")
CACHE_CHAT_ID = os.environ.get("CACHE_CHAT_ID")
MESSAGE_THREAD_ID = os.environ.get("MESSAGE_THREAD_ID")
COMMIT_URL = os.environ.get("COMMIT_URL")
COMMIT_MESSAGE = os.environ.get("COMMIT_MESSAGE")
RUN_URL = os.environ.get("RUN_URL")
TITLE = os.environ.get("TITLE")
VERSION = os.environ.get("VERSION")
MSG_TEMPLATE = """
*{title}*
\#ci\_{version}
```
{commit_message}
```
[Commit]({commit_url})
[Workflow run]({run_url})
""".strip()


def get_caption():
msg = MSG_TEMPLATE.format(
title=helpers.escape_markdown(TITLE, 2),
version=helpers.escape_markdown(VERSION, 2),
commit_message=helpers.escape_markdown(COMMIT_MESSAGE, 2, telegram.MessageEntity.PRE),
commit_url=helpers.escape_markdown(COMMIT_URL, 2, telegram.MessageEntity.TEXT_LINK),
run_url=helpers.escape_markdown(RUN_URL, 2, telegram.MessageEntity.TEXT_LINK)
)
if len(msg) > telegram.constants.MessageLimit.CAPTION_LENGTH:
return COMMIT_URL
return msg


def check_environ():
if BOT_TOKEN is None:
print("[-] Invalid BOT_TOKEN")
exit(1)
if CHAT_ID is None:
print("[-] Invalid CHAT_ID")
exit(1)
if CACHE_CHAT_ID is None:
print("[-] Invalid CACHE_CHAT_ID")
exit(1)
if COMMIT_URL is None:
print("[-] Invalid COMMIT_URL")
exit(1)
if COMMIT_MESSAGE is None:
print("[-] Invalid COMMIT_MESSAGE")
exit(1)
if RUN_URL is None:
print("[-] Invalid RUN_URL")
exit(1)
if TITLE is None:
print("[-] Invalid TITLE")
exit(1)
if VERSION is None:
print("[-] Invalid VERSION")
exit(1)


async def main():
print("[+] Uploading to telegram")
check_environ()
print("[+] Files:", sys.argv[1:])
bot = telegram.Bot(BOT_TOKEN)
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)
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,
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)
print("[+] Sending")
await bot.send_media_group(CHAT_ID, files, message_thread_id=MESSAGE_THREAD_ID)
print("[+] Done!")


if __name__ == "__main__":
loops = asyncio.new_event_loop()
loops.run_until_complete(asyncio.wait([main()]))

import asyncio
import os
import sys
from telethon import TelegramClient
from telethon.tl.functions.help import GetConfigRequest

API_ID = 611335
API_HASH = "d524b414d21f4d37f08684c1df41ac9c"


BOT_TOKEN = os.environ.get("BOT_TOKEN")
CHAT_ID = os.environ.get("CHAT_ID")
MESSAGE_THREAD_ID = os.environ.get("MESSAGE_THREAD_ID")
COMMIT_URL = os.environ.get("COMMIT_URL")
COMMIT_MESSAGE = os.environ.get("COMMIT_MESSAGE")
RUN_URL = os.environ.get("RUN_URL")
TITLE = os.environ.get("TITLE")
VERSION = os.environ.get("VERSION")
MSG_TEMPLATE = """
**{title}**
#ci_{version}
```
{commit_message}
```
[Commit]({commit_url})
[Workflow run]({run_url})
""".strip()


def get_caption():
msg = MSG_TEMPLATE.format(
title=TITLE,
version=VERSION,
commit_message=COMMIT_MESSAGE,
commit_url=COMMIT_URL,
run_url=RUN_URL,
)
if len(msg) > 1024:
return COMMIT_URL
return msg


def check_environ():
if BOT_TOKEN is None:
print("[-] Invalid BOT_TOKEN")
exit(1)
if CHAT_ID is None:
print("[-] Invalid CHAT_ID")
exit(1)
if COMMIT_URL is None:
print("[-] Invalid COMMIT_URL")
exit(1)
if COMMIT_MESSAGE is None:
print("[-] Invalid COMMIT_MESSAGE")
exit(1)
if RUN_URL is None:
print("[-] Invalid RUN_URL")
exit(1)
if TITLE is None:
print("[-] Invalid TITLE")
exit(1)
if VERSION is None:
print("[-] Invalid VERSION")
exit(1)


async def main():
print("[+] Uploading to telegram")
check_environ()
files = sys.argv[1:]
print("[+] Files:", files)
if len(files) <= 0:
print("[-] No files to upload")
exit(1)
print("[+] Logging in Telegram with bot")
async with await TelegramClient(session=None, api_id=API_ID, api_hash=API_HASH).start(bot_token=BOT_TOKEN) as bot:
caption = [""] * len(files)
caption[-1] = get_caption()
print("[+] Caption: ")
print("---")
print(caption)
print("---")
print("[+] Sending")
await bot.send_file(entity=CHAT_ID, file=files, caption=caption, reply_to=MESSAGE_THREAD_ID, parse_mode="markdown")
print("[+] Done!")
await bot.log_out()

if __name__ == "__main__":
asyncio.run(main())

0 comments on commit f349507

Please sign in to comment.