Merge branch 'master' of github.com:slackhq/python-rtmbot

Conflicts:
	requirements.txt
This commit is contained in:
Ryan Huber 2014-12-11 16:28:06 -08:00
commit 49275291e1
3 changed files with 47 additions and 23 deletions

View file

@ -19,24 +19,16 @@ 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.)
3. Configure rtmbot
pip install -r requirements.txt
3. Configure rtmbot (https://api.slack.com/bot-users)
cp doc/example-config/rtmbot.conf .
vi rtmbot.conf

View file

@ -1,4 +1,5 @@
requests
python-daemon
pyyaml
websocket-client
git+https://github.com/slackhq/python-slackclient.git

View file

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