From 8bee64cee12dd1cee230f3797b2f0fa9b968b9fb Mon Sep 17 00:00:00 2001 From: bucolucas Date: Tue, 20 Aug 2024 14:28:51 -0500 Subject: [PATCH 1/2] Add functionality to check for new commits and trigger reboot --- telegram_helper.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/telegram_helper.py b/telegram_helper.py index fff9c23..b19e983 100644 --- a/telegram_helper.py +++ b/telegram_helper.py @@ -3,6 +3,7 @@ import logging import sys import asyncio import time +import git from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes, CallbackQueryHandler @@ -10,7 +11,8 @@ class TelegramHelper: def __init__(self, bot): self.bot = bot self.telegram_bot_token = os.getenv('TELEGRAM_BOT_TOKEN') - self.repo = os.getenv("GITHUB_REPOSITORY") + self.repo_path = os.getenv("GITHUB_WORKSPACE", ".") + self.repo = git.Repo(self.repo_path) self.start_time = time.time() async def start(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: @@ -76,12 +78,13 @@ class TelegramHelper: await query.edit_message_text(text=result) async def reboot(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: - await update.message.reply_text("Rebooting the bot...") + if update: + await update.message.reply_text("Rebooting the bot...") logging.info("Received reboot command. Exiting process...") reboot_file_path = "./.doreboot" if not os.path.exists(reboot_file_path): with open(reboot_file_path, 'w') as f: - f.write(str(update.effective_chat.id)) + f.write(str(update.effective_chat.id) if update else "") sys.exit(0) async def check_doreboot_file(self, application: Application): @@ -89,7 +92,8 @@ class TelegramHelper: if os.path.exists(reboot_file_path): with open(reboot_file_path, 'r') as f: chat_id = f.read().strip() - await application.bot.send_message(chat_id=chat_id, text="The application has finished initializing.") + if chat_id: + await application.bot.send_message(chat_id=chat_id, text="The application has finished initializing.") os.remove(reboot_file_path) async def check_for_new_commits(self): From 42e86d5fb35dbefbdcc173546dfc29ba72ee4552 Mon Sep 17 00:00:00 2001 From: bucolucas Date: Tue, 20 Aug 2024 14:31:01 -0500 Subject: [PATCH 2/2] Update telegram_helper.py --- telegram_helper.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/telegram_helper.py b/telegram_helper.py index b19e983..eb96df1 100644 --- a/telegram_helper.py +++ b/telegram_helper.py @@ -11,7 +11,7 @@ class TelegramHelper: def __init__(self, bot): self.bot = bot self.telegram_bot_token = os.getenv('TELEGRAM_BOT_TOKEN') - self.repo_path = os.getenv("GITHUB_WORKSPACE", ".") + self.repo_path = os.getenv("GITHUB_REPOSITORY", ".") self.repo = git.Repo(self.repo_path) self.start_time = time.time() @@ -128,4 +128,4 @@ class TelegramHelper: # Start the commit checking task asyncio.get_event_loop().create_task(self.check_for_new_commits()) - application.run_polling() \ No newline at end of file + application.run_polling()