contrib_bots: Expose some information about user profile

- Expose some information about user profile in `RestrictedClient`
class, like `full_name` and `email` of the user.

- Add `client` argument to `triage_message()`, now it's possible to
call bot with another method instead of calling the specified
keyword.
This commit is contained in:
Rafid Aslam 2016-12-15 14:38:39 +07:00 committed by showell
parent 6df2450b60
commit 8d35f25fd6
10 changed files with 20 additions and 11 deletions

View file

@ -75,7 +75,7 @@ class CommuteHandler(object):
you can use the timezone bot.
'''
def triage_message(self, message):
def triage_message(self, message, client):
# return True iff we want to (possibly) response to this message
original_content = message['content']
# This next line of code is defensive, as we

View file

@ -21,7 +21,7 @@ class DefineHandler(object):
messages with "@define".
'''
def triage_message(DefineHandler, message):
def triage_message(DefineHandler, message, client):
# return True if we want to (possibly) response to this message
original_content = message['content']
# This next line of code is defensive, as we

View file

@ -22,7 +22,7 @@ class FollowupHandler(object):
called "followup" that your API user can send to.
'''
def triage_message(self, message):
def triage_message(self, message, client):
# return True iff we want to (possibly) response to this message
original_content = message['content']

View file

@ -31,7 +31,7 @@ class GitHubHandler(object):
'/<username>/<repository_owner>/<repository>/<issue_number>/<your_comment>'.
'''
def triage_message(self, message):
def triage_message(self, message, client):
# return True iff we want to (possibly) response to this message
original_content = message['content']

View file

@ -52,7 +52,7 @@ class IssueHandler(object):
github_token (The personal access token for the github bot)
'''
def triage_message(self, message):
def triage_message(self, message, client):
# return True if we want to (possibly) respond to this message
original_content = message['content']
# This next line of code is defensive, as we

View file

@ -11,7 +11,7 @@ class HelpHandler(object):
your Zulip instance.
'''
def triage_message(self, message):
def triage_message(self, message, client):
# return True if we think the message may be of interest
original_content = message['content']

View file

@ -56,7 +56,7 @@ class HowdoiHandler(object):
* @howdowe! OR @howdoi! > Full answer from SO
'''
def triage_message(self, message):
def triage_message(self, message, client):
cmd_list = ['@howdowe', '@howdoi', '@howdowe!', '@howdoi!']
question = message['content']

View file

@ -7,7 +7,7 @@ class VirtualFsHandler(object):
def usage(self):
return get_help()
def triage_message(self, message):
def triage_message(self, message, client):
# return True iff we want to (possibly) response to this message
if message['type'] != 'stream':
return False
@ -16,7 +16,7 @@ class VirtualFsHandler(object):
return original_content.startswith('fs ')
def handle_message(self, message, client, state_handler):
assert self.triage_message(message)
assert self.triage_message(message, client)
original_content = message['content']
command = original_content[len('fs '):]

View file

@ -27,7 +27,7 @@ class WikipediaHandler(object):
"@wiki".
'''
def triage_message(self, message):
def triage_message(self, message, client):
# return True iff we want to (possibly) response to this message
original_content = message['content']

View file

@ -18,7 +18,15 @@ from zulip import Client
class RestrictedClient(object):
def __init__(self, client):
# Only expose a subset of our Client's functionality
user_profile = client.get_profile()
self.send_message = client.send_message
try:
self.full_name = user_profile['full_name']
self.email = user_profile['email']
except KeyError:
logging.error('Cannot fetch user profile, make sure you have set'
' up the zuliprc file correctly.')
sys.exit(1)
def get_lib_module(lib_fn):
lib_fn = os.path.abspath(lib_fn)
@ -60,7 +68,8 @@ def run_message_handler_for_bot(lib_module, quiet, config_file):
def handle_message(message):
logging.info('waiting for next message')
if message_handler.triage_message(message=message):
if message_handler.triage_message(message=message,
client=restricted_client):
message_handler.handle_message(
message=message,
client=restricted_client,