Created
June 3, 2016 18:36
-
-
Save jedwood/c85aa7a3ad33957975459296785279ce to your computer and use it in GitHub Desktop.
Google Apps Script Library for interacting with the Slack API (likely outdated, as this was circa 2014)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var token = false; | |
/** | |
* Set the token. | |
* | |
* @param {string} Slack API token; | |
*/ | |
function setToken(t){ | |
token = t; | |
} | |
/** | |
* Fetches active (not archived) channels. | |
* | |
* @return {obj} the channels and meta stuff | |
*/ | |
function getChannels() { | |
checkToken_(); | |
var url = 'https://slack.com/api/channels.list?token=' + token + '&exclude_archived=1'; | |
return getJSON_(url); | |
} | |
/** | |
* Fetches recent comments from a channel. | |
* | |
* @param {string} channel the Slack channel id | |
* @param {number} oldest (optional) timestamp of how far back to fetch; | |
* @param {number} latest (optional) timestamp of how recent to fetch; | |
* @return {obj} the comments and meta stuff | |
*/ | |
function getRecentComments(channel, oldest, latest) { | |
checkToken_(); | |
if (!channel) throw "No channel specified"; | |
var count = 100; | |
var url = 'https://slack.com/api/channels.history?token=' + token + '&channel=' + channel + '&count=' + count; | |
// check if this is actually a group | |
if (/^G/.test(channel)) { | |
url = 'https://slack.com/api/groups.history?token=' + token + '&channel=' + channel + '&count=' + count | |
} | |
if (oldest) url += '&oldest=' + oldest; | |
if (latest) url += '&latest=' + latest; | |
return getJSON_(url); | |
} | |
function getFileInfo(id) { | |
checkToken_(); | |
if (!id) throw "No file id specified"; | |
var url = 'https://slack.com/api/files.info?token=' + token + '&file='+id; | |
return getJSON_(url); | |
} | |
/** | |
* Fetches recent files. | |
* | |
* @param {number} count (optional) the number of files to fetch. Defaults to 20; | |
* @return {obj} the files and meta stuff | |
*/ | |
function getRecentFiles(count) { | |
checkToken_(); | |
count = count || 20; | |
var url = 'https://slack.com/api/files.list?token=' + token + '&types=images&count=' + count; | |
return getJSON_(url); | |
} | |
/** | |
* Fetches users, in no particular order. | |
* | |
* @return {obj} the users and meta stuff | |
*/ | |
function getUsers() { | |
checkToken_(); | |
var url = 'https://slack.com/api/users.list?token=' + token; | |
return getJSON_(url); | |
} | |
/** | |
* Chat on Slack! | |
* @param {obj} opt includes channel, message, username (from), icon (optional); | |
* @return {obj} meta stuff | |
*/ | |
function post(opt) { | |
var icon = opt.icon || 'https://i.cloudup.com/rfTnn0xG2K.png'; | |
var from = opt.from || 'robo bot'; | |
if (!opt.text) return "ERROR: you need to include some text"; | |
url = 'https://slack.com/api/chat.postMessage?token=' + token + '&link_names=1&channel=' + encodeURIComponent(opt.channel) + '&text=' + encodeURIComponent(opt.text) + '&username=' + encodeURIComponent(from) + '&icon_url=' + icon; | |
return getJSON_(url); | |
} | |
/** | |
* Create a file | |
* @param {obj} opt includes channel, message, username (from), icon (optional); | |
* @return {obj} the file and meta stuff | |
*/ | |
function createFile(opt) { | |
var payload = {}; | |
var icon = opt.icon || 'https://i.cloudup.com/rfTnn0xG2K.png'; | |
var from = opt.from || 'robo bot'; | |
if (!opt.filename) return "ERROR: you need to include some text"; | |
url = 'https://slack.com/api/files.upload?token=' + token + '&link_names=1&channels=' + encodeURIComponent(opt.channels) + '&filename=' + encodeURIComponent(opt.filename) + '&username=' + encodeURIComponent(from) + '&icon_url=' + icon; | |
if (opt.content) payload.content = opt.content; | |
if (opt.title) url += "&title=" + encodeURIComponent(opt.title); | |
if (opt.initial_comment) url += "&initial_comment=" + encodeURIComponent(opt.initial_comment); | |
return getJSON_(url, payload); | |
} | |
/** | |
* Create a private group | |
* @param {obj} opt includes name and (optional) initial invitees; | |
* @return {obj} the group info | |
*/ | |
function createGroup(opt) { | |
var payload = {}; | |
url = 'https://slack.com/api/groups.create?token=' + token; | |
if (opt.name) payload.name = opt.name; | |
var leGroup = getJSON_(url, payload); | |
if (opt.purpose) { | |
url = 'https://slack.com/api/groups.setPurpose?token=' + token; | |
payload = {channel: leGroup.group.id, purpose:opt.purpose}; | |
getJSON_(url, payload); | |
} | |
if (opt.invitees) { | |
url = 'https://slack.com/api/groups.invite?token=' + token; | |
opt.invitees.forEach(function(u){ | |
payload = { | |
channel: leGroup.group.id, | |
user: u | |
} | |
getJSON_(url, payload); | |
}); | |
} | |
return leGroup; | |
} | |
function leaveGroup(channel) { | |
var url = 'https://slack.com/api/groups.leave?token=' + token; | |
var payload = {channel: channel}; | |
return getJSON_(url, payload); | |
} | |
function checkToken_(){ | |
if (!token) { | |
throw "No Slack token"; | |
} else { | |
return true; | |
} | |
} | |
function getJSON_(url, payload){ | |
var response; | |
if (payload) { | |
var options = | |
{ | |
method : "post", | |
payload : payload | |
}; | |
response = UrlFetchApp.fetch(url, options); | |
} else { | |
response = UrlFetchApp.fetch(url); | |
} | |
var json = response.getContentText(); | |
var data = JSON.parse(json); | |
return data; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment