Add ntfy support
All checks were successful
Build and Deploy / build (push) Successful in 2m37s

This commit is contained in:
2025-04-20 00:05:11 +02:00
parent ee32057c0b
commit 88ac296393
2 changed files with 198 additions and 14 deletions

88
main.py
View File

@@ -5,10 +5,72 @@ from telegram.ext import Application, CommandHandler, CallbackQueryHandler, Cont
from uptime_kuma_api import MonitorStatus
import api.kuma as kuma
import api.torrent as torrent
import api.ntfy as ntfy
import threading
TOKEN = "7396669954:AAH8_I0Y-qg3j_LfbUdRTOLPDKh80NdijMo"
AUTHORIZED_USER_ID = 634303772 # Replace with your Telegram user ID
NTFY_SERVER = "http://192.168.1.2:54720"
NTFY_AUTH_HEADER = "tk_cdmwd6ix255g3qgo4dx3r0gakw4y3"
DB_PATH = "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))
# --- Menu Definitions ---
@@ -40,18 +102,6 @@ def status_menu_keyboard():
)
# --- Command Handlers ---
async def start(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}")
def format_torrents(torrents):
if len(torrents) == 0:
return "No torrents."
@@ -157,13 +207,23 @@ def main():
"http://192.168.1.17:8112", "tMHNjrJr7nhjyhJrYsahi4anq2h6LJ"
)
ntfy_api = ntfy.NtfyAPI(DB_PATH, NTFY_SERVER, NTFY_AUTH_HEADER, AUTHORIZED_USER_ID)
app = Application.builder().token(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("start", start))
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()