From e1759100655303c0a555fa6f576d72a92c3c71c1 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Sun, 4 Oct 2015 11:36:21 +0200 Subject: [PATCH] error handling --- bot.py | 9 ++++++--- steam.py | 5 ++++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/bot.py b/bot.py index 9abbd22d..4f0d4797 100644 --- a/bot.py +++ b/bot.py @@ -5,7 +5,7 @@ import steam while(True): #Guarda il comando ricevuto. msg = telegram.getUpdates() - print(msg) + print(msg['text']) cmd = msg['text'].split(' ') sby = msg['chat']['id'] if(cmd[0].startswith('/ahnonlosoio')): @@ -17,6 +17,9 @@ while(True): elif(cmd[0].startswith('/playing')): if(cmd[1] is not None): n = steam.getNumberOfCurrentPlayers(cmd[1]) - telegram.sendMessage("In questo momento, " + str(n) + " persone stanno giocando a <" + cmd[1] + ">", sby) + if(n == None): + telegram.sendMessage(chr(9888) + " L'app specificata non esiste!", sby) + else: + telegram.sendMessage("In questo momento, " + str(n) + " persone stanno giocando a *ID: " + cmd[1] + "*", sby) else: - telegram.sendMessage("Non hai specificato un AppID!", sby) \ No newline at end of file + telegram.sendMessage(chr(9888) + " Non hai specificato un AppID!\nLa sintassi corretta รจ /playing .", sby) \ No newline at end of file diff --git a/steam.py b/steam.py index 8b6c6212..68e58097 100644 --- a/steam.py +++ b/steam.py @@ -25,7 +25,10 @@ def getNumberOfCurrentPlayers(appid): } #Manda la richiesta ai server di Steam. r = requests.get("http://api.steampowered.com/ISteamUserStats/GetNumberOfCurrentPlayers/v0001/", params=parametri).json() - return r['response']['player_count'] + if('player_count' in r['response']): + return r['response']['player_count'] + else: + return None def getPlayerAchievements(appid, steamid): """Ottieni gli achievement del giocatore e del gioco specificato."""