diff --git a/README.md b/README.md index e078a17..69f970b 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,20 @@ Plugins can send messages back to any channel, including direct messages. This i *Note*: you should always create the outputs array at the start of your program, i.e. ```outputs = []``` +####Uploading files +Plugins can upload files to any channel, including direct messages. This is done by appending a dictionary to the files global array. The dictionary must have the items: file, name, channels, title, initial_comment. + + files = [] + data = {} + data['file'] = '' + data['name'] = "filename.txt" + data['channels'] = "C12345667" + data['title'] = "My sample file" + data['initial_comment'] = "My first file, yay!" + files.append(data) + +*Note*: you should always create the files array at the start of your program, i.e. ```files = []``` + ####Timed jobs Plugins can also run methods on a schedule. This allows a plugin to poll for updates or perform housekeeping during its lifetime. This is done by appending a two item array to the crontable array. The first item is the interval in seconds and the second item is the method to run. For example, this will print "hello world" every 10 seconds. diff --git a/doc/example-config/rtmbot.conf b/doc/example-config/rtmbot.conf index af4861d..5fa2a3f 100644 --- a/doc/example-config/rtmbot.conf +++ b/doc/example-config/rtmbot.conf @@ -1,3 +1,3 @@ DEBUG: False - +USER_TOKEN : "put a user token here if you want to upload files" SLACK_TOKEN: "xoxb-111111111111-2222222222222222222" diff --git a/rtmbot.py b/rtmbot.py index bc0669c..8b67e9d 100755 --- a/rtmbot.py +++ b/rtmbot.py @@ -10,6 +10,7 @@ import sys import time import logging +import requests from slackclient import SlackClient @@ -18,9 +19,10 @@ def dbg(debug_string): logging.info(debug_string) class RtmBot(object): - def __init__(self, token): + def __init__(self, token, user_token): self.last_ping = 0 self.token = token + self.user_token = user_token self.bot_plugins = [] self.slack_client = None def connect(self): @@ -35,6 +37,7 @@ def start(self): self.input(reply) self.crons() self.output() + self.upload() self.autoping() time.sleep(.1) def autoping(self): @@ -50,6 +53,17 @@ def input(self, data): for plugin in self.bot_plugins: plugin.register_jobs() plugin.do(function_name, data) + def upload(self): + for plugin in self.bot_plugins: + for f in plugin.do_upload(): + data = {'file' : (f['name'], f['file'])} + # Basic support for options + params = {'channels' : f['channels'], 'title' : f['title'], 'initial_comment' : f['initial_comment']} + # Need a user token to upload files + if len(self.user_token) > 0: + r = requests.post("https://slack.com/api/files.upload?token=" + str(self.user_token), files=data, params=params) + else: + logging.info("User Token required for file upload") def output(self): for plugin in self.bot_plugins: limiter = False @@ -84,6 +98,7 @@ def __init__(self, name, plugin_config={}): self.module = __import__(name) self.register_jobs() self.outputs = [] + self.files = [] if name in config: logging.info("config found for: " + name) self.module.config = config[name] @@ -93,7 +108,8 @@ 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))) - logging.info(self.module.crontable) + if len(self.module.crontable) > 0: + logging.info(self.module.crontable) self.module.crontable = [] else: self.module.crontable = [] @@ -115,6 +131,18 @@ def do(self, function_name, data): def do_jobs(self): for job in self.jobs: job.check() + def do_upload(self): + files = [] + while True: + if 'files' in dir(self.module): + if len(self.module.files) > 0: + logging.info("uploading file from {}".format(self.module)) + files.append(self.module.files.pop(0)) + else: + break + else: + self.module.files = [] + return files def do_output(self): output = [] while True: @@ -173,7 +201,7 @@ def main_loop(): config = yaml.load(file('rtmbot.conf', 'r')) debug = config["DEBUG"] - bot = RtmBot(config["SLACK_TOKEN"]) + bot = RtmBot(config["SLACK_TOKEN"], config["USER_TOKEN"]) site_plugins = [] files_currently_downloading = [] job_hash = {}