zulip_bots: Make state_handler property of bot_handler.
This commit is contained in:
parent
45c38d0dcf
commit
eb6982e670
|
@ -180,7 +180,7 @@ class CommuteHandler(object):
|
||||||
result = validate_requests(r)
|
result = validate_requests(r)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def handle_message(self, message, bot_handler, state_handler):
|
def handle_message(self, message, bot_handler):
|
||||||
original_content = message['content']
|
original_content = message['content']
|
||||||
query = original_content.split()
|
query = original_content.split()
|
||||||
|
|
||||||
|
|
|
@ -46,7 +46,7 @@ class ConverterHandler(object):
|
||||||
all supported units.
|
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_response = get_bot_converter_response(message, bot_handler)
|
||||||
bot_handler.send_reply(message, bot_response)
|
bot_handler.send_reply(message, bot_response)
|
||||||
|
|
||||||
|
|
|
@ -23,7 +23,7 @@ class DefineHandler(object):
|
||||||
messages with @mention-bot.
|
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()
|
original_content = message['content'].strip()
|
||||||
bot_response = self.get_bot_define_response(original_content)
|
bot_response = self.get_bot_define_response(original_content)
|
||||||
|
|
||||||
|
|
|
@ -28,7 +28,7 @@ class EncryptHandler(object):
|
||||||
Feeding encrypted messages into the bot decrypts them.
|
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_response = self.get_bot_encrypt_response(message)
|
||||||
bot_handler.send_reply(message, bot_response)
|
bot_handler.send_reply(message, bot_response)
|
||||||
|
|
||||||
|
|
|
@ -22,7 +22,7 @@ class FollowupHandler(object):
|
||||||
called "followup" that your API user can send to.
|
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'] == '':
|
if message['content'] == '':
|
||||||
bot_response = "Please specify the message you want to send to followup stream after @mention-bot"
|
bot_response = "Please specify the message you want to send to followup stream after @mention-bot"
|
||||||
bot_handler.send_reply(message, bot_response)
|
bot_handler.send_reply(message, bot_response)
|
||||||
|
|
|
@ -47,7 +47,7 @@ Example Inputs:
|
||||||
def send_info(self, message, letter, bot_handler):
|
def send_info(self, message, letter, bot_handler):
|
||||||
bot_handler.send_reply(message, letter)
|
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()
|
words = message['content'].split()
|
||||||
if "/help" in words:
|
if "/help" in words:
|
||||||
self.send_info(message, self.help_info, bot_handler)
|
self.send_info(message, self.help_info, bot_handler)
|
||||||
|
|
|
@ -31,7 +31,7 @@ class GiphyHandler(object):
|
||||||
global config_info
|
global config_info
|
||||||
config_info = bot_handler.get_config_info('giphy')
|
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_response = get_bot_giphy_response(message, bot_handler)
|
||||||
bot_handler.send_reply(message, bot_response)
|
bot_handler.send_reply(message, bot_response)
|
||||||
|
|
||||||
|
|
|
@ -39,7 +39,7 @@ class GitHubHandler(object):
|
||||||
'<repository_owner>/<repository>/<issue_number>/<your_comment>'.
|
'<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_content = message['content']
|
||||||
original_sender = message['sender_email']
|
original_sender = message['sender_email']
|
||||||
|
|
||||||
|
|
|
@ -69,7 +69,7 @@ class GithubHandler(object):
|
||||||
repo = self.repo
|
repo = self.repo
|
||||||
return (owner, repo)
|
return (owner, repo)
|
||||||
|
|
||||||
def handle_message(self, message, bot_handler, state_handler):
|
def handle_message(self, message, bot_handler):
|
||||||
# type: () -> None
|
# type: () -> None
|
||||||
# Send help message
|
# Send help message
|
||||||
if message['content'] == 'help':
|
if message['content'] == 'help':
|
||||||
|
|
|
@ -47,7 +47,7 @@ class IssueHandler(object):
|
||||||
github_token = <oauth_token> (The personal access token for the GitHub bot)
|
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_content = message['content']
|
||||||
original_sender = message['sender_email']
|
original_sender = message['sender_email']
|
||||||
|
|
|
@ -72,7 +72,7 @@ class GoogleSearchHandler(object):
|
||||||
@mentioned-bot.
|
@mentioned-bot.
|
||||||
'''
|
'''
|
||||||
|
|
||||||
def handle_message(self, message, bot_handler, state_handler):
|
def handle_message(self, message, bot_handler):
|
||||||
original_content = message['content']
|
original_content = message['content']
|
||||||
result = get_google_result(original_content)
|
result = get_google_result(original_content)
|
||||||
bot_handler.send_reply(message, result)
|
bot_handler.send_reply(message, result)
|
||||||
|
|
|
@ -11,7 +11,7 @@ class HelloWorldHandler(object):
|
||||||
sophisticated, bots.
|
sophisticated, bots.
|
||||||
'''
|
'''
|
||||||
|
|
||||||
def handle_message(self, message, bot_handler, state_handler):
|
def handle_message(self, message, bot_handler):
|
||||||
content = 'beep boop'
|
content = 'beep boop'
|
||||||
bot_handler.send_reply(message, content)
|
bot_handler.send_reply(message, content)
|
||||||
|
|
||||||
|
|
|
@ -11,7 +11,7 @@ class HelpHandler(object):
|
||||||
your Zulip instance.
|
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"
|
help_content = "Info on Zulip can be found here:\nhttps://github.com/zulip/zulip"
|
||||||
bot_handler.send_reply(message, help_content)
|
bot_handler.send_reply(message, help_content)
|
||||||
|
|
||||||
|
|
|
@ -81,7 +81,7 @@ class HowdoiHandler(object):
|
||||||
|
|
||||||
return answer
|
return answer
|
||||||
|
|
||||||
def handle_message(self, message, bot_handler, state_handler):
|
def handle_message(self, message, bot_handler):
|
||||||
question = message['content'].strip()
|
question = message['content'].strip()
|
||||||
|
|
||||||
if question.startswith('howdowe!'):
|
if question.startswith('howdowe!'):
|
||||||
|
|
|
@ -11,8 +11,8 @@ class IncrementorHandler(object):
|
||||||
is @-mentioned, this number will be incremented in the same message.
|
is @-mentioned, this number will be incremented in the same message.
|
||||||
'''
|
'''
|
||||||
|
|
||||||
def handle_message(self, message, bot_handler, state_handler):
|
def handle_message(self, message, bot_handler):
|
||||||
with state_handler.state({'number': 0, 'message_id': None}) as state:
|
with bot_handler.state_handler.state({'number': 0, 'message_id': None}) as state:
|
||||||
state['number'] += 1
|
state['number'] += 1
|
||||||
if state['message_id'] is None:
|
if state['message_id'] is None:
|
||||||
result = bot_handler.send_reply(message, str(state['number']))
|
result = bot_handler.send_reply(message, str(state['number']))
|
||||||
|
|
|
@ -103,7 +103,7 @@ class JohnHandler(object):
|
||||||
)
|
)
|
||||||
self.chatterbot = create_chat_bot(True)
|
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']
|
original_content = message['content']
|
||||||
bot_response = str(self.chatterbot.get_response(original_content))
|
bot_response = str(self.chatterbot.get_response(original_content))
|
||||||
bot_handler.send_reply(message, bot_response)
|
bot_handler.send_reply(message, bot_response)
|
||||||
|
|
|
@ -274,14 +274,14 @@ class ticTacToeHandler(object):
|
||||||
message starts with @mention-bot.
|
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_list = message['content']
|
||||||
command = ""
|
command = ""
|
||||||
for val in command_list:
|
for val in command_list:
|
||||||
command += val
|
command += val
|
||||||
original_sender = message['sender_email']
|
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)
|
user_game = mydict.get(original_sender)
|
||||||
if (not user_game) and command == "new":
|
if (not user_game) and command == "new":
|
||||||
user_game = TicTacToeGame(copy.deepcopy(initial_board))
|
user_game = TicTacToeGame(copy.deepcopy(initial_board))
|
||||||
|
|
|
@ -7,7 +7,7 @@ class VirtualFsHandler(object):
|
||||||
def usage(self):
|
def usage(self):
|
||||||
return get_help()
|
return get_help()
|
||||||
|
|
||||||
def handle_message(self, message, bot_handler, state_handler):
|
def handle_message(self, message, bot_handler):
|
||||||
command = message['content']
|
command = message['content']
|
||||||
if command == "":
|
if command == "":
|
||||||
command = "help"
|
command = "help"
|
||||||
|
@ -18,7 +18,7 @@ class VirtualFsHandler(object):
|
||||||
if isinstance(recipient, list): # If not a stream, then hash on list of emails
|
if isinstance(recipient, list): # If not a stream, then hash on list of emails
|
||||||
recipient = " ".join([x['email'] for x in recipient])
|
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:
|
if recipient not in state:
|
||||||
state[recipient] = fs_new()
|
state[recipient] = fs_new()
|
||||||
fs = state[recipient]
|
fs = state[recipient]
|
||||||
|
|
|
@ -13,7 +13,7 @@ class WeatherHandler(object):
|
||||||
This plugin will give info about weather in a specified city
|
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 = '''
|
help_content = '''
|
||||||
This bot returns weather info for specified city.
|
This bot returns weather info for specified city.
|
||||||
You specify city in the following format:
|
You specify city in the following format:
|
||||||
|
|
|
@ -32,7 +32,7 @@ class WikipediaHandler(object):
|
||||||
should preface searches with "@mention-bot".
|
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_response = self.get_bot_wiki_response(message, bot_handler)
|
||||||
bot_handler.send_reply(message, bot_response)
|
bot_handler.send_reply(message, bot_response)
|
||||||
|
|
||||||
|
|
|
@ -32,7 +32,7 @@ class XkcdHandler(object):
|
||||||
`<comic_id>`, e.g `@mention-bot 1234`.
|
`<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)
|
xkcd_bot_response = get_xkcd_bot_response(message)
|
||||||
bot_handler.send_reply(message, xkcd_bot_response)
|
bot_handler.send_reply(message, xkcd_bot_response)
|
||||||
|
|
||||||
|
|
|
@ -54,7 +54,7 @@ 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, state_handler):
|
def handle_message(self, message, bot_handler):
|
||||||
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):
|
||||||
|
|
|
@ -8,7 +8,7 @@ class YoutubeHandler(object):
|
||||||
This bot will return the first Youtube search result for the give query.
|
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 = '''
|
help_content = '''
|
||||||
To use the, Youtube Bot send `@mention-bot search terms`
|
To use the, Youtube Bot send `@mention-bot search terms`
|
||||||
Example:
|
Example:
|
||||||
|
|
|
@ -51,6 +51,26 @@ class RateLimit(object):
|
||||||
logging.error(self.error_message)
|
logging.error(self.error_message)
|
||||||
sys.exit(1)
|
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):
|
class ExternalBotHandler(object):
|
||||||
def __init__(self, client, root_dir):
|
def __init__(self, client, root_dir):
|
||||||
# type: (Client, string) -> None
|
# type: (Client, string) -> None
|
||||||
|
@ -59,6 +79,7 @@ class ExternalBotHandler(object):
|
||||||
self._rate_limit = RateLimit(20, 5)
|
self._rate_limit = RateLimit(20, 5)
|
||||||
self._client = client
|
self._client = client
|
||||||
self._root_dir = root_dir
|
self._root_dir = root_dir
|
||||||
|
self.state_handler = StateHandler()
|
||||||
try:
|
try:
|
||||||
self.user_id = user_profile['user_id']
|
self.user_id = user_profile['user_id']
|
||||||
self.full_name = user_profile['full_name']
|
self.full_name = user_profile['full_name']
|
||||||
|
@ -122,26 +143,6 @@ class ExternalBotHandler(object):
|
||||||
raise PermissionError("Cannot open file \"{}\". Bots may only access "
|
raise PermissionError("Cannot open file \"{}\". Bots may only access "
|
||||||
"files in their local directory.".format(abs_filepath))
|
"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):
|
def extract_query_without_mention(message, client):
|
||||||
# type: (Dict[str, Any], ExternalBotHandler) -> str
|
# 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'):
|
if hasattr(message_handler, 'initialize'):
|
||||||
message_handler.initialize(bot_handler=restricted_client)
|
message_handler.initialize(bot_handler=restricted_client)
|
||||||
|
|
||||||
state_handler = StateHandler()
|
|
||||||
|
|
||||||
# Set default bot_details, then override from class, if provided
|
# Set default bot_details, then override from class, if provided
|
||||||
bot_details = {
|
bot_details = {
|
||||||
'name': bot_name.capitalize(),
|
'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:
|
if is_private_message or is_mentioned:
|
||||||
message_handler.handle_message(
|
message_handler.handle_message(
|
||||||
message=message,
|
message=message,
|
||||||
bot_handler=restricted_client,
|
bot_handler=restricted_client
|
||||||
state_handler=state_handler
|
|
||||||
)
|
)
|
||||||
|
|
||||||
signal.signal(signal.SIGINT, exit_gracefully)
|
signal.signal(signal.SIGINT, exit_gracefully)
|
||||||
|
|
|
@ -104,7 +104,9 @@ class BotTestCase(TestCase):
|
||||||
if state_handler is None:
|
if state_handler is None:
|
||||||
state_handler = StateHandler()
|
state_handler = StateHandler()
|
||||||
# Send message to the concerned bot
|
# 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.
|
# Check if the bot is sending a message via `send_message` function.
|
||||||
# Where response is a dictionary here.
|
# Where response is a dictionary here.
|
||||||
|
|
Loading…
Reference in a new issue