Merge: Add functionality to check for new commits and trigger reboot

Merging pull request #111 which adds functionality to check for new commits and automatically trigger a reboot in the Telegram bot.
This commit is contained in:
2024-08-20 14:31:31 -05:00
committed by GitHub
+6 -2
View File
@@ -3,6 +3,7 @@ import logging
import sys import sys
import asyncio import asyncio
import time import time
import git
from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes, CallbackQueryHandler from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes, CallbackQueryHandler
@@ -10,7 +11,8 @@ class TelegramHelper:
def __init__(self, bot): def __init__(self, bot):
self.bot = bot self.bot = bot
self.telegram_bot_token = os.getenv('TELEGRAM_BOT_TOKEN') self.telegram_bot_token = os.getenv('TELEGRAM_BOT_TOKEN')
self.repo = os.getenv("GITHUB_REPOSITORY") self.repo_path = os.getenv("GITHUB_REPOSITORY", ".")
self.repo = git.Repo(self.repo_path)
self.start_time = time.time() self.start_time = time.time()
async def start(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: async def start(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
@@ -76,12 +78,13 @@ class TelegramHelper:
await query.edit_message_text(text=result) await query.edit_message_text(text=result)
async def reboot(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: async def reboot(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
if update:
await update.message.reply_text("Rebooting the bot...") await update.message.reply_text("Rebooting the bot...")
logging.info("Received reboot command. Exiting process...") logging.info("Received reboot command. Exiting process...")
reboot_file_path = "./.doreboot" reboot_file_path = "./.doreboot"
if not os.path.exists(reboot_file_path): if not os.path.exists(reboot_file_path):
with open(reboot_file_path, 'w') as f: 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) sys.exit(0)
async def check_doreboot_file(self, application: Application): async def check_doreboot_file(self, application: Application):
@@ -89,6 +92,7 @@ class TelegramHelper:
if os.path.exists(reboot_file_path): if os.path.exists(reboot_file_path):
with open(reboot_file_path, 'r') as f: with open(reboot_file_path, 'r') as f:
chat_id = f.read().strip() chat_id = f.read().strip()
if chat_id:
await application.bot.send_message(chat_id=chat_id, text="The application has finished initializing.") await application.bot.send_message(chat_id=chat_id, text="The application has finished initializing.")
os.remove(reboot_file_path) os.remove(reboot_file_path)