2020-02-18 22:12:53 +01:00
|
|
|
|
#!/usr/bin/python
|
|
|
|
|
|
# -*- coding: utf-8 -*-
|
2023-09-19 16:13:23 +02:00
|
|
|
|
r"""!
|
2020-02-18 22:12:53 +01:00
|
|
|
|
____ ____ ______ __ __ __ _____
|
|
|
|
|
|
/ __ )/ __ \/ ___/ | / /___ _/ /______/ /_ |__ /
|
|
|
|
|
|
/ __ / / / /\__ \| | /| / / __ `/ __/ ___/ __ \ /_ <
|
|
|
|
|
|
/ /_/ / /_/ /___/ /| |/ |/ / /_/ / /_/ /__/ / / / ___/ /
|
|
|
|
|
|
/_____/\____//____/ |__/|__/\__,_/\__/\___/_/ /_/ /____/
|
|
|
|
|
|
German BOS Information Script
|
|
|
|
|
|
by Bastian Schroll
|
|
|
|
|
|
|
2020-02-22 19:08:53 +01:00
|
|
|
|
@file: telegram.py
|
2025-07-11 22:24:39 +02:00
|
|
|
|
@date: 12.07.2025
|
|
|
|
|
|
@author: Claus Schichl nach der Idee von Jan Speller
|
|
|
|
|
|
@description: Telegram-Plugin mit Retry-Logik ohne externe Telegram-Abhängigkeiten
|
2020-02-18 22:12:53 +01:00
|
|
|
|
"""
|
2025-07-11 22:24:39 +02:00
|
|
|
|
|
2020-02-18 22:12:53 +01:00
|
|
|
|
import logging
|
2025-07-11 22:24:39 +02:00
|
|
|
|
import time
|
|
|
|
|
|
import threading
|
|
|
|
|
|
import queue
|
|
|
|
|
|
import requests
|
2020-02-18 22:12:53 +01:00
|
|
|
|
from plugin.pluginBase import PluginBase
|
|
|
|
|
|
|
2025-07-11 22:24:39 +02:00
|
|
|
|
# Setup Logging
|
|
|
|
|
|
logging.basicConfig(
|
|
|
|
|
|
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
|
|
|
|
|
level=logging.INFO
|
|
|
|
|
|
)
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ===========================
|
|
|
|
|
|
# TelegramSender-Klasse
|
|
|
|
|
|
# ===========================
|
|
|
|
|
|
|
|
|
|
|
|
class TelegramSender:
|
|
|
|
|
|
def __init__(self, bot_token, chat_ids, max_retries=None, initial_delay=None, max_delay=None):
|
|
|
|
|
|
self.bot_token = bot_token
|
|
|
|
|
|
self.chat_ids = chat_ids
|
|
|
|
|
|
self.max_retries = max_retries if max_retries is not None else 5
|
|
|
|
|
|
self.initial_delay = initial_delay if initial_delay is not None else 2
|
|
|
|
|
|
self.max_delay = max_delay if max_delay is not None else 300
|
|
|
|
|
|
self.msg_queue = queue.Queue()
|
|
|
|
|
|
self._worker = threading.Thread(target=self._worker_loop, daemon=True)
|
|
|
|
|
|
self._worker.start()
|
|
|
|
|
|
|
|
|
|
|
|
def send_message(self, text):
|
|
|
|
|
|
for chat_id in self.chat_ids:
|
|
|
|
|
|
self.msg_queue.put(("text", chat_id, text, 0)) # retry_count = 0
|
|
|
|
|
|
|
|
|
|
|
|
def send_location(self, latitude, longitude):
|
|
|
|
|
|
for chat_id in self.chat_ids:
|
|
|
|
|
|
self.msg_queue.put(("location", chat_id, {"latitude": latitude, "longitude": longitude}, 0))
|
|
|
|
|
|
|
|
|
|
|
|
def _worker_loop(self):
|
|
|
|
|
|
delay = self.initial_delay
|
|
|
|
|
|
|
|
|
|
|
|
while True:
|
|
|
|
|
|
try:
|
|
|
|
|
|
msg_type, chat_id, content, retry_count = self.msg_queue.get()
|
|
|
|
|
|
|
|
|
|
|
|
success, permanent_failure, custom_delay = self._send_to_telegram(msg_type, chat_id, content)
|
|
|
|
|
|
|
|
|
|
|
|
if success:
|
|
|
|
|
|
delay = self.initial_delay
|
2020-02-18 22:12:53 +01:00
|
|
|
|
|
2025-07-11 22:24:39 +02:00
|
|
|
|
elif permanent_failure:
|
|
|
|
|
|
logger.error("Permanenter Fehler – Nachricht wird verworfen.")
|
2020-02-18 22:12:53 +01:00
|
|
|
|
|
2025-07-11 22:24:39 +02:00
|
|
|
|
elif retry_count >= self.max_retries:
|
|
|
|
|
|
logger.error("Maximale Wiederholungsanzahl erreicht – Nachricht wird verworfen.")
|
2020-02-18 22:12:53 +01:00
|
|
|
|
|
2025-07-11 22:24:39 +02:00
|
|
|
|
else:
|
|
|
|
|
|
logger.warning(f"Erneutes Einreihen der Nachricht (Versuch {retry_count + 1}).")
|
|
|
|
|
|
self.msg_queue.put((msg_type, chat_id, content, retry_count + 1))
|
2020-07-09 15:02:22 +02:00
|
|
|
|
|
2025-07-11 22:24:39 +02:00
|
|
|
|
# Nutze den von Telegram gelieferten Wert (retry_after), falls vorhanden
|
|
|
|
|
|
wait_time = custom_delay if custom_delay is not None else delay
|
|
|
|
|
|
time.sleep(wait_time)
|
|
|
|
|
|
|
|
|
|
|
|
# Erhöhe Delay für den nächsten Versuch (exponentielles Backoff)
|
|
|
|
|
|
delay = min(delay * 2, self.max_delay)
|
|
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
logger.exception(f"Fehler im Telegram-Worker: {e}")
|
|
|
|
|
|
time.sleep(5)
|
|
|
|
|
|
|
|
|
|
|
|
def _send_to_telegram(self, msg_type, chat_id, content):
|
|
|
|
|
|
if msg_type == "text":
|
|
|
|
|
|
url = f"https://api.telegram.org/bot{self.bot_token}/sendMessage"
|
|
|
|
|
|
payload = {
|
|
|
|
|
|
'chat_id': chat_id,
|
|
|
|
|
|
'text': content
|
|
|
|
|
|
}
|
|
|
|
|
|
elif msg_type == "location":
|
|
|
|
|
|
url = f"https://api.telegram.org/bot{self.bot_token}/sendLocation"
|
|
|
|
|
|
payload = {
|
|
|
|
|
|
'chat_id': chat_id,
|
|
|
|
|
|
**content
|
|
|
|
|
|
}
|
|
|
|
|
|
else:
|
|
|
|
|
|
logger.error("Unbekannter Nachrichtentyp.")
|
|
|
|
|
|
return False, True, None # Unbekannter Typ = permanent falsch
|
2020-07-09 15:02:22 +02:00
|
|
|
|
|
|
|
|
|
|
try:
|
2025-07-11 22:24:39 +02:00
|
|
|
|
custom_delay = None # Standardwert für Rückgabe, außer bei 429
|
2020-07-09 15:02:22 +02:00
|
|
|
|
|
2025-07-11 22:24:39 +02:00
|
|
|
|
response = requests.post(url, data=payload, timeout=10)
|
2020-07-09 15:02:22 +02:00
|
|
|
|
|
2025-07-11 22:24:39 +02:00
|
|
|
|
if response.status_code == 429:
|
|
|
|
|
|
custom_delay = response.json().get("parameters", {}).get("retry_after", 5)
|
|
|
|
|
|
logger.warning(f"Rate Limit erreicht – warte {custom_delay} Sekunden.")
|
|
|
|
|
|
return False, False, custom_delay # Telegram gibt genaue Wartezeit vor
|
|
|
|
|
|
|
|
|
|
|
|
if response.status_code == 400:
|
|
|
|
|
|
logger.error("Ungültige Parameter – Nachricht wird nicht erneut gesendet.")
|
|
|
|
|
|
return False, True, custom_delay # Permanent fehlerhaft
|
|
|
|
|
|
|
|
|
|
|
|
if response.status_code == 401:
|
|
|
|
|
|
logger.critical("Ungültiger Bot-Token – bitte prüfen!")
|
|
|
|
|
|
return False, True, custom_delay # Permanent fehlerhaft
|
|
|
|
|
|
|
|
|
|
|
|
response.raise_for_status()
|
|
|
|
|
|
logger.info(f"Erfolgreich gesendet an Chat-ID {chat_id}")
|
|
|
|
|
|
return True, False, custom_delay
|
|
|
|
|
|
|
|
|
|
|
|
except requests.RequestException as e:
|
|
|
|
|
|
logger.warning(f"Fehler beim Senden an Telegram (Chat-ID {chat_id}): {e}")
|
|
|
|
|
|
return False, False, custom_delay
|
2020-07-09 15:02:22 +02:00
|
|
|
|
|
2020-02-18 22:12:53 +01:00
|
|
|
|
|
2025-07-11 22:24:39 +02:00
|
|
|
|
# ===========================
|
|
|
|
|
|
# BoswatchPlugin-Klasse
|
|
|
|
|
|
# ===========================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class BoswatchPlugin(PluginBase):
|
2020-02-18 22:12:53 +01:00
|
|
|
|
def __init__(self, config):
|
2023-09-19 16:13:23 +02:00
|
|
|
|
r"""!Do not change anything here!"""
|
2020-02-18 22:12:53 +01:00
|
|
|
|
super().__init__(__name__, config) # you can access the config class on 'self.config'
|
|
|
|
|
|
|
|
|
|
|
|
def onLoad(self):
|
2023-09-19 16:13:23 +02:00
|
|
|
|
r"""!Called by import of the plugin"""
|
2025-07-11 22:24:39 +02:00
|
|
|
|
bot_token = self.config.get("botToken")
|
|
|
|
|
|
chat_ids = self.config.get("chatIds", default=[])
|
|
|
|
|
|
|
|
|
|
|
|
if not bot_token or not chat_ids:
|
|
|
|
|
|
logger.error("botToken oder chatIds fehlen in der Konfiguration!")
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
# Konfigurierbare Parameter mit Fallback-Defaults
|
|
|
|
|
|
max_retries = self.config.get("max_retries")
|
|
|
|
|
|
initial_delay = self.config.get("initial_delay")
|
|
|
|
|
|
max_delay = self.config.get("max_delay")
|
|
|
|
|
|
|
|
|
|
|
|
self.sender = TelegramSender(
|
|
|
|
|
|
bot_token=bot_token,
|
|
|
|
|
|
chat_ids=chat_ids,
|
|
|
|
|
|
max_retries=max_retries,
|
|
|
|
|
|
initial_delay=initial_delay,
|
|
|
|
|
|
max_delay=max_delay
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
startup_message = self.config.get("startup_message")
|
|
|
|
|
|
if startup_message and startup_message.strip():
|
|
|
|
|
|
self.sender.send_message(startup_message)
|
|
|
|
|
|
|
|
|
|
|
|
def setup(self):
|
|
|
|
|
|
r"""!Called before alarm
|
|
|
|
|
|
Remove if not implemented"""
|
|
|
|
|
|
pass
|
2020-02-18 22:12:53 +01:00
|
|
|
|
|
2020-05-01 23:52:19 +02:00
|
|
|
|
def fms(self, bwPacket):
|
2023-09-19 16:13:23 +02:00
|
|
|
|
r"""!Called on FMS alarm
|
2020-05-06 07:41:52 +02:00
|
|
|
|
@param bwPacket: bwPacket instance"""
|
2020-05-01 23:52:19 +02:00
|
|
|
|
msg = self.parseWildcards(self.config.get("message_fms", default="{FMS}"))
|
2025-07-11 22:24:39 +02:00
|
|
|
|
self.sender.send_message(msg)
|
2020-02-18 22:12:53 +01:00
|
|
|
|
|
|
|
|
|
|
def pocsag(self, bwPacket):
|
2023-09-19 16:13:23 +02:00
|
|
|
|
r"""!Called on POCSAG alarm
|
2020-02-18 22:12:53 +01:00
|
|
|
|
@param bwPacket: bwPacket instance"""
|
2020-05-01 23:52:19 +02:00
|
|
|
|
msg = self.parseWildcards(self.config.get("message_pocsag", default="{RIC}({SRIC})\n{MSG}"))
|
2025-07-11 22:24:39 +02:00
|
|
|
|
self.sender.send_message(msg)
|
2020-05-01 23:52:19 +02:00
|
|
|
|
|
2020-02-22 22:53:03 +01:00
|
|
|
|
if bwPacket.get("lat") is not None and bwPacket.get("lon") is not None:
|
2025-07-11 22:24:39 +02:00
|
|
|
|
lat, lon = bwPacket.get("lat"), bwPacket.get("lon")
|
|
|
|
|
|
logger.debug("Koordinaten gefunden – sende Standort.")
|
|
|
|
|
|
self.sender.send_location(lat, lon)
|
2020-05-01 23:52:19 +02:00
|
|
|
|
|
|
|
|
|
|
def zvei(self, bwPacket):
|
2023-09-19 16:13:23 +02:00
|
|
|
|
r"""!Called on ZVEI alarm
|
2020-05-06 07:42:20 +02:00
|
|
|
|
@param bwPacket: bwPacket instance"""
|
2020-05-01 23:52:19 +02:00
|
|
|
|
msg = self.parseWildcards(self.config.get("message_zvei", default="{TONE}"))
|
2025-07-11 22:24:39 +02:00
|
|
|
|
self.sender.send_message(msg)
|
2020-05-01 23:52:19 +02:00
|
|
|
|
|
|
|
|
|
|
def msg(self, bwPacket):
|
2023-09-19 16:13:23 +02:00
|
|
|
|
r"""!Called on MSG packet
|
2020-05-06 07:42:11 +02:00
|
|
|
|
@param bwPacket: bwPacket instance"""
|
2020-05-01 23:52:19 +02:00
|
|
|
|
msg = self.parseWildcards(self.config.get("message_msg"))
|
2025-07-11 22:24:39 +02:00
|
|
|
|
self.sender.send_message(msg)
|
2020-05-01 23:52:19 +02:00
|
|
|
|
|
2025-07-11 22:24:39 +02:00
|
|
|
|
def teardown(self):
|
|
|
|
|
|
r"""!Called after alarm
|
|
|
|
|
|
Remove if not implemented"""
|
|
|
|
|
|
pass
|
2020-05-01 13:58:36 +02:00
|
|
|
|
|
2025-07-11 22:24:39 +02:00
|
|
|
|
def onUnload(self):
|
|
|
|
|
|
r"""!Called by destruction of the plugin
|
|
|
|
|
|
Remove if not implemented"""
|
|
|
|
|
|
pass
|