2016-11-22 01:44:16 +01:00
|
|
|
#!/usr/bin/env python
|
2016-08-17 18:34:11 +02:00
|
|
|
from __future__ import print_function
|
|
|
|
|
|
|
|
import importlib
|
|
|
|
import logging
|
|
|
|
import optparse
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
|
|
|
our_dir = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
|
|
|
|
# For dev setups, we can find the API in the repo itself.
|
|
|
|
if os.path.exists(os.path.join(our_dir, '../api/zulip')):
|
|
|
|
sys.path.append('../api')
|
|
|
|
|
|
|
|
from zulip import Client
|
|
|
|
|
|
|
|
class RestrictedClient(object):
|
|
|
|
def __init__(self, client):
|
|
|
|
# Only expose a subset of our Client's functionality
|
2016-12-15 08:38:39 +01:00
|
|
|
user_profile = client.get_profile()
|
2016-08-17 18:34:11 +02:00
|
|
|
self.send_message = client.send_message
|
2016-12-15 08:38:39 +01:00
|
|
|
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)
|
2016-08-17 18:34:11 +02:00
|
|
|
|
|
|
|
def get_lib_module(lib_fn):
|
|
|
|
lib_fn = os.path.abspath(lib_fn)
|
2016-12-14 11:12:26 +01:00
|
|
|
if not os.path.dirname(lib_fn).startswith(os.path.join(our_dir, 'lib')):
|
2016-08-17 18:34:11 +02:00
|
|
|
print('Sorry, we will only import code from contrib_bots/lib.')
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
if not lib_fn.endswith('.py'):
|
|
|
|
print('Please use a .py extension for library files.')
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
sys.path.append('lib')
|
|
|
|
base_lib_fn = os.path.basename(os.path.splitext(lib_fn)[0])
|
|
|
|
module_name = 'lib.' + base_lib_fn
|
|
|
|
module = importlib.import_module(module_name)
|
|
|
|
return module
|
|
|
|
|
2016-08-24 21:09:11 +02:00
|
|
|
def run_message_handler_for_bot(lib_module, quiet, config_file):
|
2016-08-17 18:34:11 +02:00
|
|
|
# Make sure you set up your ~/.zuliprc
|
2016-08-24 21:09:11 +02:00
|
|
|
client = Client(config_file=config_file)
|
2016-08-17 18:34:11 +02:00
|
|
|
restricted_client = RestrictedClient(client)
|
|
|
|
|
|
|
|
message_handler = lib_module.handler_class()
|
|
|
|
|
2016-10-10 18:42:14 +02:00
|
|
|
class StateHandler(object):
|
|
|
|
def __init__(self):
|
|
|
|
self.state = None
|
|
|
|
|
|
|
|
def set_state(self, state):
|
|
|
|
self.state = state
|
|
|
|
|
|
|
|
def get_state(self):
|
|
|
|
return self.state
|
|
|
|
|
|
|
|
state_handler = StateHandler()
|
|
|
|
|
2016-08-17 18:34:11 +02:00
|
|
|
if not quiet:
|
|
|
|
print(message_handler.usage())
|
|
|
|
|
|
|
|
def handle_message(message):
|
|
|
|
logging.info('waiting for next message')
|
2016-12-15 08:38:39 +01:00
|
|
|
if message_handler.triage_message(message=message,
|
|
|
|
client=restricted_client):
|
2016-08-17 18:34:11 +02:00
|
|
|
message_handler.handle_message(
|
|
|
|
message=message,
|
2016-10-10 18:42:14 +02:00
|
|
|
client=restricted_client,
|
|
|
|
state_handler=state_handler
|
|
|
|
)
|
2016-08-17 18:34:11 +02:00
|
|
|
|
|
|
|
logging.info('starting message handling...')
|
|
|
|
client.call_on_each_message(handle_message)
|
|
|
|
|
|
|
|
def run():
|
|
|
|
usage = '''
|
2016-11-22 01:44:16 +01:00
|
|
|
./run.py <lib file>
|
2016-08-17 18:34:11 +02:00
|
|
|
|
2016-11-22 01:44:16 +01:00
|
|
|
Example: ./run.py lib/followup.py
|
2016-08-17 18:34:11 +02:00
|
|
|
|
|
|
|
(This program loads bot-related code from the
|
|
|
|
library code and then runs a message loop,
|
|
|
|
feeding messages to the library code to handle.)
|
|
|
|
|
|
|
|
Please make sure you have a current ~/.zuliprc
|
|
|
|
file with the credentials you want to use for
|
|
|
|
this bot.
|
|
|
|
|
|
|
|
See lib/readme.md for more context.
|
|
|
|
'''
|
|
|
|
|
|
|
|
parser = optparse.OptionParser(usage=usage)
|
|
|
|
parser.add_option('--quiet', '-q',
|
2016-12-11 14:30:45 +01:00
|
|
|
action='store_true',
|
|
|
|
help='Turn off logging output.')
|
2016-08-24 21:09:11 +02:00
|
|
|
parser.add_option('--config-file',
|
2016-12-11 14:30:45 +01:00
|
|
|
action='store',
|
|
|
|
help='(alternate config file to ~/.zuliprc)')
|
2016-08-17 18:34:11 +02:00
|
|
|
(options, args) = parser.parse_args()
|
|
|
|
|
|
|
|
if len(args) == 0:
|
|
|
|
print('You must specify a library!')
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
lib_module = get_lib_module(lib_fn=args[0])
|
|
|
|
|
|
|
|
if not options.quiet:
|
|
|
|
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
|
|
|
|
|
2016-08-24 21:09:11 +02:00
|
|
|
run_message_handler_for_bot(
|
|
|
|
lib_module=lib_module,
|
|
|
|
config_file=options.config_file,
|
|
|
|
quiet=options.quiet
|
|
|
|
)
|
2016-08-17 18:34:11 +02:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
run()
|