-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathslack.js
More file actions
145 lines (127 loc) · 5.09 KB
/
slack.js
File metadata and controls
145 lines (127 loc) · 5.09 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
let userList;
let messageList;
let color = true;
//Prevent the enter button from refreshing the page
document.getElementById('messageForm').addEventListener('submit', function(e) {
sendMessage();
e.preventDefault();
}, false);
/**
* Read messages from the slack and post them in the div
*/
function slackReader()
{
let socket;
messageList = [];
$.ajax({
url: "https://slack.com/api/rtm.connect?token=" + slackAPI, //Url for the rtm api. returns a websocket url
success: function(data) {
socket = new WebSocket(data.url); //opens a new websocket to access the slack messages
// Listen for messages
socket.addEventListener('message', function (event) {
let messageData = JSON.parse(event.data); //message data
//Is the event a message
if (messageData.type == "message") {
let text = messageData.text; //text of message
let name;
//Only bot user messages have a "username" property. Determines if message originates from bot
if ("username" in messageData)
{
name = messageData.username;
}
else //Not a bot message
{
//Run through array of users, to find the username of the message
for (let user of userList)
{
//if id of the user i'm looking at is the same as the id of the message, set the name to that user's name
if (user.id == messageData.user)
{
name = user.name;
break;
}
}
}
//Adds the message to the array of messages
if (text.substr(0, 11) == "!important!")
{
messageList.push(name + ": " + text.substr(11));
document.getElementById("slackAlert").innerHTML = name + ": " + text.substr(11);
color = !color;
if (color)
{
document.getElementById("slackAlert").style.backgroundColor = "#ff89c9";
}
else
{
document.getElementById("slackAlert").style.backgroundColor = "#89f9f9";
}
}
else
{
messageList.push(name + ": " + text);
}
//only show the 8 most recent messages
//while (messageList.length > 8)
//{
//messageList.shift(); //removes the oldest message
//}
document.getElementById("alertBox").innerHTML = ""; //clears the chat before refilling
//Run through the array of messages and re-populate the chat
//for (let b of messageList)
//{
//document.getElementById("alertBox").innerHTML += (b + "<br>");
//}
for (i = messageList.length - 1; i >= 0; i--)
{
document.getElementById("alertBox").innerHTML += (messageList[i] + "<br>");
}
}
});
}
});
}
/**
* Builds an array of objects holding a user's id and their name
*/
function storeUsers()
{
$.ajax({
url: "https://slack.com/api/users.list?token=" + slackAPI,
async: false,
type: "GET",
success: function(data) {
userList = [];
let users = JSON.parse(JSON.stringify(data)).members; //Grabs the list of members
//Runs through the list of users to build an array of objects holding the name and is
for (let i = 0; i < users.length; i++)
{
//Create an object holding an id and a name and add it to the userList array;
//if user is a bot, use the bot_id property
if ("bot_id" in users[i].profile)
{
userList[i] = {"id": users[i].profile.bot_id, "name": users[i].real_name};
}
else //else, use the id property
{
userList[i] = {"id": users[i].id, "name": users[i].real_name};
}
}
}
});
}
/**
* Send a message to the slack channel
*/
function sendMessage()
{
let message = document.getElementById("messageForm").elements[0].value; //Grab the text of the message from the form
//Send the message
$.ajax({
type: "POST",
url: "https://slack.com/api/chat.postMessage?token=" + slackAPI + "&channel=" + slackChannel + "&text=" + message,
success: function() {
document.getElementById("messageForm").elements[0].value = "";
}
})
}