From 79b32f6c05488baed724f3f790f2ed9519889ca5 Mon Sep 17 00:00:00 2001 From: Shivam Gera Date: Fri, 29 Dec 2017 04:28:24 +0530 Subject: [PATCH] Update google_translate.py --- .../bots/google_translate/google_translate.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/zulip_bots/zulip_bots/bots/google_translate/google_translate.py b/zulip_bots/zulip_bots/bots/google_translate/google_translate.py index 7df1769..e38e521 100644 --- a/zulip_bots/zulip_bots/bots/google_translate/google_translate.py +++ b/zulip_bots/zulip_bots/bots/google_translate/google_translate.py @@ -2,6 +2,7 @@ # googletranslate.conf in this (zulip_bots/bots/googletranslate/) directory. import requests +from requests.exceptions import HTTPError, ConnectionError class GoogleTranslateHandler(object): ''' @@ -19,6 +20,19 @@ class GoogleTranslateHandler(object): def initialize(self, bot_handler): self.config_info = bot_handler.get_config_info('googletranslate') self.supported_languages = get_supported_languages(self.config_info['key']) + query = {'key': self.config_info['key'], + 'target': 'en'} + try: + data = requests.get(api_url + '/languages', params = query) + except HTTPError as e: + if (e.response.json()['error']['errors'][0]['reason'] == 'keyInvalid'): + logging.error('Invalid key.' + 'Follow the instructions in doc.md for setting API key.') + sys.exit(1) + else: + raise + except ConnectionError: + logging.warning('Bad connection') def handle_message(self, message, bot_handler): bot_response = get_translate_bot_response(message['content'],