From fcb4cf67215b9ccfc74b50668ce7e57da00b2ab8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20H=C3=B6nig?= Date: Sun, 11 Jun 2017 15:03:39 +0200 Subject: [PATCH] bots: Rename BotHandlerApi object `client` to `bot_handler`. --- bots/commute/commute.py | 24 ++++++++++++------------ bots/converter/converter.py | 8 ++++---- bots/define/define.py | 4 ++-- bots/encrypt/encrypt.py | 4 ++-- bots/followup/followup.py | 6 +++--- bots/foursquare/foursquare.py | 14 +++++++------- bots/giphy/giphy.py | 8 ++++---- bots/git_hub_comment/git_hub_comment.py | 18 +++++++++--------- bots/github_issues/github_issues.py | 6 +++--- bots/googlesearch/googlesearch.py | 10 +++++----- bots/helloworld/helloworld.py | 4 ++-- bots/help/help.py | 4 ++-- bots/howdoi/howdoi.py | 10 +++++----- bots/incrementor/incrementor.py | 6 +++--- bots/john/john.py | 4 ++-- bots/thesaurus/thesaurus.py | 4 ++-- bots/tictactoe/tictactoe.py | 4 ++-- bots/virtual_fs/virtual_fs.py | 4 ++-- bots/weather/weather.py | 4 ++-- bots/wikipedia/wikipedia.py | 8 ++++---- bots/xkcd/xkcd.py | 4 ++-- bots/yoda/yoda.py | 14 +++++++------- bots/youtube/youtube.py | 8 ++++---- bots_api/bot_lib.py | 2 +- 24 files changed, 91 insertions(+), 91 deletions(-) diff --git a/bots/commute/commute.py b/bots/commute/commute.py index b961ef8..5146bcd 100644 --- a/bots/commute/commute.py +++ b/bots/commute/commute.py @@ -89,8 +89,8 @@ class CommuteHandler(object): return config.get('Google.com', 'api_key') # determines if bot will respond as a private message/ stream message - def send_info(self, message, letter, client): - client.send_reply(message, letter) + def send_info(self, message, letter, bot_handler): + bot_handler.send_reply(message, letter) def calculate_seconds(self, time_str): times = time_str.split(',') @@ -114,7 +114,7 @@ class CommuteHandler(object): return # gets content for output and sends it to user - def get_send_content(self, rjson, params, message, client): + def get_send_content(self, rjson, params, message, bot_handler): try: # JSON list of output variables variable_list = rjson["rows"][0]["elements"][0] @@ -126,14 +126,14 @@ class CommuteHandler(object): if no_result: self.send_info(message, "Zero results\nIf stuck, try '@commute help'.", - client) + bot_handler) return elif not_found or invalid_request: raise IndexError except IndexError: self.send_info(message, "Invalid input, please see instructions." - "\nIf stuck, try '@commute help'.", client) + "\nIf stuck, try '@commute help'.", bot_handler) return # origin and destination strings @@ -165,7 +165,7 @@ class CommuteHandler(object): output += '\n' + duration # bot sends commute information to user - self.send_info(message, output, client) + self.send_info(message, output, bot_handler) # creates parameters for HTTP request def parse_pair(self, content_list): @@ -180,7 +180,7 @@ class CommuteHandler(object): result[key] = value return result - def receive_response(self, params, message, client): + def receive_response(self, params, message, bot_handler): def validate_requests(request): if request.status_code == 200: return request.json() @@ -189,30 +189,30 @@ class CommuteHandler(object): "Something went wrong. Please try again." + " Error: {error_num}.\n{error_text}" .format(error_num=request.status_code, - error_text=request.text), client) + error_text=request.text), bot_handler) return r = requests.get('https://maps.googleapis.com/maps/api/' + 'distancematrix/json', params=params) result = validate_requests(r) return result - def handle_message(self, message, client, state_handler): + def handle_message(self, message, bot_handler, state_handler): original_content = message['content'] query = original_content.split() if "help" in query: - self.send_info(message, self.help_info, client) + self.send_info(message, self.help_info, bot_handler) return params = self.parse_pair(query) params['key'] = self.api_key self.add_time_to_params(params) - rjson = self.receive_response(params, message, client) + rjson = self.receive_response(params, message, bot_handler) if not rjson: return - self.get_send_content(rjson, params, message, client) + self.get_send_content(rjson, params, message, bot_handler) handler_class = CommuteHandler handler = CommuteHandler() diff --git a/bots/converter/converter.py b/bots/converter/converter.py index 9658d75..a84de5a 100644 --- a/bots/converter/converter.py +++ b/bots/converter/converter.py @@ -47,11 +47,11 @@ class ConverterHandler(object): all supported units. ''' - def handle_message(self, message, client, state_handler): - bot_response = get_bot_converter_response(message, client) - client.send_reply(message, bot_response) + def handle_message(self, message, bot_handler, state_handler): + bot_response = get_bot_converter_response(message, bot_handler) + bot_handler.send_reply(message, bot_response) -def get_bot_converter_response(message, client): +def get_bot_converter_response(message, bot_handler): content = message['content'] words = content.lower().split() diff --git a/bots/define/define.py b/bots/define/define.py index f2947ed..3c7122e 100644 --- a/bots/define/define.py +++ b/bots/define/define.py @@ -21,11 +21,11 @@ class DefineHandler(object): messages with @mention-bot. ''' - def handle_message(self, message, client, state_handler): + def handle_message(self, message, bot_handler, state_handler): original_content = message['content'].strip() bot_response = self.get_bot_define_response(original_content) - client.send_reply(message, bot_response) + bot_handler.send_reply(message, bot_response) def get_bot_define_response(self, original_content): split_content = original_content.split(' ') diff --git a/bots/encrypt/encrypt.py b/bots/encrypt/encrypt.py index a7e5fc2..13dad29 100755 --- a/bots/encrypt/encrypt.py +++ b/bots/encrypt/encrypt.py @@ -28,9 +28,9 @@ class EncryptHandler(object): Feeding encrypted messages into the bot decrypts them. ''' - def handle_message(self, message, client, state_handler): + def handle_message(self, message, bot_handler, state_handler): bot_response = self.get_bot_encrypt_response(message) - client.send_reply(message, bot_response) + bot_handler.send_reply(message, bot_response) def get_bot_encrypt_response(self, message): original_content = message['content'] diff --git a/bots/followup/followup.py b/bots/followup/followup.py index 3243491..dcf76c3 100644 --- a/bots/followup/followup.py +++ b/bots/followup/followup.py @@ -22,13 +22,13 @@ class FollowupHandler(object): called "followup" that your API user can send to. ''' - def handle_message(self, message, client, state_handler): + def handle_message(self, message, bot_handler, state_handler): if message['content'] == '': bot_response = "Please specify the message you want to send to followup stream after @mention-bot" - client.send_reply(message, bot_response) + bot_handler.send_reply(message, bot_response) else: bot_response = self.get_bot_followup_response(message) - client.send_message(dict( + bot_handler.send_message(dict( type='stream', to='followup', subject=message['sender_email'], diff --git a/bots/foursquare/foursquare.py b/bots/foursquare/foursquare.py index 2fa11aa..e43d763 100644 --- a/bots/foursquare/foursquare.py +++ b/bots/foursquare/foursquare.py @@ -59,13 +59,13 @@ Example Inputs: return '\n'.join(format_venue(venue) for venue in venues) - def send_info(self, message, letter, client): - client.send_reply(message, letter) + def send_info(self, message, letter, bot_handler): + bot_handler.send_reply(message, letter) - def handle_message(self, message, client, state_handler): + def handle_message(self, message, bot_handler, state_handler): words = message['content'].split() if "/help" in words: - self.send_info(message, self.help_info, client) + self.send_info(message, self.help_info, bot_handler) return # These are required inputs for the HTTP request. @@ -96,19 +96,19 @@ Example Inputs: else: self.send_info(message, "Invalid Request\nIf stuck, try '@mention-bot help'.", - client) + bot_handler) return if received_json['meta']['code'] == 200: response_msg = ('Food nearby ' + params['near'] + ' coming right up:\n' + self.format_json(received_json['response']['venues'])) - self.send_info(message, response_msg, client) + self.send_info(message, response_msg, bot_handler) return self.send_info(message, "Invalid Request\nIf stuck, try '@mention-bot help'.", - client) + bot_handler) return handler_class = FoursquareHandler diff --git a/bots/giphy/giphy.py b/bots/giphy/giphy.py index db93dc1..159a3e4 100644 --- a/bots/giphy/giphy.py +++ b/bots/giphy/giphy.py @@ -33,9 +33,9 @@ class GiphyHandler(object): The bot responds also to private messages. ''' - def handle_message(self, message, client, state_handler): - bot_response = get_bot_giphy_response(message, client) - client.send_reply(message, bot_response) + def handle_message(self, message, bot_handler, state_handler): + bot_response = get_bot_giphy_response(message, bot_handler) + bot_handler.send_reply(message, bot_response) class GiphyNoResultException(Exception): @@ -73,7 +73,7 @@ def get_url_gif_giphy(keyword, api_key): return gif_url -def get_bot_giphy_response(message, client): +def get_bot_giphy_response(message, bot_handler): # Each exception has a specific reply should "gif_url" return a number. # The bot will post the appropriate message for the error. keyword = message['content'] diff --git a/bots/git_hub_comment/git_hub_comment.py b/bots/git_hub_comment/git_hub_comment.py index cbef815..7437320 100644 --- a/bots/git_hub_comment/git_hub_comment.py +++ b/bots/git_hub_comment/git_hub_comment.py @@ -39,11 +39,11 @@ class GitHubHandler(object): '///'. ''' - def handle_message(self, message, client, state_handler): + def handle_message(self, message, bot_handler, state_handler): original_content = message['content'] original_sender = message['sender_email'] - handle_input(client, original_content, original_sender) + handle_input(bot_handler, original_content, original_sender) handler_class = GitHubHandler @@ -77,7 +77,7 @@ def get_values_message(original_content): raise InputError -def handle_input(client, original_content, original_sender): +def handle_input(bot_handler, original_content, original_sender): try: params = get_values_message(original_content) @@ -89,7 +89,7 @@ def handle_input(client, original_content, original_sender): reply_message = "You commented on issue number " + params['issue'] + " under " + \ params['repo_owner'] + "'s repository " + params['repo'] + "!" - send_message(client, reply_message, original_sender) + send_message(bot_handler, reply_message, original_sender) elif status_code == 404: # this error could be from an error with the OAuth token @@ -98,7 +98,7 @@ def handle_input(client, original_content, original_sender): params['repo_owner'] + "'s repository " + params['repo'] + \ ". Do you have the right OAuth token?" - send_message(client, reply_message, original_sender) + send_message(bot_handler, reply_message, original_sender) else: # sending info to github did not work @@ -108,18 +108,18 @@ def handle_input(client, original_content, original_sender): params['repo_owner'] + "'s repository " + params['repo'] + \ ". Did you enter the information in the correct format?" - send_message(client, reply_message, original_sender) + send_message(bot_handler, reply_message, original_sender) except InputError: message = "It doesn't look like the information was entered in the correct format." \ " Did you input it like this? " \ "'/////'." - send_message(client, message, original_sender) + send_message(bot_handler, message, original_sender) logging.error('there was an error with the information you entered') -def send_message(client, message, original_sender): +def send_message(bot_handler, message, original_sender): # function for sending a message - client.send_message(dict( + bot_handler.send_message(dict( type='private', to=original_sender, content=message, diff --git a/bots/github_issues/github_issues.py b/bots/github_issues/github_issues.py index 8d415d0..3f4368c 100644 --- a/bots/github_issues/github_issues.py +++ b/bots/github_issues/github_issues.py @@ -47,7 +47,7 @@ class IssueHandler(object): github_token = (The personal access token for the GitHub bot) ''' - def handle_message(self, message, client, state_handler): + def handle_message(self, message, bot_handler, state_handler): original_content = message['content'] original_sender = message['sender_email'] @@ -86,7 +86,7 @@ class IssueHandler(object): if r.ok: # sends the message onto the 'issues' stream so it can be seen by zulip users - client.send_message(dict( + bot_handler.send_message(dict( type='stream', to='issues', subject=message['sender_email'], @@ -96,7 +96,7 @@ class IssueHandler(object): return # This means that the issue has not been sent # sends the message onto the 'issues' stream so it can be seen by zulip users - client.send_message(dict( + bot_handler.send_message(dict( type='stream', to='issues', subject=message['sender_email'], diff --git a/bots/googlesearch/googlesearch.py b/bots/googlesearch/googlesearch.py index 73aec56..42492bb 100644 --- a/bots/googlesearch/googlesearch.py +++ b/bots/googlesearch/googlesearch.py @@ -1,7 +1,7 @@ # See readme.md for instructions on running this code. from __future__ import print_function import logging -import http.client +import http.bot_handler from six.moves.urllib.request import urlopen # Uses the Google search engine bindings @@ -26,7 +26,7 @@ def get_google_result(search_keywords): try: urls = search(search_keywords, stop=20) urlopen('http://216.58.192.142', timeout=1) - except http.client.RemoteDisconnected as er: + except http.bot_handler.RemoteDisconnected as er: logging.exception(er) return 'Error: No internet connection. {}.'.format(er) except Exception as e: @@ -72,10 +72,10 @@ class GoogleSearchHandler(object): @mentioned-bot. ''' - def handle_message(self, message, client, state_handler): + def handle_message(self, message, bot_handler, state_handler): original_content = message['content'] result = get_google_result(original_content) - client.send_reply(message, result) + bot_handler.send_reply(message, result) handler_class = GoogleSearchHandler @@ -85,7 +85,7 @@ def test(): urlopen('http://216.58.192.142', timeout=1) print('Success') return True - except http.client.RemoteDisconnected as e: + except http.bot_handler.RemoteDisconnected as e: print('Error: {}'.format(e)) return False diff --git a/bots/helloworld/helloworld.py b/bots/helloworld/helloworld.py index 95ca03f..ca99ae4 100644 --- a/bots/helloworld/helloworld.py +++ b/bots/helloworld/helloworld.py @@ -11,8 +11,8 @@ class HelloWorldHandler(object): sophisticated, bots. ''' - def handle_message(self, message, client, state_handler): + def handle_message(self, message, bot_handler, state_handler): content = 'beep boop' - client.send_reply(message, content) + bot_handler.send_reply(message, content) handler_class = HelloWorldHandler diff --git a/bots/help/help.py b/bots/help/help.py index 1e07f71..d51a428 100644 --- a/bots/help/help.py +++ b/bots/help/help.py @@ -11,8 +11,8 @@ class HelpHandler(object): your Zulip instance. ''' - def handle_message(self, message, client, state_handler): + def handle_message(self, message, bot_handler, state_handler): help_content = "Info on Zulip can be found here:\nhttps://github.com/zulip/zulip" - client.send_reply(message, help_content) + bot_handler.send_reply(message, help_content) handler_class = HelpHandler diff --git a/bots/howdoi/howdoi.py b/bots/howdoi/howdoi.py index 2d7921f..6acf331 100644 --- a/bots/howdoi/howdoi.py +++ b/bots/howdoi/howdoi.py @@ -81,11 +81,11 @@ class HowdoiHandler(object): return answer - def handle_message(self, message, client, state_handler): + def handle_message(self, message, bot_handler, state_handler): question = message['content'].strip() if question.startswith('howdowe!'): - client.send_message(dict( + bot_handler.send_message(dict( type='stream', to=message['display_recipient'], subject=message['subject'], @@ -93,14 +93,14 @@ class HowdoiHandler(object): )) elif question.startswith('howdoi!'): - client.send_message(dict( + bot_handler.send_message(dict( type='private', to=message['sender_email'], content=self.get_answer('howdoi!', question) )) elif question.startswith('howdowe'): - client.send_message(dict( + bot_handler.send_message(dict( type='stream', to=message['display_recipient'], subject=message['subject'], @@ -108,7 +108,7 @@ class HowdoiHandler(object): )) elif question.startswith('howdoi'): - client.send_message(dict( + bot_handler.send_message(dict( type='private', to=message['sender_email'], content=self.get_answer('howdoi', question) diff --git a/bots/incrementor/incrementor.py b/bots/incrementor/incrementor.py index b5fe1cd..c498416 100644 --- a/bots/incrementor/incrementor.py +++ b/bots/incrementor/incrementor.py @@ -15,13 +15,13 @@ class IncrementorHandler(object): is @-mentioned, this number will be incremented in the same message. ''' - def handle_message(self, message, client, state_handler): + def handle_message(self, message, bot_handler, state_handler): self.number += 1 if self.message_id is None: - result = client.send_reply(message, str(self.number)) + result = bot_handler.send_reply(message, str(self.number)) self.message_id = result['id'] else: - client.update_message(dict( + bot_handler.update_message(dict( message_id=self.message_id, content=str(self.number), )) diff --git a/bots/john/john.py b/bots/john/john.py index 341b859..29cf2e3 100644 --- a/bots/john/john.py +++ b/bots/john/john.py @@ -115,9 +115,9 @@ class JohnHandler(object): mantain a conversation, joke and give useful information. ''' - def handle_message(self, message, client, state_handler): + def handle_message(self, message, bot_handler, state_handler): original_content = message['content'] bot_response = str(bota.get_response(original_content)) - client.send_reply(message, bot_response) + bot_handler.send_reply(message, bot_response) handler_class = JohnHandler diff --git a/bots/thesaurus/thesaurus.py b/bots/thesaurus/thesaurus.py index db25e03..0638566 100644 --- a/bots/thesaurus/thesaurus.py +++ b/bots/thesaurus/thesaurus.py @@ -63,9 +63,9 @@ class ThesaurusHandler(object): preface messages with @mention-bot synonym or @mention-bot antonym. ''' - def handle_message(self, message, client, state_handler): + def handle_message(self, message, bot_handler, state_handler): original_content = message['content'].strip() new_content = get_thesaurus_result(original_content) - client.send_reply(message, new_content) + bot_handler.send_reply(message, new_content) handler_class = ThesaurusHandler diff --git a/bots/tictactoe/tictactoe.py b/bots/tictactoe/tictactoe.py index 0e6b619..4ec1a69 100644 --- a/bots/tictactoe/tictactoe.py +++ b/bots/tictactoe/tictactoe.py @@ -274,7 +274,7 @@ class ticTacToeHandler(object): message starts with @mention-bot. ''' - def handle_message(self, message, client, state_handler): + def handle_message(self, message, bot_handler, state_handler): command_list = message['content'] command = "" for val in command_list: @@ -313,7 +313,7 @@ class ticTacToeHandler(object): state_handler.set_state(mydict) - client.send_message(dict( + bot_handler.send_message(dict( type = 'private', to = original_sender, subject = message['sender_email'], diff --git a/bots/virtual_fs/virtual_fs.py b/bots/virtual_fs/virtual_fs.py index 00576df..8cc5d76 100644 --- a/bots/virtual_fs/virtual_fs.py +++ b/bots/virtual_fs/virtual_fs.py @@ -7,7 +7,7 @@ class VirtualFsHandler(object): def usage(self): return get_help() - def handle_message(self, message, client, state_handler): + def handle_message(self, message, bot_handler, state_handler): command = message['content'] if command == "": command = "help" @@ -32,7 +32,7 @@ class VirtualFsHandler(object): state[recipient] = fs state_handler.set_state(state) - client.send_reply(message, msg) + bot_handler.send_reply(message, msg) def get_help(): diff --git a/bots/weather/weather.py b/bots/weather/weather.py index 935c744..6cddb0f 100644 --- a/bots/weather/weather.py +++ b/bots/weather/weather.py @@ -23,7 +23,7 @@ class WeatherHandler(object): This plugin will give info about weather in a specified city ''' - def handle_message(self, message, client, state_handler): + def handle_message(self, message, bot_handler, state_handler): help_content = ''' This bot returns weather info for specified city. You specify city in the following format: @@ -44,7 +44,7 @@ class WeatherHandler(object): else: response = format_response(r.text, message['content'], self.response_pattern) - client.send_reply(message, response) + bot_handler.send_reply(message, response) def format_response(text, city, response_pattern): diff --git a/bots/wikipedia/wikipedia.py b/bots/wikipedia/wikipedia.py index 5c94f6f..9659273 100644 --- a/bots/wikipedia/wikipedia.py +++ b/bots/wikipedia/wikipedia.py @@ -26,11 +26,11 @@ class WikipediaHandler(object): should preface searches with "@mention-bot". ''' - def handle_message(self, message, client, state_handler): - bot_response = self.get_bot_wiki_response(message, client) - client.send_reply(message, bot_response) + def handle_message(self, message, bot_handler, state_handler): + bot_response = self.get_bot_wiki_response(message, bot_handler) + bot_handler.send_reply(message, bot_response) - def get_bot_wiki_response(self, message, client): + def get_bot_wiki_response(self, message, bot_handler): help_text = 'Please enter your message after @mention-bot' query = message['content'] if query == '': diff --git a/bots/xkcd/xkcd.py b/bots/xkcd/xkcd.py index 6a4dcce..db5d527 100644 --- a/bots/xkcd/xkcd.py +++ b/bots/xkcd/xkcd.py @@ -27,9 +27,9 @@ class XkcdHandler(object): ``, e.g `@mention-bot 1234`. ''' - def handle_message(self, message, client, state_handler): + def handle_message(self, message, bot_handler, state_handler): xkcd_bot_response = get_xkcd_bot_response(message) - client.send_reply(message, xkcd_bot_response) + bot_handler.send_reply(message, xkcd_bot_response) class XkcdBotCommand(object): LATEST = 0 diff --git a/bots/yoda/yoda.py b/bots/yoda/yoda.py index a87049f..6805326 100644 --- a/bots/yoda/yoda.py +++ b/bots/yoda/yoda.py @@ -49,8 +49,8 @@ class YodaSpeakHandler(object): @mention-bot You will learn how to speak like me someday. ''' - def handle_message(self, message, client, state_handler): - handle_input(message, client) + def handle_message(self, message, bot_handler, state_handler): + handle_input(message, bot_handler) handler_class = YodaSpeakHandler @@ -86,11 +86,11 @@ def format_input(original_content): return sentence -def handle_input(message, client): +def handle_input(message, bot_handler): original_content = message['content'] if is_help(original_content): - client.send_reply(message, HELP_MESSAGE) + bot_handler.send_reply(message, HELP_MESSAGE) else: sentence = format_input(original_content) @@ -106,7 +106,7 @@ def handle_input(message, client): '`readme.md` file?' logging.error(reply_message) - client.send_reply(message, reply_message) + bot_handler.send_reply(message, reply_message) def get_api_key(): @@ -117,9 +117,9 @@ def get_api_key(): return api_key -def send_message(client, message, stream, subject): +def send_message(bot_handler, message, stream, subject): # function for sending a message - client.send_message(dict( + bot_handler.send_message(dict( type='stream', to=stream, subject=subject, diff --git a/bots/youtube/youtube.py b/bots/youtube/youtube.py index 2b03c9f..69dfcb6 100644 --- a/bots/youtube/youtube.py +++ b/bots/youtube/youtube.py @@ -8,14 +8,14 @@ class YoutubeHandler(object): This bot will return the first Youtube search result for the give query. ''' - def handle_message(self, message, client, state_handler): + def handle_message(self, message, bot_handler, state_handler): help_content = ''' To use the, Youtube Bot send `@mention-bot search terms` Example: @mention-bot funny cats '''.strip() if message['content'] == '': - client.send_reply(message, help_content) + bot_handler.send_reply(message, help_content) else: text_to_search = message['content'] url = "https://www.youtube.com/results?search_query=" + text_to_search @@ -24,8 +24,8 @@ class YoutubeHandler(object): video_id = soup.find(attrs={'class': 'yt-uix-tile-link'}) try: link = 'https://www.youtube.com' + video_id['href'] - client.send_reply(message, link) + bot_handler.send_reply(message, link) except TypeError: - client.send_reply(message, 'No video found for specified search terms') + bot_handler.send_reply(message, 'No video found for specified search terms') handler_class = YoutubeHandler diff --git a/bots_api/bot_lib.py b/bots_api/bot_lib.py index 3dd1bb2..c3fcb3c 100644 --- a/bots_api/bot_lib.py +++ b/bots_api/bot_lib.py @@ -167,7 +167,7 @@ def run_message_handler_for_bot(lib_module, quiet, config_file): if is_private_message or is_mentioned: message_handler.handle_message( message=message, - client=restricted_client, + bot_handler=restricted_client, state_handler=state_handler )