Files
Jarvis/main.py
MrZaiko 5b01609a2f
All checks were successful
Build and Deploy / build (push) Successful in 2m14s
Fix Dockerfile and declutter main.py
2025-04-20 11:03:09 +02:00

120 lines
3.6 KiB
Python

#!/usr/bin/env python3
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update
from telegram.ext import Application, CommandHandler, CallbackQueryHandler, ContextTypes
from uptime_kuma_api import MonitorStatus
import api.kuma as kuma
import api.torrent as torrent
import api.ntfy as ntfy
import threading
from config import (
BOT_TOKEN,
AUTHORIZED_USER_ID,
NTFY_AUTH_HEADER,
KUMA_API_PASSWORD,
TORRENT_API_PASSWORD,
)
from menus import (
main_menu_keyboard,
torrents_menu_keyboard,
status_menu_keyboard,
handle_menu,
)
NTFY_SERVER = "http://192.168.1.2:54720"
DB_PATH = "config/subscriptions.db"
# --- Command Handlers ---
async def menu(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
try:
await update.message.reply_text(
"Choose an option:", reply_markup=main_menu_keyboard()
)
except Exception as e:
await update.message.reply_text(f"An error occurred: {e}")
# Optionally log the error
print(f"Error: {e}")
async def info(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
user_id = update.message.from_user.id
await update.message.reply_text(f"Your UserID is {user_id}.")
async def subscribe(update: Update, context: ContextTypes.DEFAULT_TYPE):
n_api = context.bot_data.get("ntfy_api", {})
if update.effective_user.id != AUTHORIZED_USER_ID:
return
if not context.args:
await update.message.reply_text("Usage: /subscribe <topic>")
return
topic = context.args[0]
n_api.add_subscription(topic)
await update.message.reply_text(f"Subscribed to topic: {topic}")
async def unsubscribe(update: Update, context: ContextTypes.DEFAULT_TYPE):
n_api = context.bot_data.get("ntfy_api", {})
if update.effective_user.id != AUTHORIZED_USER_ID:
return
if not context.args:
await update.message.reply_text("Usage: /unsubscribe <topic>")
return
topic = context.args[0]
n_api.remove_subscription(topic)
await update.message.reply_text(f"Unsubscribed from topic: {topic}")
async def list_subs(update: Update, context: ContextTypes.DEFAULT_TYPE):
n_api = context.bot_data.get("ntfy_api", {})
if update.effective_user.id != AUTHORIZED_USER_ID:
return
topics = n_api.get_subscriptions()
if not topics:
await update.message.reply_text("You are not subscribed to any topics.")
else:
await update.message.reply_text("Subscribed topics:\n" + "\n".join(topics))
# --- Main Function ---
def main():
print("Starting Jarvis...")
# Initiate api
kuma_api = kuma.KumaAPI("http://192.168.1.2:36667", KUMA_API_PASSWORD)
torrent_api = torrent.TorrentApi(
"http://192.168.1.17:8112", TORRENT_API_PASSWORD, username="MrZaiko"
)
ntfy_api = ntfy.NtfyAPI(DB_PATH, NTFY_SERVER, NTFY_AUTH_HEADER, AUTHORIZED_USER_ID)
app = Application.builder().token(BOT_TOKEN).build()
app.bot_data["kuma_api"] = kuma_api
app.bot_data["torrent_api"] = torrent_api
app.bot_data["ntfy_api"] = ntfy_api
app.add_handler(CommandHandler("menu", menu))
app.add_handler(CallbackQueryHandler(handle_menu))
app.add_handler(CommandHandler("info", info))
app.add_handler(CommandHandler("subscribe", subscribe))
app.add_handler(CommandHandler("unsubscribe", unsubscribe))
app.add_handler(CommandHandler("list", list_subs))
threading.Thread(target=ntfy_api.ntfy_listener, args=(app,), daemon=True).start()
print("Bot is running... Press Ctrl+C to stop.")
app.run_polling()
if __name__ == "__main__":
main()