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 8e33d9e48b
commit f9c5086658
25 changed files with 105 additions and 105 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
)

View File

@ -150,7 +150,7 @@ class MyBotHandler(object):
def usage(self):
return '''Your description of the bot'''
def handle_message(self, message, client, state_handler):
def handle_message(self, message, bot_handler, state_handler):
# add your code here
handler_class = MyBotHandler
@ -163,7 +163,7 @@ handler_class = MyBotHandler
* Every bot needs to implement the functions
* `usage(self)`
* `handle_message(self, message, client)`
* `handle_message(self, message, bot_handler)`
* These functions are documented in the [next section](#bot-api).
@ -212,7 +212,7 @@ def usage(self):
### handle_message
*handle_message(self, message, client)*
*handle_message(self, message, bot_handler)*
handles user message.
@ -222,7 +222,7 @@ handles user message.
* message - a dictionary describing a Zulip message
* client - used to interact with the server, e.g. to send a message
* bot_handler - used to interact with the server, e.g. to send a message
* state_handler - used to save states/information of the bot **beta**
* use `state_handler.set_state(state)` to set a state (any object)
@ -235,22 +235,22 @@ None.
#### Example implementation
```
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']
new_content = original_content.replace('@followup',
'from %s:' % (original_sender,))
client.send_message(dict(
bot_handler.send_message(dict(
type='stream',
to='followup',
subject=message['sender_email'],
content=new_content,
))
```
### client.send_message
### bot_handler.send_message
*client.send_message(message)*
*bot_handler.send_message(message)*
will send a message as the bot user. Generally, this is less
convenient than *send_reply*, but it offers additional flexibility
@ -263,7 +263,7 @@ about where the message is sent to.
### Example implementation
```
client.send_message(dict(
bot_handler.send_message(dict(
type='stream', # can be 'stream' or 'private'
to=stream_name, # either the stream name or user's email
subject=subject, # message subject
@ -271,9 +271,9 @@ client.send_message(dict(
))
```
### client.send_reply
### bot_handler.send_reply
*client.send_reply(message, response)*
*bot_handler.send_reply(message, response)*
will reply to the triggering message to the same place the original
message was sent to, with the content of the reply being *response*.
@ -284,9 +284,9 @@ message was sent to, with the content of the reply being *response*.
(provided by `handle_message`).
* response - Response message from the bot (string).
### client.update_message
### bot_handler.update_message
*client.update_message(message)*
*bot_handler.update_message(message)*
will edit the content of a previously sent message.
@ -299,7 +299,7 @@ will edit the content of a previously sent message.
From `/zulip/api/bots/incrementor/incrementor.py`:
```
client.update_message(dict(
bot_handler.update_message(dict(
message_id=self.message_id, # id of message to be updated
content=str(self.number), # string with which to update message with
))