From 7acf2c396bd82feb7b3138cb4928778ece82a536 Mon Sep 17 00:00:00 2001 From: fredfishgames Date: Tue, 19 Dec 2017 21:11:12 +0000 Subject: [PATCH] Update dialogflow bot to show user friendly messages --- zulip_bots/zulip_bots/bots/dialogflow/dialogflow.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/zulip_bots/zulip_bots/bots/dialogflow/dialogflow.py b/zulip_bots/zulip_bots/bots/dialogflow/dialogflow.py index ea85c3e..b3069be 100644 --- a/zulip_bots/zulip_bots/bots/dialogflow/dialogflow.py +++ b/zulip_bots/zulip_bots/bots/dialogflow/dialogflow.py @@ -23,12 +23,13 @@ def get_bot_result(message_content: str, config: Dict[str, str], sender_id: str) response = request.getresponse() res_str = response.read().decode('utf8', 'ignore') res_json = json.loads(res_str) - if res_json['status']['errorType'] != 'success': + if res_json['status']['errorType'] != 'success' and 'result' not in res_json.keys(): return 'Error {}: {}.'.format(res_json['status']['code'], res_json['status']['errorDetails']) if res_json['result']['fulfillment']['speech'] == '': - if res_json['alternateResult']['fulfillment']['speech'] == '': - return 'Error. No result.' - return res_json['alternateResult']['fulfillment']['speech'] + if 'alternateResult' in res_json.keys(): + if res_json['alternateResult']['fulfillment']['speech'] != '': + return res_json['alternateResult']['fulfillment']['speech'] + return 'Error. No result.' return res_json['result']['fulfillment']['speech'] except Exception as e: logging.exception(str(e))