-
Notifications
You must be signed in to change notification settings - Fork 0
/
API.xill
85 lines (70 loc) · 2.02 KB
/
API.xill
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/**
* This script is responsible for performing all operations related to
* the slack web API.
*
* This is part of the 'Connecting to a Web API (slack)' guide on http://xill.io
*
* @author Thomas Biesaart
*
*/
use XURL, String, Encode, System, Assert;
var token = "<< INSERT AUTHENTICATION TOKEN HERE >>";
var apiUrl = "https://slack.com/api/";
function getChannels() {
return get("channels.list", {}).channels;
}
function getChannelInfo(channelId) {
return get("channels.info", {"channel": channelId}).channel;
}
function sendMessage(channelId, message) {
return get(
"chat.postMessage",
{
"channel": channelId,
"text": message
}
);
}
private function get(method, parameters) {
var result = XURL.get(apiUrl :: method :: "?" :: parseParameters(parameters));
// Response must be 200 - OK
Assert.equal(
result.status.code,
200,
"The request failed and resulted in response code " :: result.status.code
);
// Check if there is an error
if(result.body.error != null) {
Assert.error("API Error: " :: result.body.error);
}
// Check if there is a warning
if(result.body.warning != null) {
System.print("API Warning: " :: result.body.warning, "warn");
}
return result.body;
}
private function parseParameters(parameters) {
var parsed = ["token=" :: token];
foreach(key, value in parameters) {
parsed[] = key :: "=" :: Encode.toPercent(value);
}
return String.join(parsed, "&");
}
private function runTests() {
do {
get("api.test", {});
} fail(e) {
Assert.error("API: FAIL - " :: e.message);
} success {
System.print("API: OK!");
}
do {
var authTest = get("auth.test", {});
System.print("Logged in to `" :: authTest.team :: "` as " :: authTest.user);
} fail(e) {
Assert.error("Auth: FAIL - " :: e.message);
} success {
System.print("Auth: OK!");
}
}
runTests();