bots: Use requests instead of unirest for Yoda bot.

Fixes #3500.
This commit is contained in:
sinwar 2017-02-03 08:26:47 +05:30 committed by Tim Abbott
parent 4c482bfb66
commit 46e7e05516
2 changed files with 14 additions and 38 deletions

View file

@ -10,16 +10,6 @@ It looks for messages starting with '@yoda'. You will need to have a
Mashape API key. Please see instructions for getting one below. Mashape API key. Please see instructions for getting one below.
## Setup ## Setup
This bot uses the python library `unirest` which is not a
dependency of Zulip. To use this module, you will have to
install it in your local machine. In your terminal, enter
the following command:
* $ sudo pip install unirest --upgrade
Note:
You might have to use `pip3` if you are using python 3.
The install command would also download any dependency
required by `unirest`.
Before running this bot, make sure to get a Mashape API Key. Before running this bot, make sure to get a Mashape API Key.
Go to this link: Go to this link:

View file

@ -1,30 +1,16 @@
# See readme-yoda-bot.md for instructions on running this code. # See readme-yoda-bot.md for instructions on running this code.
"""
This bot uses the python library `unirest` which is not a
dependency of Zulip. To use this module, you will have to
install it in your local machine. In your terminal, enter
the following command:
$ sudo pip install unirest --upgrade
Note:
* You might have to use `pip3` if you are using python 3.
* The install command would also download any dependency
required by `unirest`.
"""
from __future__ import print_function from __future__ import print_function
import os import os
import logging import logging
import ssl import ssl
import sys import sys
try: try:
import unirest import requests
except ImportError: except ImportError as e:
logging.error("Dependency missing!!\n%s" % (__doc__)) logging.error("Dependency missing!!\n{}".format(e))
sys.exit(0) sys.exit(0)
HELP_MESSAGE = ''' HELP_MESSAGE = '''
This bot allows users to translate a sentence into This bot allows users to translate a sentence into
'Yoda speak'. 'Yoda speak'.
@ -83,21 +69,21 @@ handler_class = YodaSpeakHandler
def send_to_yoda_api(sentence, api_key): def send_to_yoda_api(sentence, api_key):
# function for sending sentence to api # function for sending sentence to api
response = unirest.get("https://yoda.p.mashape.com/yoda?sentence=" + sentence, response = requests.get("https://yoda.p.mashape.com/yoda?sentence=" + sentence,
headers={ headers={
"X-Mashape-Key": api_key, "X-Mashape-Key": api_key,
"Accept": "text/plain" "Accept": "text/plain"
} }
) )
if response.code == 200: if response.status_code == 200:
return response.body return response.text
if response.code == 403: if response.status_code == 403:
raise ApiKeyError raise ApiKeyError
else: else:
error_message = response.body['message'] error_message = response.text['message']
logging.error(error_message) logging.error(error_message)
error_code = response.code error_code = response.status_code
error_message = error_message + 'Error code: ' + error_code +\ error_message = error_message + 'Error code: ' + error_code +\
' Did you follow the instructions in the `readme-yoda-bot.md` file?' ' Did you follow the instructions in the `readme-yoda-bot.md` file?'
return error_message return error_message