bots/giphy: Support bot response to empty message.

This commit is contained in:
Ricky 2018-03-06 17:19:00 +05:30 committed by showell
parent 38d00e9a1e
commit fb228f13ff
4 changed files with 42 additions and 9 deletions

View file

@ -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"
}
}

View file

@ -1,2 +1,2 @@
[giphy]
key = 12345678
key=12345678

View file

@ -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.

View file

@ -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]' \