From fb228f13ff51f5c1118b1a0c4a797e2cf439e7b9 Mon Sep 17 00:00:00 2001 From: Ricky Date: Tue, 6 Mar 2018 17:19:00 +0530 Subject: [PATCH] bots/giphy: Support bot response to empty message. --- .../bots/giphy/fixtures/test_random.json | 24 +++++++++++++++++++ zulip_bots/zulip_bots/bots/giphy/giphy.conf | 2 +- zulip_bots/zulip_bots/bots/giphy/giphy.py | 15 ++++++++---- .../zulip_bots/bots/giphy/test_giphy.py | 10 +++++--- 4 files changed, 42 insertions(+), 9 deletions(-) create mode 100644 zulip_bots/zulip_bots/bots/giphy/fixtures/test_random.json diff --git a/zulip_bots/zulip_bots/bots/giphy/fixtures/test_random.json b/zulip_bots/zulip_bots/bots/giphy/fixtures/test_random.json new file mode 100644 index 0000000..2fdc7ac --- /dev/null +++ b/zulip_bots/zulip_bots/bots/giphy/fixtures/test_random.json @@ -0,0 +1,24 @@ +{ + "request": { + "api_url": "http://api.giphy.com/v1/gifs/random", + "params": { + "api_key": "12345678" + } + }, + "response": { + "meta": { + "status": 200 + }, + "data": { + "images": { + "original": { + "url": "https://media0.giphy.com/media/ISumMYQyX4sSI/giphy.gif" + } + } + } + }, + "response-headers": { + "status": 200, + "content-type": "application/json; charset=utf-8" + } +} diff --git a/zulip_bots/zulip_bots/bots/giphy/giphy.conf b/zulip_bots/zulip_bots/bots/giphy/giphy.conf index d83ba7e..6806295 100644 --- a/zulip_bots/zulip_bots/bots/giphy/giphy.conf +++ b/zulip_bots/zulip_bots/bots/giphy/giphy.conf @@ -1,2 +1,2 @@ [giphy] -key = 12345678 +key=12345678 diff --git a/zulip_bots/zulip_bots/bots/giphy/giphy.py b/zulip_bots/zulip_bots/bots/giphy/giphy.py index 91f7d98..2f360bc 100644 --- a/zulip_bots/zulip_bots/bots/giphy/giphy.py +++ b/zulip_bots/zulip_bots/bots/giphy/giphy.py @@ -10,6 +10,7 @@ from requests.exceptions import HTTPError, ConnectionError from zulip_bots.custom_exceptions import ConfigValidationError GIPHY_TRANSLATE_API = 'http://api.giphy.com/v1/gifs/translate' +GIPHY_RANDOM_API = 'http://api.giphy.com/v1/gifs/random' class GiphyHandler(object): ''' @@ -62,22 +63,26 @@ def get_url_gif_giphy(keyword: str, api_key: str) -> Union[int, str]: # Return a URL for a Giphy GIF based on keywords given. # In case of error, e.g. failure to fetch a GIF URL, it will # return a number. - query = {'s': keyword, - 'api_key': api_key} + query = {'api_key': api_key} + if len(keyword) > 0: + query['s'] = keyword + url = GIPHY_TRANSLATE_API + else: + url = GIPHY_RANDOM_API + try: - data = requests.get(GIPHY_TRANSLATE_API, params=query) + data = requests.get(url, params=query) except requests.exceptions.ConnectionError as e: # Usually triggered by bad connection. logging.exception('Bad connection') raise data.raise_for_status() + try: gif_url = data.json()['data']['images']['original']['url'] except (TypeError, KeyError): # Usually triggered by no result in Giphy. raise GiphyNoResultException() - return gif_url - def get_bot_giphy_response(message: Dict[str, str], bot_handler: Any, config_info: Dict[str, str]) -> str: # Each exception has a specific reply should "gif_url" return a number. # The bot will post the appropriate message for the error. diff --git a/zulip_bots/zulip_bots/bots/giphy/test_giphy.py b/zulip_bots/zulip_bots/bots/giphy/test_giphy.py index 09bcaba..7f60e1d 100755 --- a/zulip_bots/zulip_bots/bots/giphy/test_giphy.py +++ b/zulip_bots/zulip_bots/bots/giphy/test_giphy.py @@ -7,10 +7,14 @@ from zulip_bots.test_lib import StubBotHandler, BotTestCase, get_bot_message_han class TestGiphyBot(BotTestCase): bot_name = "giphy" - # Override default function in BotTestCase + # Test for bot response to empty message def test_bot_responds_to_empty_message(self) -> None: - # FIXME?: Giphy does not respond to empty messages - pass + bot_response = '[Click to enlarge]' \ + '(https://media0.giphy.com/media/ISumMYQyX4sSI/giphy.gif)' \ + '[](/static/images/interactive-bot/giphy/powered-by-giphy.png)' + with self.mock_config_info({'key': '12345678'}), \ + self.mock_http_conversation('test_random'): + self.verify_reply('', bot_response) def test_normal(self) -> None: bot_response = '[Click to enlarge]' \