BW3-Core/plugin/telegram.py

105 lines
4.1 KiB
Python
Raw Normal View History

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"""
2020-05-01 23:52:19 +02:00
self.bot = telegram.Bot(token=self.config.get("botToken"))
def fms(self, bwPacket):
"""!Called on FMS alarm
@param bwPacket: bwPacket instance"""
2020-05-01 23:52:19 +02:00
msg = self.parseWildcards(self.config.get("message_fms", default="{FMS}"))
2020-05-01 23:56:29 +02:00
self._sendMessage(msg)
2020-02-18 22:12:53 +01:00
def pocsag(self, bwPacket):
"""!Called on POCSAG alarm
@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}"))
self._sendMessage(msg)
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-05-01 23:52:19 +02:00
self._sendMessage(lat, lon)
def zvei(self, bwPacket):
"""!Called on ZVEI alarm
@param bwPacket: bwPacket instance"""
2020-05-01 23:52:19 +02:00
msg = self.parseWildcards(self.config.get("message_zvei", default="{TONE}"))
2020-05-01 23:56:29 +02:00
self._sendMessage(msg)
2020-05-01 23:52:19 +02:00
def msg(self, bwPacket):
"""!Called on MSG packet
2020-02-24 23:52:20 +01:00
@param bwPacket: bwPacket instance"""
2020-05-01 23:52:19 +02:00
msg = self.parseWildcards(self.config.get("message_msg"))
2020-05-01 23:56:29 +02:00
self._sendMessage(msg)
2020-05-01 23:52:19 +02:00
def _sendMessage(self, message):
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-05-01 23:52:19 +02:00
self.bot.send_message(chat_id=chatId, text=message)
2020-02-24 23:52:20 +01:00
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
2020-05-01 23:52:19 +02:00
def _sendLocation(self, lat, lon):
2020-05-01 13:58:36 +02:00
for chatId in self.config.get("chatIds", default=[]):
try:
2020-05-01 23:52:19 +02:00
# Send Location via Telegram
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-05-01 13:58:36 +02:00
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))