bots: Rename BotHandlerApi object client to bot_handler.

This commit is contained in:
Robert Hönig 2017-06-11 15:03:39 +02:00 committed by Tim Abbott
parent c193443541
commit fcb4cf6721
24 changed files with 91 additions and 91 deletions

View file

@ -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()

View file

@ -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()

View file

@ -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(' ')

View file

@ -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']

View file

@ -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'],

View file

@ -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

View file

@ -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']

View file

@ -39,11 +39,11 @@ class GitHubHandler(object):
'<repository_owner>/<repository>/<issue_number>/<your_comment>'.
'''
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? " \
"'/<username>/<repository_owner>/<repository>/<issue_number>/<your_comment>'."
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,

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, 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'],

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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)

View file

@ -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),
))

View file

@ -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

View file

@ -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

View file

@ -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'],

View file

@ -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():

View file

@ -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):

View file

@ -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 == '':

View file

@ -27,9 +27,9 @@ class XkcdHandler(object):
`<comic_id>`, 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

View file

@ -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,

View file

@ -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

View file

@ -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
)