From 3eb1d6d9f57e706a5b251a144cdab1255c0b1225 Mon Sep 17 00:00:00 2001 From: Ryan Huber Date: Mon, 8 Dec 2014 16:01:33 -0800 Subject: [PATCH 1/4] Update README.md --- README.md | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 08bdaf9..203b7a4 100644 --- a/README.md +++ b/README.md @@ -19,22 +19,14 @@ Dependencies Installation ----------- -1. Download and install the python-slackclient and websocket-client libraries - - git clone https://github.com/liris/websocket-client.git - cd websocket-client - sudo python setup.py install - cd .. - git clone git@github.com:slackhq/python-slackclient.git - cd python-slackclient - sudo python setup.py install - cd .. - -2. Download the python-rtmbot code +1. Download the python-rtmbot code git clone git@github.com:slackhq/python-rtmbot.git cd python-rtmbot +2. Install dependencies ([virtualenv](http://virtualenv.readthedocs.org/en/latest/) is recommended.) + + pip install -r requirements.txt 3. Configure rtmbot From 749988b7c1abbda6a04ec90f3072a2496212543c Mon Sep 17 00:00:00 2001 From: Ryan Huber Date: Tue, 9 Dec 2014 11:52:59 -0800 Subject: [PATCH 2/4] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 203b7a4..f8a19bf 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ Installation pip install -r requirements.txt -3. Configure rtmbot +3. Configure rtmbot (token can be found at https://api.slack.com/web) cp doc/example-config/rtmbot.conf . vi rtmbot.conf From 2b7145d0eda275bdfd105f783fe6f22276bf39da Mon Sep 17 00:00:00 2001 From: Ryan Huber Date: Tue, 9 Dec 2014 11:55:20 -0800 Subject: [PATCH 3/4] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f8a19bf..e078a17 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ Installation pip install -r requirements.txt -3. Configure rtmbot (token can be found at https://api.slack.com/web) +3. Configure rtmbot (https://api.slack.com/bot-users) cp doc/example-config/rtmbot.conf . vi rtmbot.conf From 68d3784533552cc02935a543994c0f4243f6769e Mon Sep 17 00:00:00 2001 From: Ryan Huber Date: Thu, 11 Dec 2014 16:25:41 -0800 Subject: [PATCH 4/4] add shiny new daemonize mode --- requirements.txt | 1 + rtmbot.py | 51 ++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 42 insertions(+), 10 deletions(-) diff --git a/requirements.txt b/requirements.txt index f771ba9..ced3092 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ +python-daemon pyyaml websocket-client git+https://github.com/slackhq/python-slackclient.git diff --git a/rtmbot.py b/rtmbot.py index a5147a2..ea706ea 100755 --- a/rtmbot.py +++ b/rtmbot.py @@ -9,12 +9,13 @@ import json import os import sys import time +import logging from slackclient import SlackClient def dbg(debug_string): if debug: - print debug_string + logging.info(debug_string) class RtmBot(object): def __init__(self, token): @@ -55,30 +56,34 @@ class RtmBot(object): for plugin in self.bot_plugins: plugin.do_jobs() def load_plugins(self): - path = os.path.dirname(sys.argv[0]) - for plugin in glob.glob(path+'/plugins/*'): + for plugin in glob.glob(directory+'/plugins/*'): sys.path.insert(0, plugin) - sys.path.insert(0, path+'/plugins/') - for plugin in glob.glob(path+'/plugins/*.py') + glob.glob(path+'/plugins/*/*.py'): - print plugin - name = plugin.split('/')[-1][:-2] + sys.path.insert(0, directory+'/plugins/') + for plugin in glob.glob(directory+'/plugins/*.py') + glob.glob(directory+'/plugins/*/*.py'): + logging.info(plugin) + name = plugin.split('/')[-1][:-3] # try: self.bot_plugins.append(Plugin(name)) # except: # print "error loading plugin %s" % name class Plugin(object): - def __init__(self, name): + def __init__(self, name, plugin_config={}): self.name = name self.jobs = [] self.module = __import__(name) self.register_jobs() self.outputs = [] + if name in config: + logging.info("config found for: " + name) + self.module.config = config[name] + if 'setup' in dir(self.module): + self.module.setup() def register_jobs(self): if 'crontable' in dir(self.module): for interval, function in self.module.crontable: self.jobs.append(Job(interval, eval("self.module."+function))) - print self.module.crontable + logging.info(self.module.crontable) else: self.module.crontable = [] def do(self, function_name, data): @@ -104,6 +109,7 @@ class Plugin(object): while True: if 'outputs' in dir(self.module): if len(self.module.outputs) > 0: + logging.info("output from {}".format(self.module)) output.append(self.module.outputs.pop(0)) else: break @@ -136,9 +142,34 @@ class UnknownChannel(Exception): pass +def main_loop(): + logging.basicConfig(filename=config["LOGFILE"], level=logging.INFO, format='%(asctime)s %(message)s') + logging.info(directory) + try: + bot.start() + except KeyboardInterrupt: + sys.exit(0) + except: + logging.exception('OOPS') + if __name__ == "__main__": + directory = os.path.dirname(sys.argv[0]) + if not directory.startswith('/'): + directory = os.path.abspath("{}/{}".format(os.getcwd(), + directory + )) + config = yaml.load(file('rtmbot.conf', 'r')) debug = config["DEBUG"] bot = RtmBot(config["SLACK_TOKEN"]) - bot.start() + site_plugins = [] + files_currently_downloading = [] + job_hash = {} + + if config.has_key("DAEMON"): + if config["DAEMON"]: + import daemon + with daemon.DaemonContext(): + main_loop() + main_loop()