Skip to content
This repository was archived by the owner on Aug 15, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'] = '<The File Data>'
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.

Expand Down
2 changes: 1 addition & 1 deletion doc/example-config/rtmbot.conf
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
DEBUG: False

USER_TOKEN : "put a user token here if you want to upload files"
SLACK_TOKEN: "xoxb-111111111111-2222222222222222222"
34 changes: 31 additions & 3 deletions rtmbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import sys
import time
import logging
import requests

from slackclient import SlackClient

Expand All @@ -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):
Expand All @@ -35,6 +37,7 @@ def start(self):
self.input(reply)
self.crons()
self.output()
self.upload()
self.autoping()
time.sleep(.1)
def autoping(self):
Expand All @@ -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
Expand Down Expand Up @@ -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]
Expand All @@ -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 = []
Expand All @@ -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:
Expand Down Expand Up @@ -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 = {}
Expand Down