BOSWatch/plugins/Telegram/Telegram.py

152 lines
5.6 KiB
Python
Raw Normal View History

2016-09-30 20:58:35 +02:00
#!/usr/bin/python
# -*- coding: UTF-8 -*-
"""
Plugin to send FMS-, ZVEI- and POCSAG-messages via Telegram
@author: Peter Laemmle
@requires: Telegram BOT token, Telegram chat ID, library python-telegram-bot and optional requests and json
2016-09-30 20:58:35 +02:00
"""
#
# Imports
#
import logging # Global logger
import telegram
from telegram.error import (TelegramError, Unauthorized, BadRequest, NetworkError)
from includes import globalVars # Global variables
if globalVars.config.get("Telegram","RICforLocationAPIKey"):
import requests, json
2016-09-30 20:58:35 +02:00
# Helper function, uncomment to use
2017-04-24 20:08:37 +02:00
from includes.helper import wildcardHandler
2016-09-30 20:58:35 +02:00
from includes.helper import configHandler
# local variables
BOTTokenAPIKey = None
BOTChatIDAPIKey = None
RICforLocationAPIKey = None
GoogleAPIKey = None
##
#
# onLoad (init) function of plugin
# will be called one time by the pluginLoader on start
#
def onLoad():
"""
While loading the plugins by pluginLoader.loadPlugins()
this onLoad() routine is called one time for initialize the plugin
@requires: nothing
@return: nothing
"""
global BOTTokenAPIKey
global BOTChatIDAPIKey
global RICforLocationAPIKey
global GoogleAPIKey
configHandler.checkConfig("Telegram")
2016-10-03 12:02:18 +02:00
BOTTokenAPIKey = globalVars.config.get("Telegram","BOTTokenAPIKey")
BOTChatIDAPIKey = globalVars.config.get("Telegram","BOTChatIDAPIKey")
RICforLocationAPIKey = globalVars.config.get("Telegram","RICforLocationAPIKey")
GoogleAPIKey = globalVars.config.get("Telegram","GoogleAPIKey")
2016-09-30 20:58:35 +02:00
return
##
#
# Main function of plugin
# will be called by the alarmHandler
#
def run(typ,freq,data):
"""
This function is the implementation of the Plugin.
2016-09-30 20:58:35 +02:00
If necessary the configuration hast to be set in the config.ini.
2016-09-30 20:58:35 +02:00
@type typ: string (FMS|ZVEI|POC)
@param typ: Typ of the dataset
2016-10-14 14:12:03 +02:00
@type data: map of data (structure see readme.md in plugin folder)
2016-09-30 20:58:35 +02:00
@param data: Contains the parameter for dispatch
@type freq: string
@keyword freq: frequency of the SDR Stick
2016-09-30 20:58:35 +02:00
@requires: If necessary the configuration hast to be set in the config.ini.
2016-09-30 20:58:35 +02:00
@return: nothing
@exception: nothing, make sure this function will never thrown an exception
"""
2016-10-16 16:07:47 +02:00
2016-09-30 20:58:35 +02:00
try:
########## User Plugin CODE ##########
try:
2017-09-15 06:33:52 +02:00
if typ in ("POC", "FMS", "ZVEI"):
logging.debug("Read format and compose output for %s-message" % typ)
2016-09-30 20:58:35 +02:00
# compose message content
2017-09-15 06:33:52 +02:00
text = globalVars.config.get("Telegram", "%s_message" % typ)
text = wildcardHandler.replaceWildcards(text, data)
2016-09-30 20:58:35 +02:00
# Initiate Telegram Bot
logging.debug("Initiate Telegram BOT")
2016-10-16 16:07:47 +02:00
bot = telegram.Bot(token='%s' % BOTTokenAPIKey)
2016-09-30 20:58:35 +02:00
# Send message to chat via Telegram BOT API
logging.debug("Send message to chat via Telegram BOT API")
2017-04-24 20:17:07 +02:00
bot.sendMessage('%s' % BOTChatIDAPIKey, text)
2016-09-30 20:58:35 +02:00
# Generate location information only for specific RIC
2017-09-15 06:33:52 +02:00
if typ == "POC" and data["ric"] == RICforLocationAPIKey:
2016-09-30 20:58:35 +02:00
# Generate map
logging.debug("Extract address from POCSAG message")
address = "+".join(data["msg"].split(')')[0].split('/',1)[1].replace('(',' ').split())
# Origin for routing, use format 'City+Street+Number'
origin = "CityOfDeparture+Street+Number"
logging.debug("Retrieve polylines from Directions API")
url = "".join(["https://maps.googleapis.com/maps/api/directions/json?origin=", origin, "&destination=", address, "&mode=driving&key=", GoogleAPIKey])
response = json.loads(requests.get(url).content.decode('utf-8'))
logging.debug("Directions API return status: %s" % response['status'])
2016-09-30 20:58:35 +02:00
logging.debug("Retrieve maps from Google")
2018-10-28 20:48:52 +01:00
url = "".join(["https://maps.googleapis.com/maps/api/staticmap?&size=480x640&maptype=roadmap&language=de&path=enc:",
response['routes'][0]['overview_polyline']['points'], "&key=", GoogleAPIKey])
with open("overview_map.png", "wb") as img: img.write(requests.get(url).content)
2018-10-28 20:48:52 +01:00
url = "".join(["https://maps.googleapis.com/maps/api/staticmap?markers=",
address, "&size=240x320&scale=2&maptype=hybrid&zoom=17&language=de&key=", GoogleAPIKey])
with open("detail_map.png", "wb") as img: img.write(requests.get(url).content)
2016-10-16 16:07:47 +02:00
2016-09-30 20:58:35 +02:00
# Send message and map with Telegram
logging.debug("Send message and maps via Telegram BOT")
bot.sendPhoto('%s' % BOTChatIDAPIKey, open('overview_map.png', 'rb'), disable_notification='true')
bot.sendPhoto('%s' % BOTChatIDAPIKey, open('detail_map.png', 'rb'), disable_notification='true')
2016-09-30 20:58:35 +02:00
# Geocoding of address
logging.debug("Geocode address")
2018-10-28 20:48:52 +01:00
url = "".join(["https://maps.googleapis.com/maps/api/geocode/json?address=",
address, "&language=de&key=", GoogleAPIKey])
gcode_result = json.loads(requests.get(url).content)
logging.debug("Geocoding API return status: %s" % gcode_result['status'])
2016-09-30 20:58:35 +02:00
logging.debug("Send location via Telegram BOT API")
2018-10-28 20:48:52 +01:00
bot.sendLocation('%s' % BOTChatIDAPIKey,
gcode_result[results][0]['geometry']['location']['lat'],
gcode_result[results][0]['geometry']['location']['lng'],
disable_notification='true')
2016-09-30 20:58:35 +02:00
else:
logging.warning("Invalid Typ: %s", typ)
except Unauthorized:
logging.error("Telegram Error: Unauthorized")
logging.debug("Telegram Error: Unauthorized", exc_info=True)
except BadRequest:
logging.error("Telegram Error: BadRequest")
logging.debug("Telegram Error: BadRequest", exc_info=True)
except NetworkError:
logging.error("Telegram Error: NetworkError")
logging.debug("Telegram Error: NetworkError", exc_info=True)
except TelegramError:
logging.error("Telegram Error: TelegramError")
logging.debug("Telegram Error: TelegramError", exc_info=True)
########## User Plugin CODE ##########
2016-09-30 20:58:35 +02:00
2016-10-02 22:59:14 +02:00
except:
2016-09-30 20:58:35 +02:00
logging.error("unknown error")
2016-10-02 22:59:14 +02:00
logging.debug("unknown error", exc_info=True)