zulip_bots: Clean gyphy bot code.

This commit is contained in:
dkvasov 2018-05-16 17:47:12 +03:00 committed by Tim Abbott
parent 6542f8c867
commit be5a7a8c4a

View file

@ -1,10 +1,6 @@
from configparser import SafeConfigParser
from typing import Dict, Any, Union from typing import Dict, Any, Union
import requests import requests
import logging import logging
import sys
import os
import re
from requests.exceptions import HTTPError, ConnectionError from requests.exceptions import HTTPError, ConnectionError
from zulip_bots.custom_exceptions import ConfigValidationError from zulip_bots.custom_exceptions import ConfigValidationError
@ -12,14 +8,15 @@ from zulip_bots.custom_exceptions import ConfigValidationError
GIPHY_TRANSLATE_API = 'http://api.giphy.com/v1/gifs/translate' GIPHY_TRANSLATE_API = 'http://api.giphy.com/v1/gifs/translate'
GIPHY_RANDOM_API = 'http://api.giphy.com/v1/gifs/random' GIPHY_RANDOM_API = 'http://api.giphy.com/v1/gifs/random'
class GiphyHandler(object): class GiphyHandler(object):
''' """
This plugin posts a GIF in response to the keywords provided by the user. This plugin posts a GIF in response to the keywords provided by the user.
Images are provided by Giphy, through the public API. Images are provided by Giphy, through the public API.
The bot looks for messages starting with @mention of the bot The bot looks for messages starting with @mention of the bot
and responds with a message with the GIF based on provided keywords. and responds with a message with the GIF based on provided keywords.
It also responds to private messages. It also responds to private messages.
''' """
def usage(self) -> str: def usage(self) -> str:
return ''' return '''
This plugin allows users to post GIFs provided by Giphy. This plugin allows users to post GIFs provided by Giphy.
@ -38,7 +35,7 @@ class GiphyHandler(object):
raise ConfigValidationError(str(e)) raise ConfigValidationError(str(e))
except HTTPError as e: except HTTPError as e:
error_message = str(e) error_message = str(e)
if (data.status_code == 403): if data.status_code == 403:
error_message += ('This is likely due to an invalid key.\n' error_message += ('This is likely due to an invalid key.\n'
'Follow the instructions in doc.md for setting an API key.') 'Follow the instructions in doc.md for setting an API key.')
raise ConfigValidationError(error_message) raise ConfigValidationError(error_message)
@ -83,6 +80,7 @@ def get_url_gif_giphy(keyword: str, api_key: str) -> Union[int, str]:
raise GiphyNoResultException() raise GiphyNoResultException()
return gif_url return gif_url
def get_bot_giphy_response(message: Dict[str, str], bot_handler: Any, config_info: Dict[str, str]) -> str: 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. # Each exception has a specific reply should "gif_url" return a number.
# The bot will post the appropriate message for the error. # The bot will post the appropriate message for the error.