From 22aea6913648ce559e4403bdea0eae842f536d00 Mon Sep 17 00:00:00 2001 From: PeterLaemmle Date: Fri, 14 Oct 2016 14:04:35 +0200 Subject: [PATCH] Update Telegram.py Added exception for Telegram errors --- plugins/Telegram/Telegram.py | 41 ++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/plugins/Telegram/Telegram.py b/plugins/Telegram/Telegram.py index 25e0c7c..2f1c832 100644 --- a/plugins/Telegram/Telegram.py +++ b/plugins/Telegram/Telegram.py @@ -58,41 +58,44 @@ def onLoad(): 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 readme.md in plugin folder) + @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 """ - + try: - #if configHandler.checkConfig("Telegram"): #read and debug the config (let empty if no config used) - - ########## User Plugin CODE ########## + ########## User Plugin CODE ########## + try: 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) + 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: + 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") @@ -101,15 +104,15 @@ def run(typ,freq,data): # 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')) + bot.sendPhoto('%s' % BOTChatIDAPIKey, open('overview_map.png', 'rb'), disable_notification='true') + bot.sendPhoto('%s' % BOTChatIDAPIKey, open('detail_map.png', 'rb'), disable_notification='true') # 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']) + bot.sendLocation('%s' % BOTChatIDAPIKey, gcode_result[0]['geometry']['location']['lat'], gcode_result[0]['geometry']['location']['lng'], disable_notification='true') elif typ == "FMS": logging.debug("FMS not supported yet") @@ -117,7 +120,19 @@ def run(typ,freq,data): logging.debug("ZVEI not supported yet") else: logging.warning("Invalid Typ: %s", typ) - ########## User Plugin CODE ########## + 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 ########## except: logging.error("unknown error")