BW3-Core/plugin/telegram.py

67 lines
2.7 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"""
self.bot = telegram.Bot(token=self.config.get("botToken", default=""))
def pocsag(self, bwPacket):
"""!Called on POCSAG alarm
@param bwPacket: bwPacket instance"""
2020-02-22 19:08:53 +01:00
msg = bwPacket.get("ric") + " (" + bwPacket.get("subric") + ")\n" + bwPacket.get("message")
2020-02-22 22:53:03 +01:00
if bwPacket.get("lat") is not None and bwPacket.get("lon") is not None:
logging.info("Found coordinates in packet")
(lat, lon) = (bwPacket.get("lat"), bwPacket.get("lon"))
2020-02-22 19:11:21 +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-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:
logging.error("Error while sending Telegram Message, please Check your api-key")
except (TimedOut, NetworkError):
logging.error("Error while sending Telegram Message, please Check your connectivity")
except (BadRequest, TelegramError):
logging.error("Error while sending Telegram Message")
except Exception as e:
logging.error("Unknown Error while sending Telegram Message: " + str(type(e).__name__) + ": " + str(e))