From b9e4e5f072d90adfdb4885a99d748e59ec5f29e4 Mon Sep 17 00:00:00 2001 From: PeterLaemmle Date: Sat, 20 Aug 2016 14:05:02 +0200 Subject: [PATCH 1/8] Create sendTelegram.py BOSWatch plugin to send messages using the Telegram messenger BOT API. --- plugins/sendTelegram/sendTelegram.py | 137 +++++++++++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 plugins/sendTelegram/sendTelegram.py diff --git a/plugins/sendTelegram/sendTelegram.py b/plugins/sendTelegram/sendTelegram.py new file mode 100644 index 0000000..568f8e3 --- /dev/null +++ b/plugins/sendTelegram/sendTelegram.py @@ -0,0 +1,137 @@ +#!/usr/bin/python +# -*- coding: UTF-8 -*- + +""" +Plugin to send FMS-, ZVEI- and POCSAG-messages via Telegram + +@author: Peter Laemmle + +@requires: none +""" + +# +# Imports +# +import logging # Global logger +import httplib, urllib, telegram, googlemaps +from includes import globals # Global variables + +# Helper function, uncomment to use +from includes.helper import configHandler +from includes.helper import timeHandler + +# 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("sendTelegram") + BOTTokenAPIKey = globals.config.get("sendTelegram","BOTTokenAPIKey") + BOTChatIDAPIKey = globals.config.get("sendTelegram","BOTChatIDAPIKey") + RICforLocationAPIKey = globals.config.get("sendTelegram","RICforLocationAPIKey") + GoogleAPIKey = globals.config.get("sendTelegram","GoogleAPIKey") + + return + + +## +# +# Main function of plugin +# will be called by the alarmHandler +# +def run(typ,freq,data): + """ + This function is the implementation of the Plugin. + + If necessary the configuration hast to be set in the config.ini. + + @type typ: string (FMS|ZVEI|POC) + @param typ: Typ of the dataset + @type data: map of data (structure see interface.txt) + @param data: Contains the parameter for dispatch + @type freq: string + @keyword freq: frequency of the SDR Stick + + @requires: If necessary the configuration hast to be set in the config.ini. + + @return: nothing + @exception: nothing, make sure this function will never thrown an exception + """ + + global BOTTokenKey + global BOTChatIDAPIKey + global RICforLocationAPIKey + global GoogleAPIKey + + try: + if configHandler.checkConfig("sendTelegram"): #read and debug the config (let empty if no config used) + + ########## User Plugin CODE ########## + if typ == "POC": + logging.debug("Compose output from POCSAG-message") + # compose message content + output = timeHandler.curtime()+"\n"+data["ric"]+"("+data["functionChar"]+")\n"+data["description"]+"\n"+data["msg"] + + # Initiate Telegram Bot + logging.debug("Initiate Telegram BOT") + bot = telegram.Bot(token='%s' % BOTTokenAPIKey) + + # Send message to chat via Telegram BOT API + logging.debug("Send message to chat via Telegram BOT API") + bot.sendMessage('%s' % BOTChatIDAPIKey, output) + + # Generate location information only for specific RIC + if data["ric"] == RICforLocationAPIKey: + # Generate map + logging.debug("Extract address from POCSAG message") + address = "+".join(data["msg"].split(')')[0].split('/',1)[1].replace('(',' ').split()) + + logging.debug("Retrieve maps from Google") + url = "+".join(["http://maps.googleapis.com/maps/api/staticmap?markers=", address, "&size=480x640&maptype=roadmap&zoom=16&key=", GoogleAPIKey]) + urllib.urlretrieve(url, "overview_map.png") + url = "+".join(["http://maps.googleapis.com/maps/api/staticmap?markers=", address, "&size=240x320&scale=2&maptype=hybrid&zoom=17&key=", GoogleAPIKey]) + urllib.urlretrieve(url, "detail_map.png") + + # Send message and map with Telegram + logging.debug("Send message and maps via Telegram BOT") + bot.sendPhoto('%s' % BOTChatIDAPIKey, open('overview_map.png', 'rb')) + bot.sendPhoto('%s' % BOTChatIDAPIKey, open('detail_map.png', 'rb')) + + # Geocoding of address + logging.debug("Geocode address") + gcode = googlemaps.Client(key='%s' % GoogleAPIKey) + gcode_result = gcode.geocode(address) + logging.debug("Send location via Telegram BOT API") + bot.sendLocation('%s' % BOTChatIDAPIKey, gcode_result[0]['geometry']['location']['lat'], gcode_result[0]['geometry']['location']['lng']) + + elif typ == "FMS": + logging.debug("FMS not supported yet") + elif typ == "ZVEI": + logging.debug("ZVEI not supported yet") + else: + logging.warning("Invalid Typ: %s", typ) + ########## User Plugin CODE ########## + + except: + logging.error("unknown error") + logging.debug("unknown error", exc_info=True) From 22d400b30aa524b6a93fca6dd23ef5a1a03a1026 Mon Sep 17 00:00:00 2001 From: PeterLaemmle Date: Sat, 20 Aug 2016 14:08:41 +0200 Subject: [PATCH 2/8] Update config.template.ini --- config/config.template.ini | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/config/config.template.ini b/config/config.template.ini index a7b8c26..07ad115 100644 --- a/config/config.template.ini +++ b/config/config.template.ini @@ -126,6 +126,7 @@ BosMon = 0 firEmergency = 0 jsonSocket = 0 notifyMyAndroid = 0 +sendTelegram = 0 # for developing template-module template = 0 @@ -283,6 +284,15 @@ appName = BOSWatch # configuration loaded from csv/nma.csv usecsv = 0 +[sendTelegram] +# This is your unique BOT token +BOTTokenAPIKey = +# This is the chat your BOT will send the messages to +BOTChatIDAPIKey = +# The location will be extracted from this RIC +RICforLocationAPIKey = +# This is your Google API key +GoogleAPIKey = ##################### ##### Not ready yet # From 6946979f05ed9e18c3921645b8560e86de732291 Mon Sep 17 00:00:00 2001 From: PeterLaemmle Date: Tue, 23 Aug 2016 17:16:07 +0200 Subject: [PATCH 3/8] Update sendTelegram.py --- plugins/sendTelegram/sendTelegram.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/sendTelegram/sendTelegram.py b/plugins/sendTelegram/sendTelegram.py index 568f8e3..207cae7 100644 --- a/plugins/sendTelegram/sendTelegram.py +++ b/plugins/sendTelegram/sendTelegram.py @@ -6,7 +6,7 @@ Plugin to send FMS-, ZVEI- and POCSAG-messages via Telegram @author: Peter Laemmle -@requires: none +@requires: Telegram BOT token, Telegram chat ID, library python-telegram-bot and optional googlemaps """ # From d085cd3bbeb52b7a0e0a09bcab60d08c9736b727 Mon Sep 17 00:00:00 2001 From: PeterLaemmle Date: Tue, 23 Aug 2016 17:27:32 +0200 Subject: [PATCH 4/8] Update config.template.ini --- config/config.template.ini | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/config/config.template.ini b/config/config.template.ini index 07ad115..83e953c 100644 --- a/config/config.template.ini +++ b/config/config.template.ini @@ -285,13 +285,16 @@ appName = BOSWatch usecsv = 0 [sendTelegram] -# This is your unique BOT token +# This is your unique BOT token. You will get it from the BotFather once you have created your BOT. BOTTokenAPIKey = -# This is the chat your BOT will send the messages to +# Create a group chat with your BOT and enter the chat ID here. +# The plugin will send messages as your BOT and post everything in this group chat. BOTChatIDAPIKey = -# The location will be extracted from this RIC +# The plugin can extract a location from the POCSAG message. +# However, this will be done for the following RIC only (7 digits e.g. 0012345). RICforLocationAPIKey = -# This is your Google API key +# This is your Google API key. +# Required if you want to create a map based on location information received with the above RIC. GoogleAPIKey = ##################### From 91fd449bee1db3bece397027b69353e5aab8ab49 Mon Sep 17 00:00:00 2001 From: PeterLaemmle Date: Fri, 30 Sep 2016 20:54:35 +0200 Subject: [PATCH 5/8] Update sendTelegram.py --- plugins/sendTelegram/sendTelegram.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/plugins/sendTelegram/sendTelegram.py b/plugins/sendTelegram/sendTelegram.py index 207cae7..68bb0a9 100644 --- a/plugins/sendTelegram/sendTelegram.py +++ b/plugins/sendTelegram/sendTelegram.py @@ -45,11 +45,11 @@ def onLoad(): global RICforLocationAPIKey global GoogleAPIKey - configHandler.checkConfig("sendTelegram") - BOTTokenAPIKey = globals.config.get("sendTelegram","BOTTokenAPIKey") - BOTChatIDAPIKey = globals.config.get("sendTelegram","BOTChatIDAPIKey") - RICforLocationAPIKey = globals.config.get("sendTelegram","RICforLocationAPIKey") - GoogleAPIKey = globals.config.get("sendTelegram","GoogleAPIKey") + configHandler.checkConfig("Telegram") + BOTTokenAPIKey = globals.config.get("Telegram","BOTTokenAPIKey") + BOTChatIDAPIKey = globals.config.get("Telegram","BOTChatIDAPIKey") + RICforLocationAPIKey = globals.config.get("Telegram","RICforLocationAPIKey") + GoogleAPIKey = globals.config.get("Telegram","GoogleAPIKey") return @@ -84,7 +84,7 @@ def run(typ,freq,data): global GoogleAPIKey try: - if configHandler.checkConfig("sendTelegram"): #read and debug the config (let empty if no config used) + #if configHandler.checkConfig("Telegram"): #read and debug the config (let empty if no config used) ########## User Plugin CODE ########## if typ == "POC": From 4dabea7bafce6805b042e7d8f2e98fddd9660bc4 Mon Sep 17 00:00:00 2001 From: PeterLaemmle Date: Fri, 30 Sep 2016 20:55:27 +0200 Subject: [PATCH 6/8] Update config.template.ini --- config/config.template.ini | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config/config.template.ini b/config/config.template.ini index 83e953c..5c39f05 100644 --- a/config/config.template.ini +++ b/config/config.template.ini @@ -126,7 +126,7 @@ BosMon = 0 firEmergency = 0 jsonSocket = 0 notifyMyAndroid = 0 -sendTelegram = 0 +Telegram = 0 # for developing template-module template = 0 @@ -284,7 +284,7 @@ appName = BOSWatch # configuration loaded from csv/nma.csv usecsv = 0 -[sendTelegram] +[Telegram] # This is your unique BOT token. You will get it from the BotFather once you have created your BOT. BOTTokenAPIKey = # Create a group chat with your BOT and enter the chat ID here. From d1c5708d890d8edf7de3568064b3bafd1d5cff41 Mon Sep 17 00:00:00 2001 From: PeterLaemmle Date: Fri, 30 Sep 2016 20:58:35 +0200 Subject: [PATCH 7/8] Create Telegram.py --- plugins/Telegram/Telegram.py | 129 +++++++++++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 plugins/Telegram/Telegram.py diff --git a/plugins/Telegram/Telegram.py b/plugins/Telegram/Telegram.py new file mode 100644 index 0000000..e328952 --- /dev/null +++ b/plugins/Telegram/Telegram.py @@ -0,0 +1,129 @@ +#!/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 googlemaps +""" + +# +# Imports +# +import logging # Global logger +import httplib, urllib, telegram, googlemaps +from includes import globals # Global variables + +# Helper function, uncomment to use +from includes.helper import configHandler +from includes.helper import timeHandler + +# 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") + BOTTokenAPIKey = globals.config.get("Telegram","BOTTokenAPIKey") + BOTChatIDAPIKey = globals.config.get("Telegram","BOTChatIDAPIKey") + RICforLocationAPIKey = globals.config.get("Telegram","RICforLocationAPIKey") + GoogleAPIKey = globals.config.get("Telegram","GoogleAPIKey") + + return + + +## +# +# Main function of plugin +# will be called by the alarmHandler +# +def run(typ,freq,data): + """ + This function is the implementation of the Plugin. + If necessary the configuration hast to be set in the config.ini. + @type typ: string (FMS|ZVEI|POC) + @param typ: Typ of the dataset + @type data: map of data (structure see interface.txt) + @param data: Contains the parameter for dispatch + @type freq: string + @keyword freq: frequency of the SDR Stick + @requires: If necessary the configuration hast to be set in the config.ini. + @return: nothing + @exception: nothing, make sure this function will never thrown an exception + """ + + global BOTTokenKey + global BOTChatIDAPIKey + global RICforLocationAPIKey + global GoogleAPIKey + + try: + #if configHandler.checkConfig("Telegram"): #read and debug the config (let empty if no config used) + + ########## User Plugin CODE ########## + if typ == "POC": + logging.debug("Compose output from POCSAG-message") + # compose message content + output = timeHandler.curtime()+"\n"+data["ric"]+"("+data["functionChar"]+")\n"+data["description"]+"\n"+data["msg"] + + # Initiate Telegram Bot + logging.debug("Initiate Telegram BOT") + bot = telegram.Bot(token='%s' % BOTTokenAPIKey) + + # Send message to chat via Telegram BOT API + logging.debug("Send message to chat via Telegram BOT API") + bot.sendMessage('%s' % BOTChatIDAPIKey, output) + + # Generate location information only for specific RIC + if data["ric"] == RICforLocationAPIKey: + # Generate map + logging.debug("Extract address from POCSAG message") + address = "+".join(data["msg"].split(')')[0].split('/',1)[1].replace('(',' ').split()) + + logging.debug("Retrieve maps from Google") + url = "+".join(["http://maps.googleapis.com/maps/api/staticmap?markers=", address, "&size=480x640&maptype=roadmap&zoom=16&key=", GoogleAPIKey]) + urllib.urlretrieve(url, "overview_map.png") + url = "+".join(["http://maps.googleapis.com/maps/api/staticmap?markers=", address, "&size=240x320&scale=2&maptype=hybrid&zoom=17&key=", GoogleAPIKey]) + urllib.urlretrieve(url, "detail_map.png") + + # Send message and map with Telegram + logging.debug("Send message and maps via Telegram BOT") + bot.sendPhoto('%s' % BOTChatIDAPIKey, open('overview_map.png', 'rb')) + bot.sendPhoto('%s' % BOTChatIDAPIKey, open('detail_map.png', 'rb')) + + # Geocoding of address + logging.debug("Geocode address") + gcode = googlemaps.Client(key='%s' % GoogleAPIKey) + gcode_result = gcode.geocode(address) + logging.debug("Send location via Telegram BOT API") + bot.sendLocation('%s' % BOTChatIDAPIKey, gcode_result[0]['geometry']['location']['lat'], gcode_result[0]['geometry']['location']['lng']) + + elif typ == "FMS": + logging.debug("FMS not supported yet") + elif typ == "ZVEI": + logging.debug("ZVEI not supported yet") + else: + logging.warning("Invalid Typ: %s", typ) + ########## User Plugin CODE ########## + +except: + logging.error("unknown error") +logging.debug("unknown error", exc_info=True) From 6b315e2a5c35aed763bad213334b41f3e09aecc0 Mon Sep 17 00:00:00 2001 From: PeterLaemmle Date: Fri, 30 Sep 2016 20:59:02 +0200 Subject: [PATCH 8/8] Delete sendTelegram.py --- plugins/sendTelegram/sendTelegram.py | 137 --------------------------- 1 file changed, 137 deletions(-) delete mode 100644 plugins/sendTelegram/sendTelegram.py diff --git a/plugins/sendTelegram/sendTelegram.py b/plugins/sendTelegram/sendTelegram.py deleted file mode 100644 index 68bb0a9..0000000 --- a/plugins/sendTelegram/sendTelegram.py +++ /dev/null @@ -1,137 +0,0 @@ -#!/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 googlemaps -""" - -# -# Imports -# -import logging # Global logger -import httplib, urllib, telegram, googlemaps -from includes import globals # Global variables - -# Helper function, uncomment to use -from includes.helper import configHandler -from includes.helper import timeHandler - -# 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") - BOTTokenAPIKey = globals.config.get("Telegram","BOTTokenAPIKey") - BOTChatIDAPIKey = globals.config.get("Telegram","BOTChatIDAPIKey") - RICforLocationAPIKey = globals.config.get("Telegram","RICforLocationAPIKey") - GoogleAPIKey = globals.config.get("Telegram","GoogleAPIKey") - - return - - -## -# -# Main function of plugin -# will be called by the alarmHandler -# -def run(typ,freq,data): - """ - This function is the implementation of the Plugin. - - If necessary the configuration hast to be set in the config.ini. - - @type typ: string (FMS|ZVEI|POC) - @param typ: Typ of the dataset - @type data: map of data (structure see interface.txt) - @param data: Contains the parameter for dispatch - @type freq: string - @keyword freq: frequency of the SDR Stick - - @requires: If necessary the configuration hast to be set in the config.ini. - - @return: nothing - @exception: nothing, make sure this function will never thrown an exception - """ - - global BOTTokenKey - global BOTChatIDAPIKey - global RICforLocationAPIKey - global GoogleAPIKey - - try: - #if configHandler.checkConfig("Telegram"): #read and debug the config (let empty if no config used) - - ########## User Plugin CODE ########## - if typ == "POC": - logging.debug("Compose output from POCSAG-message") - # compose message content - output = timeHandler.curtime()+"\n"+data["ric"]+"("+data["functionChar"]+")\n"+data["description"]+"\n"+data["msg"] - - # Initiate Telegram Bot - logging.debug("Initiate Telegram BOT") - bot = telegram.Bot(token='%s' % BOTTokenAPIKey) - - # Send message to chat via Telegram BOT API - logging.debug("Send message to chat via Telegram BOT API") - bot.sendMessage('%s' % BOTChatIDAPIKey, output) - - # Generate location information only for specific RIC - if data["ric"] == RICforLocationAPIKey: - # Generate map - logging.debug("Extract address from POCSAG message") - address = "+".join(data["msg"].split(')')[0].split('/',1)[1].replace('(',' ').split()) - - logging.debug("Retrieve maps from Google") - url = "+".join(["http://maps.googleapis.com/maps/api/staticmap?markers=", address, "&size=480x640&maptype=roadmap&zoom=16&key=", GoogleAPIKey]) - urllib.urlretrieve(url, "overview_map.png") - url = "+".join(["http://maps.googleapis.com/maps/api/staticmap?markers=", address, "&size=240x320&scale=2&maptype=hybrid&zoom=17&key=", GoogleAPIKey]) - urllib.urlretrieve(url, "detail_map.png") - - # Send message and map with Telegram - logging.debug("Send message and maps via Telegram BOT") - bot.sendPhoto('%s' % BOTChatIDAPIKey, open('overview_map.png', 'rb')) - bot.sendPhoto('%s' % BOTChatIDAPIKey, open('detail_map.png', 'rb')) - - # Geocoding of address - logging.debug("Geocode address") - gcode = googlemaps.Client(key='%s' % GoogleAPIKey) - gcode_result = gcode.geocode(address) - logging.debug("Send location via Telegram BOT API") - bot.sendLocation('%s' % BOTChatIDAPIKey, gcode_result[0]['geometry']['location']['lat'], gcode_result[0]['geometry']['location']['lng']) - - elif typ == "FMS": - logging.debug("FMS not supported yet") - elif typ == "ZVEI": - logging.debug("ZVEI not supported yet") - else: - logging.warning("Invalid Typ: %s", typ) - ########## User Plugin CODE ########## - - except: - logging.error("unknown error") - logging.debug("unknown error", exc_info=True)