zulip_bots: Make state_handler property of bot_handler.

This commit is contained in:
derAnfaenger 2017-10-23 12:17:46 +02:00
parent 45c38d0dcf
commit eb6982e670
25 changed files with 51 additions and 51 deletions

View file

@ -180,7 +180,7 @@ class CommuteHandler(object):
result = validate_requests(r)
return result
def handle_message(self, message, bot_handler, state_handler):
def handle_message(self, message, bot_handler):
original_content = message['content']
query = original_content.split()

View file

@ -46,7 +46,7 @@ class ConverterHandler(object):
all supported units.
'''
def handle_message(self, message, bot_handler, state_handler):
def handle_message(self, message, bot_handler):
bot_response = get_bot_converter_response(message, bot_handler)
bot_handler.send_reply(message, bot_response)

View file

@ -23,7 +23,7 @@ class DefineHandler(object):
messages with @mention-bot.
'''
def handle_message(self, message, bot_handler, state_handler):
def handle_message(self, message, bot_handler):
original_content = message['content'].strip()
bot_response = self.get_bot_define_response(original_content)

View file

@ -28,7 +28,7 @@ class EncryptHandler(object):
Feeding encrypted messages into the bot decrypts them.
'''
def handle_message(self, message, bot_handler, state_handler):
def handle_message(self, message, bot_handler):
bot_response = self.get_bot_encrypt_response(message)
bot_handler.send_reply(message, bot_response)

View file

@ -22,7 +22,7 @@ class FollowupHandler(object):
called "followup" that your API user can send to.
'''
def handle_message(self, message, bot_handler, state_handler):
def handle_message(self, message, bot_handler):
if message['content'] == '':
bot_response = "Please specify the message you want to send to followup stream after @mention-bot"
bot_handler.send_reply(message, bot_response)

View file

@ -47,7 +47,7 @@ Example Inputs:
def send_info(self, message, letter, bot_handler):
bot_handler.send_reply(message, letter)
def handle_message(self, message, bot_handler, state_handler):
def handle_message(self, message, bot_handler):
words = message['content'].split()
if "/help" in words:
self.send_info(message, self.help_info, bot_handler)

View file

@ -31,7 +31,7 @@ class GiphyHandler(object):
global config_info
config_info = bot_handler.get_config_info('giphy')
def handle_message(self, message, bot_handler, state_handler):
def handle_message(self, message, bot_handler):
bot_response = get_bot_giphy_response(message, bot_handler)
bot_handler.send_reply(message, bot_response)

View file

@ -39,7 +39,7 @@ class GitHubHandler(object):
'<repository_owner>/<repository>/<issue_number>/<your_comment>'.
'''
def handle_message(self, message, bot_handler, state_handler):
def handle_message(self, message, bot_handler):
original_content = message['content']
original_sender = message['sender_email']

View file

@ -69,7 +69,7 @@ class GithubHandler(object):
repo = self.repo
return (owner, repo)
def handle_message(self, message, bot_handler, state_handler):
def handle_message(self, message, bot_handler):
# type: () -> None
# Send help message
if message['content'] == 'help':

View file

@ -47,7 +47,7 @@ class IssueHandler(object):
github_token = <oauth_token> (The personal access token for the GitHub bot)
'''
def handle_message(self, message, bot_handler, state_handler):
def handle_message(self, message, bot_handler):
original_content = message['content']
original_sender = message['sender_email']

View file

@ -72,7 +72,7 @@ class GoogleSearchHandler(object):
@mentioned-bot.
'''
def handle_message(self, message, bot_handler, state_handler):
def handle_message(self, message, bot_handler):
original_content = message['content']
result = get_google_result(original_content)
bot_handler.send_reply(message, result)

View file

@ -11,7 +11,7 @@ class HelloWorldHandler(object):
sophisticated, bots.
'''
def handle_message(self, message, bot_handler, state_handler):
def handle_message(self, message, bot_handler):
content = 'beep boop'
bot_handler.send_reply(message, content)

View file

@ -11,7 +11,7 @@ class HelpHandler(object):
your Zulip instance.
'''
def handle_message(self, message, bot_handler, state_handler):
def handle_message(self, message, bot_handler):
help_content = "Info on Zulip can be found here:\nhttps://github.com/zulip/zulip"
bot_handler.send_reply(message, help_content)

View file

@ -81,7 +81,7 @@ class HowdoiHandler(object):
return answer
def handle_message(self, message, bot_handler, state_handler):
def handle_message(self, message, bot_handler):
question = message['content'].strip()
if question.startswith('howdowe!'):

View file

@ -11,8 +11,8 @@ class IncrementorHandler(object):
is @-mentioned, this number will be incremented in the same message.
'''
def handle_message(self, message, bot_handler, state_handler):
with state_handler.state({'number': 0, 'message_id': None}) as state:
def handle_message(self, message, bot_handler):
with bot_handler.state_handler.state({'number': 0, 'message_id': None}) as state:
state['number'] += 1
if state['message_id'] is None:
result = bot_handler.send_reply(message, str(state['number']))

View file

@ -103,7 +103,7 @@ class JohnHandler(object):
)
self.chatterbot = create_chat_bot(True)
def handle_message(self, message, bot_handler, state_handler):
def handle_message(self, message, bot_handler):
original_content = message['content']
bot_response = str(self.chatterbot.get_response(original_content))
bot_handler.send_reply(message, bot_response)

View file

