Backup automatico script del 2026-01-04 07:00
This commit is contained in:
424
services/telegram-bot/bot.py
Normal file → Executable file
424
services/telegram-bot/bot.py
Normal file → Executable file
@@ -5,9 +5,6 @@ import datetime
|
||||
import requests
|
||||
from functools import wraps
|
||||
from zoneinfo import ZoneInfo
|
||||
from dateutil import parser
|
||||
from typing import Dict, List, Optional, Tuple
|
||||
|
||||
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update
|
||||
from telegram.ext import (
|
||||
Application,
|
||||
@@ -18,34 +15,28 @@ from telegram.ext import (
|
||||
)
|
||||
|
||||
# =============================================================================
|
||||
# LOOGLE BOT V7.9 (ULTIMATE + GLOBAL GFS FIX)
|
||||
# - Dashboard Sistema
|
||||
# - Meteo Smart: Arome (EU), Icon (EU-Est), JMA (JP), GFS (Mondo)
|
||||
# - Multi-User Security
|
||||
# LOOGLE BOT V9.0 (ULTIMATE + CAMERAS + MODULAR)
|
||||
# - Dashboard Sistema (SSH/WOL/Monitor)
|
||||
# - Meteo Smart (Meteo.py / Previsione7.py)
|
||||
# - CCTV Hub (Cam.py + FFMPEG)
|
||||
# =============================================================================
|
||||
|
||||
# --- CONFIGURAZIONE ---
|
||||
# --- CONFIGURAZIONE AMBIENTE ---
|
||||
BOT_TOKEN = os.environ.get('BOT_TOKEN')
|
||||
|
||||
# Gestione Multi-Utente
|
||||
allowed_users_raw = os.environ.get('ALLOWED_USER_ID', '')
|
||||
ALLOWED_IDS = [int(x.strip()) for x in allowed_users_raw.split(',') if x.strip().isdigit()]
|
||||
|
||||
# Configurazione Sistema
|
||||
SSH_USER = "daniely"
|
||||
NAS_USER = "daniely"
|
||||
MASTER_IP = "192.168.128.80"
|
||||
|
||||
# Configurazione Meteo
|
||||
HOME_LAT = 43.9356
|
||||
HOME_LON = 12.4296
|
||||
HOME_NAME = "🏠 Casa (Strada Cà Toro)"
|
||||
TZ = "Europe/Rome"
|
||||
TZINFO = ZoneInfo(TZ)
|
||||
|
||||
OPEN_METEO_URL = "https://api.open-meteo.com/v1/forecast"
|
||||
GEOCODING_URL = "https://geocoding-api.open-meteo.com/v1/search"
|
||||
HTTP_HEADERS = {"User-Agent": "loogle-bot-v7.9"}
|
||||
# --- GESTIONE PERCORSI DINAMICA (DOCKER FRIENDLY) ---
|
||||
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
|
||||
METEO_SCRIPT = os.path.join(SCRIPT_DIR, "meteo.py")
|
||||
METEO7_SCRIPT = os.path.join(SCRIPT_DIR, "previsione7.py")
|
||||
CAM_SCRIPT = os.path.join(SCRIPT_DIR, "cam.py")
|
||||
|
||||
# --- LISTE DISPOSITIVI ---
|
||||
CORE_DEVICES = [
|
||||
@@ -67,17 +58,19 @@ INFRA_DEVICES = [
|
||||
{"name": "🔌 Sw Tav", "ip": "192.168.128.108"}
|
||||
]
|
||||
|
||||
# Configurazione Logging
|
||||
logging.basicConfig(format="%(asctime)s - %(levelname)s - %(message)s", level=logging.INFO)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# =============================================================================
|
||||
# SEZIONE 1: FUNZIONI SISTEMA (SSH, PING, UTILS)
|
||||
# SEZIONE 1: FUNZIONI UTILI E HELPER
|
||||
# =============================================================================
|
||||
|
||||
def run_cmd(command, ip=None, user=None):
|
||||
"""Esegue comandi shell locali o via SSH"""
|
||||
try:
|
||||
if ip == "127.0.0.1" or ip is None:
|
||||
return subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT, timeout=5).decode('utf-8').strip()
|
||||
return subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT, timeout=8).decode('utf-8').strip()
|
||||
else:
|
||||
safe_cmd = command.replace("'", "'\\''")
|
||||
full_cmd = f"ssh -o LogLevel=ERROR -o BatchMode=yes -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o ConnectTimeout=3 {user}@{ip} '{safe_cmd}'"
|
||||
@@ -104,7 +97,7 @@ def get_device_stats(device):
|
||||
elif dtype == "nas":
|
||||
t = run_cmd("cat /sys/class/hwmon/hwmon0/temp1_input 2>/dev/null || cat /sys/class/thermal/thermal_zone0/temp", ip, user)
|
||||
if t.isdigit():
|
||||
v = int(t); temp = f"{v/1000:.1f}°C" if v > 1000 else f"{v}°C"
|
||||
v = int(t); temp = f"{v/1000:.1f}°C" if v > 1000 else f"{v}°C"
|
||||
|
||||
if dtype == "nas": ram_cmd = "free | grep Mem | awk '{printf \"%.0f%%\", $3*100/$2}'"
|
||||
else: ram_cmd = "free -m | awk 'NR==2{if ($2>0) printf \"%.0f%%\", $3*100/$2; else print \"0%\"}'"
|
||||
@@ -118,225 +111,27 @@ def read_log_file(filepath, lines=15):
|
||||
except Exception as e: return f"Errore: {str(e)}"
|
||||
|
||||
def run_speedtest():
|
||||
try: return subprocess.check_output("speedtest-cli --simple", shell=True, timeout=50).decode('utf-8')
|
||||
try: return subprocess.check_output("speedtest-cli --simple", shell=True, timeout=60).decode('utf-8')
|
||||
except: return "Errore Speedtest"
|
||||
|
||||
# =============================================================================
|
||||
# SEZIONE 2: METEO INTELLIGENTE (MULTI-MODELLO MOSAICO)
|
||||
# =============================================================================
|
||||
|
||||
def now_local() -> datetime.datetime:
|
||||
return datetime.datetime.now(TZINFO)
|
||||
|
||||
def parse_time(t: str) -> datetime.datetime:
|
||||
dt = parser.isoparse(t)
|
||||
if dt.tzinfo is None: return dt.replace(tzinfo=TZINFO)
|
||||
return dt.astimezone(TZINFO)
|
||||
|
||||
def degrees_to_cardinal(d: int) -> str:
|
||||
dirs = ["N", "NE", "E", "SE", "S", "SW", "W", "NW"]
|
||||
return dirs[round(d / 45) % 8]
|
||||
|
||||
def get_icon_set(prec, snow, code, is_day, cloud, vis, temp, rain, gust, cape):
|
||||
sky = "☁️"
|
||||
if code in (95, 96, 99): sky = "⛈️" if prec > 0 else "🌩️"
|
||||
elif (code in (45, 48) or vis < 1000) and prec < 1: sky = "🌫️"
|
||||
elif prec >= 0.1: sky = "🌨️" if snow > 0 else "🌧️"
|
||||
elif cloud <= 20: sky = "☀️" if is_day else "🌙"
|
||||
elif cloud <= 40: sky = "🌤️" if is_day else "🌙"
|
||||
elif cloud <= 60: sky = "⛅️"
|
||||
elif cloud <= 80: sky = "🌥️"
|
||||
|
||||
sgx = "-"
|
||||
if snow > 0 or (code in (71,73,75,77,85,86) if code else False): sgx = "☃️"
|
||||
elif temp < 0 or (code in (66,67) if code else False): sgx = "🧊"
|
||||
elif cape > 2000: sgx = "🌪️"
|
||||
elif cape > 1000: sgx = "⚡"
|
||||
elif temp > 35: sgx = "🥵"
|
||||
elif rain > 4: sgx = "☔️"
|
||||
elif gust > 50: sgx = "💨"
|
||||
return sky, sgx
|
||||
|
||||
def get_coordinates(city_name: str):
|
||||
"""
|
||||
Cerca le coordinate della città con fallback EN.
|
||||
"""
|
||||
# 1. Tentativo ITALIANO
|
||||
params = {"name": city_name, "count": 10, "language": "it", "format": "json"}
|
||||
def call_script_text(script_path, args_list):
|
||||
"""Wrapper per lanciare script che restituiscono testo (Meteo)"""
|
||||
try:
|
||||
r = requests.get(GEOCODING_URL, params=params, headers=HTTP_HEADERS, timeout=10)
|
||||
data = r.json()
|
||||
if "results" in data and len(data["results"]) > 0:
|
||||
res = data["results"][0]
|
||||
cc = res.get("country_code", "IT").upper()
|
||||
name = f"{res.get('name')} ({cc})"
|
||||
return res["latitude"], res["longitude"], name, cc
|
||||
except Exception as e: logger.error(f"Geocoding IT error: {e}")
|
||||
|
||||
# 2. Tentativo FALLBACK INGLESE
|
||||
try:
|
||||
params["language"] = "en"
|
||||
r = requests.get(GEOCODING_URL, params=params, headers=HTTP_HEADERS, timeout=10)
|
||||
data = r.json()
|
||||
if "results" in data and len(data["results"]) > 0:
|
||||
res = data["results"][0]
|
||||
cc = res.get("country_code", "IT").upper()
|
||||
name = f"{res.get('name')} ({cc})"
|
||||
return res["latitude"], res["longitude"], name, cc
|
||||
except Exception as e: logger.error(f"Geocoding EN error: {e}")
|
||||
|
||||
return None
|
||||
|
||||
def choose_best_model(lat, lon, cc):
|
||||
"""
|
||||
Seleziona il modello migliore.
|
||||
Fallback Generale: NOAA GFS (perché ha sempre Visibilità/Cape).
|
||||
"""
|
||||
|
||||
# 1. GIAPPONE -> JMA MSM
|
||||
if cc == 'JP':
|
||||
return "jma_msm", "JMA MSM (5km)"
|
||||
|
||||
# 2. SCANDINAVIA -> Yr.no
|
||||
if cc in ['NO', 'SE', 'FI', 'DK', 'IS']:
|
||||
return "metno_nordic", "Yr.no (Nordic)"
|
||||
|
||||
# 3. UK & IRLANDA -> UK Met Office
|
||||
if cc in ['GB', 'IE']:
|
||||
return "ukmo_global", "UK MetOffice"
|
||||
|
||||
# 4. TUNING ITALIA (Mosaico)
|
||||
if cc == 'IT' or cc == 'SM':
|
||||
# ZONA 1: Nord-Ovest, Tirreno, Sardegna (Lon <= 13.0, Lat > 40.5)
|
||||
if lon <= 13.0 and lat > 40.5:
|
||||
return "meteofrance_arome_france_hd", "Arome HD"
|
||||
# ZONA 2: Nord-Est, Adriatico Nord/Centro
|
||||
if lat >= 43.0:
|
||||
return "icon_d2", "ICON-D2 (2km)"
|
||||
# ZONA 3: Sud Italia -> ICON-EU (Meglio di GFS per locale)
|
||||
return "icon_eu", "ICON-EU (7km)"
|
||||
|
||||
# 5. RESTO DEL MONDO (Europa Centrale ICON-D2)
|
||||
if cc in ['DE', 'AT', 'CH', 'LI']:
|
||||
return "icon_d2", "ICON-D2"
|
||||
|
||||
# 6. RESTO DEL MONDO -> NOAA GFS
|
||||
# Usiamo GFS invece di ECMWF perché ECMWF spesso manca di 'visibility'/'cape'
|
||||
# facendo crashare o svuotare il report. GFS è completo.
|
||||
return "gfs_global", "NOAA GFS Global"
|
||||
|
||||
def get_forecast(lat, lon, model):
|
||||
params = {
|
||||
"latitude": lat, "longitude": lon, "timezone": TZ,
|
||||
"forecast_days": 3,
|
||||
"models": model,
|
||||
"wind_speed_unit": "kmh", "precipitation_unit": "mm",
|
||||
"hourly": "temperature_2m,relative_humidity_2m,cloudcover,windspeed_10m,winddirection_10m,windgusts_10m,precipitation,rain,snowfall,weathercode,is_day,cape,visibility"
|
||||
}
|
||||
try:
|
||||
r = requests.get(OPEN_METEO_URL, params=params, headers=HTTP_HEADERS, timeout=25)
|
||||
r.raise_for_status()
|
||||
return r.json()
|
||||
except Exception as e: logger.error(f"Meteo API error: {e}"); return None
|
||||
|
||||
def safe_get_list(hourly_data, key, length, default=None):
|
||||
"""Estrae una lista sicura, gestendo chiavi mancanti"""
|
||||
if key in hourly_data and hourly_data[key] is not None:
|
||||
return hourly_data[key]
|
||||
return [default] * length
|
||||
|
||||
def generate_weather_report(lat, lon, location_name, cc="IT") -> str:
|
||||
model_id, model_name = choose_best_model(lat, lon, cc)
|
||||
|
||||
data = get_forecast(lat, lon, model_id)
|
||||
if not data: return f"❌ Errore API Meteo ({model_name})."
|
||||
|
||||
hourly = data.get("hourly", {})
|
||||
times = hourly.get("time", [])
|
||||
if not times: return "❌ Dati orari mancanti."
|
||||
|
||||
L = len(times)
|
||||
|
||||
try:
|
||||
l_temp = safe_get_list(hourly, "temperature_2m", L, 0)
|
||||
l_rh = safe_get_list(hourly, "relative_humidity_2m", L, 0)
|
||||
l_cl = safe_get_list(hourly, "cloudcover", L, 0)
|
||||
l_prec = safe_get_list(hourly, "precipitation", L, 0)
|
||||
l_rain = safe_get_list(hourly, "rain", L, 0)
|
||||
l_snow = safe_get_list(hourly, "snowfall", L, 0)
|
||||
l_wspd = safe_get_list(hourly, "windspeed_10m", L, 0)
|
||||
l_gust = safe_get_list(hourly, "windgusts_10m", L, 0)
|
||||
l_wdir = safe_get_list(hourly, "winddirection_10m", L, 0)
|
||||
l_code = safe_get_list(hourly, "weathercode", L, 0)
|
||||
l_day = safe_get_list(hourly, "is_day", L, 1)
|
||||
l_cape = safe_get_list(hourly, "cape", L, 0)
|
||||
l_vis = safe_get_list(hourly, "visibility", L, 10000)
|
||||
|
||||
except Exception as e:
|
||||
return f"❌ Errore elaborazione dati meteo: {e}"
|
||||
|
||||
now = now_local().replace(minute=0, second=0, microsecond=0)
|
||||
blocks = []
|
||||
|
||||
for (label, hours_duration, step) in [("Prime 24h", 24, 1), ("Successive 24h", 24, 2)]:
|
||||
end_time = now + datetime.timedelta(hours=hours_duration)
|
||||
lines = [f"{'LT':<2} {'T°':>3} {'h%':>3} {'mm':>2} {'Vento':<5} {'Nv%':>3} {'Sky':<2} {'Sgx':<3}", "-" * 30]
|
||||
count = 0
|
||||
for i, t_str in enumerate(times):
|
||||
try: dt = parse_time(t_str)
|
||||
except: continue
|
||||
if dt < now or dt >= end_time: continue
|
||||
if dt.hour % step != 0: continue
|
||||
|
||||
try:
|
||||
T = float(l_temp[i])
|
||||
Rh = int(l_rh[i] or 0)
|
||||
Cl = int(l_cl[i] or 0)
|
||||
Pr = float(l_prec[i] or 0)
|
||||
Rn = float(l_rain[i] or 0)
|
||||
Sn = float(l_snow[i] or 0)
|
||||
Wspd = float(l_wspd[i] or 0)
|
||||
Gust = float(l_gust[i] or 0)
|
||||
Wdir = int(l_wdir[i] or 0)
|
||||
Cape = float(l_cape[i] or 0)
|
||||
Vis = float(l_vis[i] or 10000)
|
||||
Code = int(l_code[i]) if l_code[i] is not None else None
|
||||
IsDay = int(l_day[i] if l_day[i] is not None else 1)
|
||||
|
||||
t_s = f"{int(round(T))}"
|
||||
p_s = "--" if Pr < 0.2 else f"{int(round(Pr))}"
|
||||
card = degrees_to_cardinal(Wdir)
|
||||
w_val = Gust if (Gust - Wspd) > 15 else Wspd
|
||||
w_txt = f"{card} {int(round(w_val))}"
|
||||
if (Gust - Wspd) > 15:
|
||||
g_txt = f"G{int(round(w_val))}"
|
||||
if len(card)+len(g_txt) <= 5: w_txt = f"{card}{g_txt}"
|
||||
elif len(card)+1+len(g_txt) <= 5: w_txt = f"{card} {g_txt}"
|
||||
else: w_txt = g_txt
|
||||
w_fmt = f"{w_txt:<5}"
|
||||
sky, sgx = get_icon_set(Pr, Sn, Code, IsDay, Cl, Vis, T, Rn, Gust, Cape)
|
||||
lines.append(f"{dt.strftime('%H'):<2} {t_s:>3} {Rh:>3} {p_s:>2} {w_fmt} {Cl:>3} {sky:<2} {sgx:<3}")
|
||||
count += 1
|
||||
except: continue
|
||||
if count > 0:
|
||||
day_label = f"{['Lun','Mar','Mer','Gio','Ven','Sab','Dom'][now.weekday()]} {now.day}"
|
||||
blocks.append(f"*{day_label} ({label})*\n```text\n" + "\n".join(lines) + "\n```")
|
||||
now = end_time
|
||||
|
||||
return f"🌤️ *METEO REPORT*\n📍 {location_name}\n🧠 Fonte: {model_name}\n\n" + "\n\n".join(blocks)
|
||||
cmd = ["python3", script_path] + args_list
|
||||
result = subprocess.run(cmd, capture_output=True, text=True, timeout=30)
|
||||
return result.stdout.strip() if result.returncode == 0 else f"⚠️ Errore Script:\n{result.stderr}"
|
||||
except Exception as e: return f"❌ Errore esecuzione: {e}"
|
||||
|
||||
# =============================================================================
|
||||
# SEZIONE 3: BOT HANDLERS & SCHEDULER
|
||||
# SEZIONE 2: GESTORI COMANDI (HANDLERS)
|
||||
# =============================================================================
|
||||
|
||||
# Decoratore Sicurezza Multi-Utente
|
||||
# Decoratore Sicurezza
|
||||
def restricted(func):
|
||||
@wraps(func)
|
||||
async def wrapped(update: Update, context: ContextTypes.DEFAULT_TYPE, *args, **kwargs):
|
||||
user_id = update.effective_user.id
|
||||
if user_id not in ALLOWED_IDS:
|
||||
logger.warning(f"⚠️ ACCESSO NEGATO: User {user_id}")
|
||||
return
|
||||
if user_id not in ALLOWED_IDS: return
|
||||
return await func(update, context, *args, **kwargs)
|
||||
return wrapped
|
||||
|
||||
@@ -345,53 +140,161 @@ async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
||||
keyboard = [
|
||||
[InlineKeyboardButton("🖥️ Core Server", callback_data="menu_core"), InlineKeyboardButton("🔍 Scan LAN", callback_data="menu_lan")],
|
||||
[InlineKeyboardButton("🛡️ Pi-hole", callback_data="menu_pihole"), InlineKeyboardButton("🌐 Rete", callback_data="menu_net")],
|
||||
[InlineKeyboardButton("🌤️ Meteo Casa", callback_data="req_meteo_home"), InlineKeyboardButton("📜 Logs", callback_data="menu_logs")]
|
||||
[InlineKeyboardButton("🌤️ Meteo Casa", callback_data="req_meteo_home"), InlineKeyboardButton("📹 Camere", callback_data="menu_cams")],
|
||||
[InlineKeyboardButton("📜 Logs", callback_data="menu_logs")]
|
||||
]
|
||||
text = "🎛 **Loogle Control Center v7.9**\nComandi disponibili:\n🔹 `/meteo <città>`\n🔹 Pulsanti sotto"
|
||||
text = "🎛 **Loogle Control Center v9.0**\n\n🔹 `/meteo <città>`\n🔹 `/meteo7 <città>` (7 Giorni)\n🔹 `/cam <nome>` (Snapshot)"
|
||||
|
||||
if update.message: await update.message.reply_text(text, reply_markup=InlineKeyboardMarkup(keyboard), parse_mode="Markdown")
|
||||
else: await update.callback_query.edit_message_text(text, reply_markup=InlineKeyboardMarkup(keyboard), parse_mode="Markdown")
|
||||
|
||||
@restricted
|
||||
async def meteo_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
||||
query = " ".join(context.args).strip()
|
||||
if not query or query.lower() == "casa":
|
||||
await update.message.reply_text("⏳ **Scarico Meteo Casa...**", parse_mode="Markdown")
|
||||
report = call_script_text(METEO_SCRIPT, ["--home"])
|
||||
else:
|
||||
await update.message.reply_text(f"🔄 Cerco '{query}'...", parse_mode="Markdown")
|
||||
report = call_script_text(METEO_SCRIPT, ["--query", query])
|
||||
|
||||
await update.message.reply_text(report, parse_mode="Markdown")
|
||||
|
||||
@restricted
|
||||
async def meteo7_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
||||
chat_id = update.effective_chat.id
|
||||
query = "casa"
|
||||
if context.args: query = " ".join(context.args)
|
||||
await update.message.reply_text(f"📡 Calcolo previsione 7gg per: {query}...", parse_mode="Markdown")
|
||||
subprocess.Popen(["python3", METEO7_SCRIPT, query, "--chat_id", str(chat_id)])
|
||||
|
||||
@restricted
|
||||
async def cam_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
||||
if not context.args:
|
||||
await update.message.reply_text("⚠️ Usa: `/meteo <città>` (es. `/meteo Rimini`)", parse_mode="Markdown")
|
||||
# Se non c'è argomento, mostra il menu camere
|
||||
keyboard = [
|
||||
[InlineKeyboardButton("📷 Sala", callback_data="req_cam_sala"), InlineKeyboardButton("📷 Ingresso", callback_data="req_cam_ingresso")],
|
||||
[InlineKeyboardButton("📷 Taverna", callback_data="req_cam_taverna"), InlineKeyboardButton("📷 Retro", callback_data="req_cam_retro")],
|
||||
[InlineKeyboardButton("📷 Matrim.", callback_data="req_cam_matrimoniale"), InlineKeyboardButton("📷 Luca", callback_data="req_cam_luca")],
|
||||
[InlineKeyboardButton("⬅️ Indietro", callback_data="main_menu")]
|
||||
]
|
||||
await update.message.reply_text("📹 **Scegli una telecamera:**", reply_markup=InlineKeyboardMarkup(keyboard), parse_mode="Markdown")
|
||||
return
|
||||
|
||||
city = " ".join(context.args)
|
||||
await update.message.reply_text(f"🔄 Cerco '{city}'...", parse_mode="Markdown")
|
||||
cam_name = context.args[0]
|
||||
await update.message.reply_chat_action(action="upload_photo")
|
||||
|
||||
coords = get_coordinates(city)
|
||||
if coords:
|
||||
lat, lon, name, cc = coords
|
||||
report = generate_weather_report(lat, lon, name, cc)
|
||||
await update.message.reply_text(report, parse_mode="Markdown")
|
||||
else:
|
||||
await update.message.reply_text(f"❌ Città '{city}' non trovata.", parse_mode="Markdown")
|
||||
try:
|
||||
# Timeout 15s per RTSP
|
||||
result = subprocess.run(["python3", CAM_SCRIPT, cam_name], capture_output=True, text=True, timeout=15)
|
||||
output = result.stdout.strip()
|
||||
|
||||
if output.startswith("OK:"):
|
||||
img_path = output.split(":", 1)[1]
|
||||
await update.message.reply_photo(photo=open(img_path, 'rb'), caption=f"📷 **{cam_name.capitalize()}**")
|
||||
elif output.startswith("ERR:"):
|
||||
await update.message.reply_text(output.split(":", 1)[1])
|
||||
else:
|
||||
await update.message.reply_text(f"❌ Risposta imprevista dallo script: {output}")
|
||||
|
||||
except Exception as e:
|
||||
await update.message.reply_text(f"❌ Errore critico: {e}")
|
||||
|
||||
async def scheduled_morning_report(context: ContextTypes.DEFAULT_TYPE) -> None:
|
||||
logger.info("⏰ Invio report automatico meteo...")
|
||||
# Forza "SM" per casa -> Arome/IconD2 in base alla posizione
|
||||
report = generate_weather_report(HOME_LAT, HOME_LON, HOME_NAME, "SM")
|
||||
# Meteo automatico alle 8:00
|
||||
report = call_script_text(METEO_SCRIPT, ["--home"])
|
||||
for uid in ALLOWED_IDS:
|
||||
try: await context.bot.send_message(chat_id=uid, text=report, parse_mode="Markdown")
|
||||
except: pass
|
||||
|
||||
@restricted
|
||||
async def clip_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
||||
if not context.args:
|
||||
await update.message.reply_text("⚠️ Usa: `/clip <nome_camera>` (es. /clip sala)", parse_mode="Markdown")
|
||||
return
|
||||
|
||||
cam_name = context.args[0]
|
||||
await update.message.reply_chat_action(action="upload_video") # Icona "sta inviando video..."
|
||||
await update.message.reply_text(f"🎥 **Registro 10s da {cam_name}...**", parse_mode="Markdown")
|
||||
|
||||
try:
|
||||
# Lancia lo script con flag --video
|
||||
result = subprocess.run(["python3", CAM_SCRIPT, cam_name, "--video"], capture_output=True, text=True, timeout=20)
|
||||
output = result.stdout.strip()
|
||||
|
||||
if output.startswith("OK:"):
|
||||
vid_path = output.split(":", 1)[1]
|
||||
await update.message.reply_video(video=open(vid_path, 'rb'), caption=f"🎥 **Clip: {cam_name.capitalize()}**")
|
||||
elif output.startswith("ERR:"):
|
||||
await update.message.reply_text(output.split(":", 1)[1])
|
||||
|
||||
except Exception as e:
|
||||
await update.message.reply_text(f"❌ Errore critico: {e}")
|
||||
|
||||
@restricted
|
||||
async def button_handler(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
||||
query = update.callback_query
|
||||
await query.answer()
|
||||
await query.answer() # Risposta immediata per togliere il loading dal pulsante
|
||||
data = query.data
|
||||
|
||||
if data == "main_menu": await start(update, context)
|
||||
|
||||
# --- NAVIGAZIONE MENU ---
|
||||
if data == "main_menu":
|
||||
await start(update, context)
|
||||
|
||||
# --- SEZIONE METEO ---
|
||||
elif data == "req_meteo_home":
|
||||
await query.edit_message_text("⏳ **Scaricamento Meteo Casa...**", parse_mode="Markdown")
|
||||
# Forza "SM" per casa
|
||||
report = generate_weather_report(HOME_LAT, HOME_LON, HOME_NAME, "SM")
|
||||
report = call_script_text(METEO_SCRIPT, ["--home"])
|
||||
keyboard = [[InlineKeyboardButton("⬅️ Indietro", callback_data="main_menu")]]
|
||||
await query.edit_message_text(report, reply_markup=InlineKeyboardMarkup(keyboard), parse_mode="Markdown")
|
||||
|
||||
# --- SEZIONE CAMERE ---
|
||||
elif data == "menu_cams":
|
||||
keyboard = [
|
||||
[InlineKeyboardButton("📷 Sala", callback_data="req_cam_sala"), InlineKeyboardButton("📷 Ingresso", callback_data="req_cam_ingresso")],
|
||||
[InlineKeyboardButton("📷 Taverna", callback_data="req_cam_taverna"), InlineKeyboardButton("📷 Retro", callback_data="req_cam_retro")],
|
||||
[InlineKeyboardButton("📷 Matrim.", callback_data="req_cam_matrimoniale"), InlineKeyboardButton("📷 Luca", callback_data="req_cam_luca")],
|
||||
[InlineKeyboardButton("⬅️ Indietro", callback_data="main_menu")]
|
||||
]
|
||||
await query.edit_message_text("📹 **Centrale Video**\nSeleziona una telecamera:", reply_markup=InlineKeyboardMarkup(keyboard), parse_mode="Markdown")
|
||||
|
||||
elif data.startswith("req_cam_"):
|
||||
cam_name = data.replace("req_cam_", "")
|
||||
# Non editiamo il messaggio, inviamo una nuova foto sotto
|
||||
try:
|
||||
result = subprocess.run(["python3", CAM_SCRIPT, cam_name], capture_output=True, text=True, timeout=15)
|
||||
output = result.stdout.strip()
|
||||
|
||||
if output.startswith("OK:"):
|
||||
img_path = output.split(":", 1)[1]
|
||||
await context.bot.send_photo(chat_id=update.effective_chat.id, photo=open(img_path, 'rb'), caption=f"📷 **{cam_name.capitalize()}**")
|
||||
elif output.startswith("ERR:"):
|
||||
await context.bot.send_message(chat_id=update.effective_chat.id, text=output.split(":", 1)[1])
|
||||
except Exception as e:
|
||||
await context.bot.send_message(chat_id=update.effective_chat.id, text=f"❌ Errore richiesta cam: {e}")
|
||||
|
||||
elif data.startswith("req_vid_"):
|
||||
cam_name = data.replace("req_vid_", "")
|
||||
await query.answer("🎥 Registrazione in corso (10s)...")
|
||||
# Inviamo un messaggio di attesa perché ci mette un po'
|
||||
msg = await context.bot.send_message(chat_id=update.effective_chat.id, text=f"⏳ Registro clip: {cam_name}...")
|
||||
|
||||
try:
|
||||
result = subprocess.run(["python3", CAM_SCRIPT, cam_name, "--video"], capture_output=True, text=True, timeout=20)
|
||||
output = result.stdout.strip()
|
||||
|
||||
# Cancelliamo il messaggio di attesa
|
||||
await context.bot.delete_message(chat_id=update.effective_chat.id, message_id=msg.message_id)
|
||||
|
||||
if output.startswith("OK:"):
|
||||
vid_path = output.split(":", 1)[1]
|
||||
await context.bot.send_video(chat_id=update.effective_chat.id, video=open(vid_path, 'rb'), caption=f"🎥 **Clip: {cam_name.capitalize()}**")
|
||||
elif output.startswith("ERR:"):
|
||||
await context.bot.send_message(chat_id=update.effective_chat.id, text=output.split(":", 1)[1])
|
||||
except Exception as e:
|
||||
await context.bot.send_message(chat_id=update.effective_chat.id, text=f"❌ Errore: {e}")
|
||||
|
||||
# --- SEZIONE SISTEMA CORE ---
|
||||
elif data == "menu_core":
|
||||
keyboard = []
|
||||
for i, dev in enumerate(CORE_DEVICES): keyboard.append([InlineKeyboardButton(dev['name'], callback_data=f"stat_{i}")])
|
||||
@@ -410,6 +313,7 @@ async def button_handler(update: Update, context: ContextTypes.DEFAULT_TYPE) ->
|
||||
await query.edit_message_text(f"⏳ Controllo {dev['name']}...", parse_mode="Markdown")
|
||||
await query.edit_message_text(f"🔹 **{dev['name']}**\n\n{get_device_stats(dev)}", reply_markup=InlineKeyboardMarkup([[InlineKeyboardButton("⬅️ Indietro", callback_data="menu_core")]]), parse_mode="Markdown")
|
||||
|
||||
# --- SEZIONE LAN ---
|
||||
elif data == "menu_lan":
|
||||
await query.edit_message_text("⏳ **Scansione LAN...**", parse_mode="Markdown")
|
||||
report = "🔍 **DIAGNOSTICA LAN**\n\n"
|
||||
@@ -433,6 +337,7 @@ async def button_handler(update: Update, context: ContextTypes.DEFAULT_TYPE) ->
|
||||
res = run_cmd("reboot", dev['ip'], "admin")
|
||||
await query.edit_message_text(f"⚡ Inviato a {dev['name']}...\n\n`{res}`", reply_markup=InlineKeyboardMarkup([[InlineKeyboardButton("⬅️ Indietro", callback_data="menu_reboot")]]), parse_mode="Markdown")
|
||||
|
||||
# --- SEZIONE PI-HOLE ---
|
||||
elif data == "menu_pihole":
|
||||
status_raw = run_cmd("sudo pihole status", MASTER_IP, SSH_USER)
|
||||
icon = "✅" if "Enabled" in status_raw or "enabled" in status_raw else "🔴"
|
||||
@@ -446,16 +351,18 @@ async def button_handler(update: Update, context: ContextTypes.DEFAULT_TYPE) ->
|
||||
elif "restart" in data: run_cmd("sudo systemctl restart pihole-FTL", MASTER_IP, SSH_USER)
|
||||
await query.edit_message_text("✅ Comando inviato.", reply_markup=InlineKeyboardMarkup([[InlineKeyboardButton("⬅️ Indietro", callback_data="menu_pihole")]]), parse_mode="Markdown")
|
||||
|
||||
# --- SEZIONE RETE ---
|
||||
elif data == "menu_net":
|
||||
ip = run_cmd("curl -s ifconfig.me")
|
||||
keyboard = [[InlineKeyboardButton("🚀 Speedtest", callback_data="net_speedtest")], [InlineKeyboardButton("⬅️ Indietro", callback_data="main_menu")]]
|
||||
await query.edit_message_text(f"🌐 **Rete**\n🌍 IP: `{ip}`", reply_markup=InlineKeyboardMarkup(keyboard), parse_mode="Markdown")
|
||||
|
||||
elif data == "net_speedtest":
|
||||
await query.edit_message_text("🚀 **Speedtest...**", parse_mode="Markdown")
|
||||
await query.edit_message_text("🚀 **Speedtest... (attendi 40s)**", parse_mode="Markdown")
|
||||
res = run_speedtest()
|
||||
await query.edit_message_text(f"🚀 **Risultato:**\n\n`{res}`", reply_markup=InlineKeyboardMarkup([[InlineKeyboardButton("⬅️ Indietro", callback_data="menu_net")]]), parse_mode="Markdown")
|
||||
|
||||
# --- SEZIONE LOGS ---
|
||||
elif data == "menu_logs":
|
||||
keyboard = [[InlineKeyboardButton("🐶 Watchdog", callback_data="log_wd"), InlineKeyboardButton("💾 Backup", callback_data="log_bk")], [InlineKeyboardButton("🔄 NPM Sync", callback_data="log_npm"), InlineKeyboardButton("⬅️ Indietro", callback_data="main_menu")]]
|
||||
await query.edit_message_text("📜 **Logs**", reply_markup=InlineKeyboardMarkup(keyboard), parse_mode="Markdown")
|
||||
@@ -466,19 +373,24 @@ async def button_handler(update: Update, context: ContextTypes.DEFAULT_TYPE) ->
|
||||
await query.edit_message_text(f"📜 **Log:**\n\n`{log_c}`", reply_markup=InlineKeyboardMarkup([[InlineKeyboardButton("⬅️ Indietro", callback_data="menu_logs")]]), parse_mode="Markdown")
|
||||
|
||||
def main():
|
||||
logger.info("Avvio Loogle Bot v7.9 (Global GFS Fix)...")
|
||||
logger.info("Avvio Loogle Bot v9.0 (Modular)...")
|
||||
application = Application.builder().token(BOT_TOKEN).build()
|
||||
|
||||
# Handlers
|
||||
|
||||
# Registrazione Comandi
|
||||
application.add_handler(CommandHandler("start", start))
|
||||
application.add_handler(CommandHandler("meteo", meteo_command))
|
||||
application.add_handler(CommandHandler("meteo7", meteo7_command))
|
||||
application.add_handler(CommandHandler("cam", cam_command))
|
||||
application.add_handler(CommandHandler("clip", clip_command))
|
||||
|
||||
# Registrazione Callback Menu
|
||||
application.add_handler(CallbackQueryHandler(button_handler))
|
||||
|
||||
# SCHEDULER
|
||||
# Scheduler
|
||||
job_queue = application.job_queue
|
||||
job_queue.run_daily(scheduled_morning_report, time=datetime.time(hour=8, minute=0, tzinfo=TZINFO), days=(0, 1, 2, 3, 4, 5, 6))
|
||||
|
||||
application.run_polling()
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
main()
|
||||
Reference in New Issue
Block a user