-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathslack-integration.gs
132 lines (115 loc) · 3.61 KB
/
slack-integration.gs
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
var spreadsheetURL = YOURSPREADSHEETURL;
var spreadsheetID = YOURSPREADSHEETID;
var inWebhook = 'https://hooks.slack.com/services/YOURWEBHOOKURL';
var chatPostURL = 'https://slack.com/api/chat.postMessage';
var channel = YOURCHANNEL;
var channelID = YOURCHANNELID;
var appToken = YOURAPPTOKEN;
var appUserID = YOURAPPUSERID;
var botToken = YOURBOTTOKEN;
var botUserID = BOTUSERID;
var teamID = YOURTEAMID;
var scope = 'identify,bot,incoming-webhook,chat:write:bot';
function findInColumn(column, data) {
var column = spreadsheet.getRangeByName(column);
var values = column.getValues();
var row = 0;
while ( values[row] && values[row][0] !== data ) {
row++;
}
if (values[row] && values[row][0] === data)
return row;
else
return -1;
}
function idFromName(name) {
var row = findInColumn("Name", name);
if (row != -1) {
var range = spreadsheet.getRangeByName("SlackID");
var values = range.getValues();
return values[row];
}
else
return '';
}
function postNewTask(owner, task, dueDate, notes, reviewer) {
var ownerID = idFromName(String(owner));
var row = findInColumn("Task", task);
var cellString = 'A1'; // + row;
var text = ":memo: <" + spreadsheetURL + "&range=" +
cellString + "|*" + task +
"*> assigned to";
if (ownerID != '')
text = text + "<@" + ownerID + ">";
else
text = text + " _" + owner + "_:grey_question:";
if (dueDate != '')
text = text + "\n\n*Due Date*\n " + Utilities.formatDate(new Date('' + dueDate), "EST", "EEE MMM d");
if (notes != '')
text = text + "\n\n*Notes*\n " + notes;
if (reviewer != '') {
var reviewerID = idFromName(String(reviewer));
text = text + "\n\n*Reviewer*\n <@" + reviewerID + ">";
}
var payload = {
"channel": "#" + channel,
"mrkdwn_in": ["text"],
"text": text,
};
var url = inWebhook;
var options = {
'method': 'post',
'payload': JSON.stringify(payload)
};
var response = UrlFetchApp.fetch(inWebhook,options);
}
function postNeedsReview(task, notes, reviewer) {
var row = findInColumn("Task", task);
var cellString = 'A1'// + row;
if (reviewer != '')
var reviewerID = '@' + idFromName(String(reviewer));
else
var reviewerID = '!channel';
var text = ":eyes: <" + spreadsheetURL + "|*" + task + "*> needs review by <" + reviewerID + ">";
if (notes != '')
text = text + "\n\n*Notes*\n " + notes;
var payload = {
"channel": "#" + channel,
"mrkdwn_in": ["text"],
"text": text,
};
var url = inWebhook;
var options = {
'method': 'post',
'payload': JSON.stringify(payload)
};
var response = UrlFetchApp.fetch(inWebhook,options);
}
function postUserDigest(user, dmID, tasks, reviews, viewURL) {
Logger.log(dmID);
if (tasks[0][0] == "#N/A" && reviews[0][0] == "#N/A")
return user + ": No matches, no digest sent";
var payload = {
// 'token': appToken
'token': botToken,
'channel': dmID,
// 'as_user': 'true',
'username': 'TaskBot',
'icon_url': 'https://s3-us-west-2.amazonaws.com/slack-files2/avatars/2017-10-03/252104451239_0c43f0197a3224e66d47_96.png',
"text": "Hello, " + user + "! Here are your incomplete <" + viewURL + "|tasks and items needing review>:\n",
};
for (var i = 0; i < tasks.length; i++) {
if (tasks[i][0] != "#N/A")
payload.text += ':memo: ' + tasks[i][0] + '\n';
}
for (var i = 0; i < reviews.length; i++) {
if (reviews[i][0] != "#N/A")
payload.text += ':eyes: ' + reviews[i][0] + '\n';
}
var options = {
'method': 'post',
'payload': payload
};
var response = UrlFetchApp.fetch(chatPostURL, options);
return response;
}