@ -274,14 +274,14 @@ class ticTacToeHandler(object):
message starts with @mention-bot.
'''
def handle_message(self, message, bot_handler, state_handler):
def handle_message(self, message, bot_handler):
command_list = message['content']
command = ""
for val in command_list:
command += val
original_sender = message['sender_email']
with state_handler.state({}) as mydict:
with bot_handler.state_handler.state({}) as mydict:
user_game = mydict.get(original_sender)
if (not user_game) and command == "new":
user_game = TicTacToeGame(copy.deepcopy(initial_board))

View file

@ -7,7 +7,7 @@ class VirtualFsHandler(object):
def usage(self):
return get_help()
def handle_message(self, message, bot_handler, state_handler):
def handle_message(self, message, bot_handler):
command = message['content']
if command == "":
command = "help"
@ -18,7 +18,7 @@ class VirtualFsHandler(object):
if isinstance(recipient, list): # If not a stream, then hash on list of emails
recipient = " ".join([x['email'] for x in recipient])
with state_handler.state({}) as state:
with bot_handler.state_handler.state({}) as state:
if recipient not in state:
state[recipient] = fs_new()
fs = state[recipient]

View file

@ -13,7 +13,7 @@ class WeatherHandler(object):
This plugin will give info about weather in a specified city
'''
def handle_message(self, message, bot_handler, state_handler):
def handle_message(self, message, bot_handler):
help_content = '''
This bot returns weather info for specified city.
You specify city in the following format:

View file

@ -32,7 +32,7 @@ class WikipediaHandler(object):
should preface searches with "@mention-bot".
'''
def handle_message(self, message, bot_handler, state_handler):
def handle_message(self, message, bot_handler):
bot_response = self.get_bot_wiki_response(message, bot_handler)
bot_handler.send_reply(message, bot_response)

View file

@ -32,7 +32,7 @@ class XkcdHandler(object):
`<comic_id>`, e.g `@mention-bot 1234`.
'''
def handle_message(self, message, bot_handler, state_handler):
def handle_message(self, message, bot_handler):
xkcd_bot_response = get_xkcd_bot_response(message)
bot_handler.send_reply(message, xkcd_bot_response)

View file

@ -54,7 +54,7 @@ class YodaSpeakHandler(object):
@mention-bot You will learn how to speak like me someday.
'''
def handle_message(self, message, bot_handler, state_handler):
def handle_message(self, message, bot_handler):
self.handle_input(message, bot_handler)
def send_to_yoda_api(self, sentence):

View file

@ -8,7 +8,7 @@ class YoutubeHandler(object):
This bot will return the first Youtube search result for the give query.
'''
def handle_message(self, message, bot_handler, state_handler):
def handle_message(self, message, bot_handler):
help_content = '''
To use the, Youtube Bot send `@mention-bot search terms`
Example:

View file

@ -51,6 +51,26 @@ class RateLimit(object):
logging.error(self.error_message)
sys.exit(1)
class StateHandler(object):
def __init__(self):
# type: () -> None
self.state_ = None # type: Any
def set_state(self, state):
# type: (Any) -> None
self.state_ = state
def get_state(self):
# type: () -> Any
return self.state_
@contextmanager
def state(self, default):
# type: (Any) -> Any
new_state = self.get_state() or default
yield new_state
self.set_state(new_state)
class ExternalBotHandler(object):
def __init__(self, client, root_dir):
# type: (Client, string) -> None
@ -59,6 +79,7 @@ class ExternalBotHandler(object):
self._rate_limit = RateLimit(20, 5)
self._client = client
self._root_dir = root_dir
self.state_handler = StateHandler()
try:
self.user_id = user_profile['user_id']
self.full_name = user_profile['full_name']
@ -122,26 +143,6 @@ class ExternalBotHandler(object):
raise PermissionError("Cannot open file \"{}\". Bots may only access "
"files in their local directory.".format(abs_filepath))
class StateHandler(object):
def __init__(self):
# type: () -> None
self.state_ = None # type: Any
def set_state(self, state):
# type: (Any) -> None
self.state_ = state
def get_state(self):
# type: () -> Any
return self.state_
@contextmanager
def state(self, default):
# type: (Any) -> Any
new_state = self.get_state() or default
yield new_state
self.set_state(new_state)
def extract_query_without_mention(message, client):
# type: (Dict[str, Any], ExternalBotHandler) -> str
"""
@ -185,8 +186,6 @@ def run_message_handler_for_bot(lib_module, quiet, config_file, bot_name):
if hasattr(message_handler, 'initialize'):
message_handler.initialize(bot_handler=restricted_client)
state_handler = StateHandler()
# Set default bot_details, then override from class, if provided
bot_details = {
'name': bot_name.capitalize(),
@ -220,8 +219,7 @@ def run_message_handler_for_bot(lib_module, quiet, config_file, bot_name):
if is_private_message or is_mentioned:
message_handler.handle_message(
message=message,
bot_handler=restricted_client,
state_handler=state_handler
bot_handler=restricted_client
)
signal.signal(signal.SIGINT, exit_gracefully)

View file

@ -104,7 +104,9 @@ class BotTestCase(TestCase):
if state_handler is None:
state_handler = StateHandler()
# Send message to the concerned bot
self.message_handler.handle_message(message, self.MockClass(None, None), state_handler)
mock_bot_handler = self.MockClass(None, None)
mock_bot_handler.state_handler = state_handler
self.message_handler.handle_message(message, mock_bot_handler)
# Check if the bot is sending a message via `send_message` function.
# Where response is a dictionary here.