mirror of
https://github.com/BOSWatch/BW3-Core.git
synced 2026-01-20 15:40:24 +01:00
update telegram plugin
This commit is contained in:
parent
fd9307dd03
commit
456efc1116
Binary file not shown.
|
|
@ -35,45 +35,69 @@ class BoswatchPlugin(PluginBase):
|
|||
|
||||
def onLoad(self):
|
||||
"""!Called by import of the plugin"""
|
||||
self.bot = telegram.Bot(token=self.config.get("botToken", default=""))
|
||||
self.bot = telegram.Bot(token=self.config.get("botToken"))
|
||||
|
||||
def fms(self, bwPacket):
|
||||
"""!Called on FMS alarm
|
||||
|
||||
@param bwPacket: bwPacket instance
|
||||
Remove if not implemented"""
|
||||
msg = self.parseWildcards(self.config.get("message_fms", default="{FMS}"))
|
||||
_sendMessage(msg)
|
||||
|
||||
def pocsag(self, bwPacket):
|
||||
"""!Called on POCSAG alarm
|
||||
|
||||
@param bwPacket: bwPacket instance"""
|
||||
msg = self.parseWildcards(self.config.get("message"))
|
||||
@param bwPacket: bwPacket instance
|
||||
Remove if not implemented"""
|
||||
msg = self.parseWildcards(self.config.get("message_pocsag", default="{RIC}({SRIC})\n{MSG}"))
|
||||
self._sendMessage(msg)
|
||||
|
||||
if bwPacket.get("lat") is not None and bwPacket.get("lon") is not None:
|
||||
logging.debug("Found coordinates in packet")
|
||||
(lat, lon) = (bwPacket.get("lat"), bwPacket.get("lon"))
|
||||
|
||||
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)
|
||||
|
||||
# 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)
|
||||
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))
|
||||
self._sendMessage(lat, lon)
|
||||
|
||||
def zvei(self, bwPacket):
|
||||
"""!Called on ZVEI alarm
|
||||
@param bwPacket: bwPacket instance"""
|
||||
msg = self.parseWildcards(self.config.get("message"))
|
||||
|
||||
@param bwPacket: bwPacket instance
|
||||
Remove if not implemented"""
|
||||
msg = self.parseWildcards(self.config.get("message_zvei", default="{TONE}"))
|
||||
_sendMessage(msg)
|
||||
|
||||
def msg(self, bwPacket):
|
||||
"""!Called on MSG packet
|
||||
|
||||
@param bwPacket: bwPacket instance
|
||||
Remove if not implemented"""
|
||||
msg = self.parseWildcards(self.config.get("message_msg"))
|
||||
_sendMessage(msg)
|
||||
|
||||
def _sendMessage(self, 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)
|
||||
self.bot.send_message(chat_id=chatId, text=message)
|
||||
|
||||
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))
|
||||
|
||||
def _sendLocation(self, lat, lon):
|
||||
for chatId in self.config.get("chatIds", default=[]):
|
||||
try:
|
||||
# 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)
|
||||
|
||||
except Unauthorized:
|
||||
logging.exception("Error while sending Telegram Message, please Check your api-key")
|
||||
except (TimedOut, NetworkError):
|
||||
|
|
|
|||
Loading…
Reference in a new issue