2020-02-18 22:12:53 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
"""!
|
|
|
|
|
____ ____ ______ __ __ __ _____
|
|
|
|
|
/ __ )/ __ \/ ___/ | / /___ _/ /______/ /_ |__ /
|
|
|
|
|
/ __ / / / /\__ \| | /| / / __ `/ __/ ___/ __ \ /_ <
|
|
|
|
|
/ /_/ / /_/ /___/ /| |/ |/ / /_/ / /_/ /__/ / / / ___/ /
|
|
|
|
|
/_____/\____//____/ |__/|__/\__,_/\__/\___/_/ /_/ /____/
|
|
|
|
|
German BOS Information Script
|
|
|
|
|
by Bastian Schroll
|
|
|
|
|
|
2020-02-22 19:08:53 +01:00
|
|
|
@file: telegram.py
|
|
|
|
|
@date: 20.02.2020
|
|
|
|
|
@author: Jan Speller
|
|
|
|
|
@description: Telegram Plugin
|
2020-02-18 22:12:53 +01:00
|
|
|
"""
|
|
|
|
|
import logging
|
|
|
|
|
from plugin.pluginBase import PluginBase
|
|
|
|
|
|
|
|
|
|
# ###################### #
|
|
|
|
|
# Custom plugin includes #
|
2020-02-22 19:11:21 +01:00
|
|
|
from telegram.error import (TelegramError, Unauthorized, BadRequest, TimedOut, NetworkError)
|
2020-02-22 19:08:53 +01:00
|
|
|
import telegram
|
2020-02-18 22:12:53 +01:00
|
|
|
# ###################### #
|
|
|
|
|
|
|
|
|
|
logging.debug("- %s loaded", __name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class BoswatchPlugin(PluginBase):
|
|
|
|
|
"""!Description of the Plugin"""
|
|
|
|
|
|
|
|
|
|
def __init__(self, config):
|
|
|
|
|
"""!Do not change anything here!"""
|
|
|
|
|
super().__init__(__name__, config) # you can access the config class on 'self.config'
|
|
|
|
|
|
|
|
|
|
def onLoad(self):
|
|
|
|
|
"""!Called by import of the plugin"""
|
|
|
|
|
self.bot = telegram.Bot(token=self.config.get("botToken", default=""))
|
|
|
|
|
|
|
|
|
|
def pocsag(self, bwPacket):
|
|
|
|
|
"""!Called on POCSAG alarm
|
|
|
|
|
|
|
|
|
|
@param bwPacket: bwPacket instance"""
|
2020-02-24 21:51:19 +01:00
|
|
|
msg = self.parseWildcards(self.config.get("message"))
|
2020-02-22 22:53:03 +01:00
|
|
|
if bwPacket.get("lat") is not None and bwPacket.get("lon") is not None:
|
2020-02-24 21:51:19 +01:00
|
|
|
logging.debug("Found coordinates in packet")
|
2020-02-22 22:53:03 +01:00
|
|
|
(lat, lon) = (bwPacket.get("lat"), bwPacket.get("lon"))
|
2020-02-24 23:52:20 +01:00
|
|
|
|
2020-02-22 19:08:53 +01:00
|
|
|
for chatId in self.config.get("chatIds", default=[]):
|
|
|
|
|
try:
|
|
|
|
|
# Send Message via Telegram
|
2020-02-22 22:53:03 +01:00
|
|
|
logging.info("Sending message to " + chatId)
|
2020-02-22 19:11:21 +01:00
|
|
|
self.bot.send_message(chat_id=chatId, text=msg)
|
2020-02-24 23:52:20 +01:00
|
|
|
|
2020-02-22 22:53:03 +01:00
|
|
|
# Send Location via Telegram if lat and lon are defined
|
|
|
|
|
if lat is not None and lon is not None:
|
|
|
|
|
logging.info("Sending location to " + chatId)
|
|
|
|
|
self.bot.sendLocation(chat_id=chatId, latitude=lat, longitude=lon)
|
2020-02-22 19:08:53 +01:00
|
|
|
except Unauthorized:
|
2020-02-24 22:15:28 +01:00
|
|
|
logging.exception("Error while sending Telegram Message, please Check your api-key")
|
2020-02-22 19:08:53 +01:00
|
|
|
except (TimedOut, NetworkError):
|
2020-02-24 22:15:28 +01:00
|
|
|
logging.exception("Error while sending Telegram Message, please Check your connectivity")
|
2020-02-22 19:08:53 +01:00
|
|
|
except (BadRequest, TelegramError):
|
2020-02-24 22:15:28 +01:00
|
|
|
logging.exception("Error while sending Telegram Message")
|
2020-02-22 19:08:53 +01:00
|
|
|
except Exception as e:
|
2020-02-24 22:15:28 +01:00
|
|
|
logging.exception("Unknown Error while sending Telegram Message: " + str(type(e).__name__) + ": " + str(e))
|
2020-05-01 13:58:36 +02:00
|
|
|
|
|
|
|
|
def zvei(self, bwPacket):
|
|
|
|
|
"""!Called on ZVEI alarm
|
|
|
|
|
@param bwPacket: bwPacket instance"""
|
|
|
|
|
msg = self.parseWildcards(self.config.get("message"))
|
|
|
|
|
for chatId in self.config.get("chatIds", default=[]):
|
|
|
|
|
try:
|
|
|
|
|
# Send Message via Telegram
|
|
|
|
|
logging.info("Sending message to " + chatId)
|
|
|
|
|
self.bot.send_message(chat_id=chatId, text=msg)
|
|
|
|
|
except Unauthorized:
|
|
|
|
|
logging.exception("Error while sending Telegram Message, please Check your api-key")
|
|
|
|
|
except (TimedOut, NetworkError):
|
|
|
|
|
logging.exception("Error while sending Telegram Message, please Check your connectivity")
|
|
|
|
|
except (BadRequest, TelegramError):
|
|
|
|
|
logging.exception("Error while sending Telegram Message")
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logging.exception("Unknown Error while sending Telegram Message: " + str(type(e).__name__) + ": " + str(e))
|