mypy: Add annotations for Yoda Bot.
This commit is contained in:
parent
8f64405bae
commit
3c794b384b
|
@ -64,6 +64,8 @@ force_include = [
|
||||||
"zulip_bots/zulip_bots/bots/xkcd/test_xkcd.py",
|
"zulip_bots/zulip_bots/bots/xkcd/test_xkcd.py",
|
||||||
"zulip_bots/zulip_bots/bots/wikipedia/wikipedia.py",
|
"zulip_bots/zulip_bots/bots/wikipedia/wikipedia.py",
|
||||||
"zulip_bots/zulip_bots/bots/wikipedia/test_wikipedia.py",
|
"zulip_bots/zulip_bots/bots/wikipedia/test_wikipedia.py",
|
||||||
|
"zulip_bots/zulip_bots/bots/yoda/yoda.py",
|
||||||
|
"zulip_bots/zulip_bots/bots/yoda/test_yoda.py",
|
||||||
]
|
]
|
||||||
|
|
||||||
parser = argparse.ArgumentParser(description="Run mypy on files tracked by git.")
|
parser = argparse.ArgumentParser(description="Run mypy on files tracked by git.")
|
||||||
|
|
|
@ -17,7 +17,7 @@ class TestYodaBot(StubBotTestCase):
|
||||||
@mention-bot You will learn how to speak like me someday.
|
@mention-bot You will learn how to speak like me someday.
|
||||||
'''
|
'''
|
||||||
|
|
||||||
def _test(self, message, response, fixture=None):
|
def _test(self, message: str, response: str, fixture: str=None) -> None:
|
||||||
with self.mock_config_info({'api_key': '12345678'}):
|
with self.mock_config_info({'api_key': '12345678'}):
|
||||||
if fixture is not None:
|
if fixture is not None:
|
||||||
with self.mock_http_conversation(fixture):
|
with self.mock_http_conversation(fixture):
|
||||||
|
@ -26,11 +26,10 @@ class TestYodaBot(StubBotTestCase):
|
||||||
self.verify_reply(message, response)
|
self.verify_reply(message, response)
|
||||||
|
|
||||||
# Override default function in StubBotTestCase
|
# Override default function in StubBotTestCase
|
||||||
def test_bot_responds_to_empty_message(self):
|
def test_bot_responds_to_empty_message(self) -> None:
|
||||||
self._test('', self.help_text)
|
self._test('', self.help_text)
|
||||||
|
|
||||||
def test_bot(self):
|
def test_bot(self) -> None:
|
||||||
|
|
||||||
# Test normal sentence (1).
|
# Test normal sentence (1).
|
||||||
self._test('You will learn how to speak like me someday.',
|
self._test('You will learn how to speak like me someday.',
|
||||||
"Learn how to speak like me someday, you will. Yes, hmmm.",
|
"Learn how to speak like me someday, you will. Yes, hmmm.",
|
||||||
|
|
|
@ -4,6 +4,7 @@ from __future__ import print_function
|
||||||
import logging
|
import logging
|
||||||
import ssl
|
import ssl
|
||||||
import sys
|
import sys
|
||||||
|
from zulip_bots.lib import ExternalBotHandler
|
||||||
try:
|
try:
|
||||||
import requests
|
import requests
|
||||||
except ImportError as e:
|
except ImportError as e:
|
||||||
|
@ -37,10 +38,10 @@ class YodaSpeakHandler(object):
|
||||||
This bot will allow users to translate a sentence into 'Yoda speak'.
|
This bot will allow users to translate a sentence into 'Yoda speak'.
|
||||||
It looks for messages starting with '@mention-bot'.
|
It looks for messages starting with '@mention-bot'.
|
||||||
'''
|
'''
|
||||||
def initialize(self, bot_handler):
|
def initialize(self, bot_handler: ExternalBotHandler) -> None:
|
||||||
self.api_key = bot_handler.get_config_info('yoda')['api_key']
|
self.api_key = bot_handler.get_config_info('yoda')['api_key']
|
||||||
|
|
||||||
def usage(self):
|
def usage(self) -> str:
|
||||||
return '''
|
return '''
|
||||||
This bot will allow users to translate a sentence into
|
This bot will allow users to translate a sentence into
|
||||||
'Yoda speak'.
|
'Yoda speak'.
|
||||||
|
@ -54,10 +55,10 @@ class YodaSpeakHandler(object):
|
||||||
@mention-bot You will learn how to speak like me someday.
|
@mention-bot You will learn how to speak like me someday.
|
||||||
'''
|
'''
|
||||||
|
|
||||||
def handle_message(self, message, bot_handler):
|
def handle_message(self, message: dict, bot_handler: ExternalBotHandler) -> None:
|
||||||
self.handle_input(message, bot_handler)
|
self.handle_input(message, bot_handler)
|
||||||
|
|
||||||
def send_to_yoda_api(self, sentence):
|
def send_to_yoda_api(self, sentence: str) -> str:
|
||||||
# function for sending sentence to api
|
# function for sending sentence to api
|
||||||
response = requests.get("https://yoda.p.mashape.com/yoda?sentence=" + sentence,
|
response = requests.get("https://yoda.p.mashape.com/yoda?sentence=" + sentence,
|
||||||
headers={
|
headers={
|
||||||
|
@ -73,22 +74,21 @@ class YodaSpeakHandler(object):
|
||||||
if response.status_code == 503:
|
if response.status_code == 503:
|
||||||
raise ServiceUnavailableError
|
raise ServiceUnavailableError
|
||||||
else:
|
else:
|
||||||
error_message = response.text['message']
|
error_message = response.json()['message']
|
||||||
logging.error(error_message)
|
logging.error(error_message)
|
||||||
error_code = response.status_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.md` file?'
|
' Did you follow the instructions in the `readme.md` file?'
|
||||||
return error_message
|
return error_message
|
||||||
|
|
||||||
def format_input(self, original_content):
|
def format_input(self, original_content: str) -> str:
|
||||||
# gets rid of whitespace around the edges, so that they aren't a problem in the future
|
# gets rid of whitespace around the edges, so that they aren't a problem in the future
|
||||||
message_content = original_content.strip()
|
message_content = original_content.strip()
|
||||||
# replaces all spaces with '+' to be in the format the api requires
|
# replaces all spaces with '+' to be in the format the api requires
|
||||||
sentence = message_content.replace(' ', '+')
|
sentence = message_content.replace(' ', '+')
|
||||||
return sentence
|
return sentence
|
||||||
|
|
||||||
def handle_input(self, message, bot_handler):
|
def handle_input(self, message: dict, bot_handler: ExternalBotHandler) -> None:
|
||||||
|
|
||||||
original_content = message['content']
|
original_content = message['content']
|
||||||
|
|
||||||
if self.is_help(original_content) or (original_content == ""):
|
if self.is_help(original_content) or (original_content == ""):
|
||||||
|
@ -113,7 +113,7 @@ class YodaSpeakHandler(object):
|
||||||
|
|
||||||
bot_handler.send_reply(message, reply_message)
|
bot_handler.send_reply(message, reply_message)
|
||||||
|
|
||||||
def send_message(self, bot_handler, message, stream, subject):
|
def send_message(self, bot_handler: ExternalBotHandler, message: str, stream: str, subject: str) -> None:
|
||||||
# function for sending a message
|
# function for sending a message
|
||||||
bot_handler.send_message(dict(
|
bot_handler.send_message(dict(
|
||||||
type='stream',
|
type='stream',
|
||||||
|
@ -122,7 +122,7 @@ class YodaSpeakHandler(object):
|
||||||
content=message
|
content=message
|
||||||
))
|
))
|
||||||
|
|
||||||
def is_help(self, original_content):
|
def is_help(self, original_content: str) -> bool:
|
||||||
# gets rid of whitespace around the edges, so that they aren't a problem in the future
|
# gets rid of whitespace around the edges, so that they aren't a problem in the future
|
||||||
message_content = original_content.strip()
|
message_content = original_content.strip()
|
||||||
if message_content == 'help':
|
if message_content == 'help':
|
||||||
|
|
Loading…
Reference in a new issue