From c79e62cc8f2579fee4f7345dd5b79617d3a5eb6b Mon Sep 17 00:00:00 2001 From: Justin Kenyon Date: Fri, 18 Sep 2015 16:09:15 -0400 Subject: Add upload capability to slack buffer This commit: - Adds the ability to upload a file to the current slack buffer using the `/slack upload [file_path]` command Why? - This is a very useful feature for people that are trying to fully replace the slack web app --- wee_slack.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'wee_slack.py') diff --git a/wee_slack.py b/wee_slack.py index c789b43..4b334c0 100644 --- a/wee_slack.py +++ b/wee_slack.py @@ -4,6 +4,7 @@ from functools import wraps import time import json +import os import pickle import sha import re @@ -901,6 +902,26 @@ def slack_buffer_required(f): return wrapper +@slack_buffer_required +def command_upload(current_buffer, args): + """ + Uploads a file to the current buffer + /slack upload [file_path] + """ + post_data = {} + channel = current_buffer_name(short=True) + domain = current_domain_name() + token = servers.find(domain).token + + if servers.find(domain).channels.find(channel): + channel_identifier = servers.find(domain).channels.find(channel).identifier + + if channel_identifier: + post_data["token"] = token + post_data["channels"] = channel_identifier + post_data["file"] = args + async_slack_api_upload_request(token, "files.upload", post_data) + @slack_buffer_required def command_talk(current_buffer, args): """ @@ -1719,6 +1740,14 @@ def async_slack_api_request(domain, token, request, post_data, priority=False): dbg("URL: {} context: {} params: {}".format(url, context, params)) w.hook_process_hashtable(url, params, 20000, "url_processor_cb", context) +def async_slack_api_upload_request(token, request, post_data, priority=False): + if not STOP_TALKING_TO_SLACK: + url = 'https://slack.com/api/{}'.format(request) + file_path = os.path.expanduser(post_data["file"]) + command = 'curl -F file=@{} -F channels={} -F token={} {}'.format(file_path, post_data["channels"], token, url) + context = pickle.dumps({"request": request, "token": token, "post_data": post_data}) + w.hook_process(command, 20000, "url_processor_cb", context) + # funny, right? big_data = {} -- cgit