diff --git a/.github/scripts/README.md b/.github/scripts/README.md new file mode 100755 index 0000000..bf4986c --- /dev/null +++ b/.github/scripts/README.md @@ -0,0 +1,28 @@ +# Auto Rom Poster Bot + +A simple Post Bot written in [Python]('https://www.python.org') using [pyTelegramBotAPI]('https://pypi.org/project/pyTelegramBotAPI') to post rom updates to telegram channel whenever there is an OTA update. + +## Instructions + +### 1. Adding secrets + +Go to your repo `settings > secrets > new repository secret`, and add these secrets. + +- `BOT_TOKEN`: Telegram bot token +- `CHAT_ID`: Telegram group/channel chat ID where the rom needs to be posted +- `GH_TOKEN`: Github access token to push changes to repo + +**Note:** Bot should be added in the group/channel where the rom needs to be posted + +### 2. Running the bot + +After adding secrets the next thing is to run the bot. + +**We use Github Actions to run the bot** + +- Actions will automatically run if any changes are committed to the repo and rom will be posted +- You can also run the bot by going to `actions > Rom Poster Bot Runner > workflow-dispatch` and tap on run + +## Credits + +- [Ashwin DS](https://github.com/geek0609) diff --git a/.github/scripts/file_ids.txt b/.github/scripts/file_ids.txt new file mode 100755 index 0000000..e69de29 diff --git a/.github/scripts/post.py b/.github/scripts/post.py new file mode 100755 index 0000000..7de2ea0 --- /dev/null +++ b/.github/scripts/post.py @@ -0,0 +1,187 @@ +#!/usr/bin/env python +# +# Python code which automatically posts Message in a Telegram Group if any new update is found. +# Intended to be run on every push +# USAGE : python3 post.py +# See README for more. +# +# Copyright (C) 2024 PrajjuS +# +# Credits: Ashwin DS +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, see . + +import telebot +import os +import json +import datetime +from time import sleep + +# Get configs from workflow secrets +def getConfig(config_name: str): + return os.getenv(config_name) +try: + BOT_TOKEN = getConfig("BOT_TOKEN") + CHAT_ID = getConfig("CHAT_ID") +except KeyError: + print("Fill all the configs plox..\nExiting...") + exit(0) + +BANNER_PATH = "./assets/banner.jpg" + +# Init bot +bot = telebot.TeleBot(BOT_TOKEN, parse_mode="HTML") + +# File directories +jsonDir = { + "Gapps": "builds/Gapps", + "Vanilla": "builds/Vanilla" +} +idDir = ".github/scripts" + +# Store IDs in a file to compare +def update(IDs): + with open(f"{idDir}/file_ids.txt", "w+") as log: + for ids in IDs: + log.write(f"{str(ids)}\n") + +# Return IDs of all latest files from json files +def get_new_id(): + files = [] + file_id = [] + for type, dirName in jsonDir.items(): + for all in os.listdir(dirName): + if all.endswith('.json'): + files.append({"type": type, "dir": dirName, "file": all}) + for all_files in files: + with open(f"{all_files['dir']}/{all_files['file']}", "r") as file: + data = json.loads(file.read())['response'][0] + file_id.append(data['md5']) + return file_id + +# Return previous IDs +def get_old_id(): + old_id = [] + with open(f"{idDir}/file_ids.txt", "r") as log: + for ids in log.readlines(): + old_id.append(ids.replace("\n", "")) + return old_id + +# Remove elements in 2nd list from 1st, helps to find out which device got an update +def get_diff(new_id, old_id): + first_set = set(new_id) + sec_set = set(old_id) + return list(first_set - sec_set) + +# Grab needed info using ID of the file +def get_info(ID): + files = [] + for type, dirName in jsonDir.items(): + for all in os.listdir(dirName): + if all.endswith('.json'): + files.append({"type": type, "dir": dirName, "file": all}) + for all_files in files: + with open(f"{all_files['dir']}/{all_files['file']}", "r") as file: + data = json.loads(file.read())['response'][0] + if data['md5'] == ID: + device = all_files['file'] + build_type = all_files['type'] + break + with open(f"{jsonDir[build_type]}/{device}") as device_file: + info = json.loads(device_file.read())['response'][0] + EUCLID_VERSION = info['version'] + OEM = info['oem'] + DEVICE_NAME = info['device'] + DEVICE_CODENAME = device.split('.')[0] + MAINTAINER = info['maintainer'] + DATE_TIME = datetime.datetime.fromtimestamp(int(info['timestamp'])) + FILENAME = info['filename'] + DOWNLOAD_URL = info['download'] + BUILD_TYPE = info['buildtype'] + SIZE = round(int(info['size'])/1000000000, 2) + MD5 = info['md5'] + SHA256 = info['sha256'] + XDA = info['forum'] + TELEGRAM = info['telegram'] + msg = "" + msg += f"Euclid-OS {EUCLID_VERSION}\n" + msg += f"Device Name: {OEM} {DEVICE_NAME} ({DEVICE_CODENAME})\n" + msg += f"Maintainer: {MAINTAINER}\n" + msg += f"Date Time: {DATE_TIME}\n" + # msg += f"Download URL: {DOWNLOAD_URL}\n" + msg += f"Build Type: {BUILD_TYPE}\n" + msg += f"Size: {SIZE}G\n" + msg += f"MD5: {MD5}\n" + msg += f"SHA256: {SHA256}\n" + msg += f"XDA: {XDA}\n" + msg += f"Telegram: {TELEGRAM}\n\n" + print(msg) + return { + "version": EUCLID_VERSION, + "oem": OEM, + "device_name": DEVICE_NAME, + "codename": DEVICE_CODENAME, + "maintainer": MAINTAINER, + "datetime": DATE_TIME, + "filename": FILENAME, + "download": DOWNLOAD_URL, + "buildtype": BUILD_TYPE, + "size": SIZE, + "md5": MD5, + "sha256": SHA256, + "xda": XDA, + "telegram": TELEGRAM + } + +# Prepare function for posting message in channel +def send_post(chat_id, image, caption): + return bot.send_photo(chat_id=chat_id, photo=image, caption=caption) + +# Prepare message format for channel +def message_content(information): + msg = "" + msg += f"EuclidOS {information['version']} - OFFICIAL | Android 14\n" + msg += f"📲 : {information['oem']} {information['device_name']} ({information['codename']})\n" + msg += f"👤 : {information['maintainer']}\n" + msg += f"🗓 : {information['datetime']} UTC\n\n" + msg += f"▪️ Build Variant: {information['buildtype']}\n" + msg += f"▪️ Changelog: Source | Device\n" + msg += f"▪️ Download: Here\n\n" + msg += f"▪️ Rom Support: Here\n" + msg += f"▪️ Device Support: Here\n" + msg += f"\n#euclidOS #{information['codename']} #Android14 #Official" + return msg + +# Send updates to channel and commit changes in repo +def tg_message(): + commit_message = "Update new IDs and push OTA" + commit_description = "Data for following device(s) were changed:\n" + if len(get_diff(get_new_id(), get_old_id())) == 0: + print("All are Updated\nNothing to do\nExiting...") + sleep(2) + exit(1) + else: + print(f"IDs Changed:\n{get_diff(get_new_id(), get_old_id())}\n\n") + for devices in get_diff(get_new_id(), get_old_id()): + info = get_info(devices) + with open(BANNER_PATH, "rb") as image: + send_post(CHAT_ID, image, message_content(info)) + commit_description += f"- {info['device_name']} ({info['codename']})\n" + sleep(5) + update(get_new_id()) + open("commit_mesg.txt", "w+").write(f"EuclidOTA: {commit_message} [BOT]\n\n{commit_description}") + +# Final stuffs +tg_message() +print("Successful") +sleep(2) diff --git a/.github/workflows/post-bot.yml b/.github/workflows/post-bot.yml new file mode 100755 index 0000000..1c31954 --- /dev/null +++ b/.github/workflows/post-bot.yml @@ -0,0 +1,44 @@ +name: Rom Poster Bot + +on: + push: + paths: + - "builds/gapps/*.json" + - "builds/vanilla/*.json" + workflow_dispatch: + +env: + GitHubMail: "euclidosbot@gmail.com" + GitHubName: "EuclidOS-Bot" + GitHubToken: ${{ secrets.GH_TOKEN }} + BOT_TOKEN: ${{ secrets.BOT_TOKEN }} + CHAT_ID: ${{ secrets.CHAT_ID }} + +jobs: + post: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + + - name: Install Dependencies + run: sudo pip install pyTelegramBotAPI + + - name: Set Git Configs & Secrets + run: | + git config --global user.email ${GitHubMail} + git config --global user.name ${GitHubName} + git config --global color.ui true + git config --global credential.helper store + echo "https://${GitHubName}:${GitHubToken}@github.com" > ~/.git-credentials + + - name: Run RomPosterBot + run: sudo -E python3 .github/scripts/post.py + + - name: Commit and Push Changes + run: | + git fetch && git pull + COMMIT_MESSAGE=$(cat commit_mesg.txt) + rm commit_mesg.txt + git add . && git commit -sm "$COMMIT_MESSAGE" + git push origin