-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcode.gs
72 lines (58 loc) · 1.76 KB
/
code.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
function Initialize() {
var triggers = ScriptApp.getProjectTriggers();
for (var i in triggers) {
ScriptApp.deleteTrigger(triggers[i]);
}
//Set a trigger when the form updates the spreadsheet to call our Slack notification function
ScriptApp.newTrigger("CreateMessage")
.forSpreadsheet(SpreadsheetApp.getActiveSpreadsheet())
.onFormSubmit()
.create();
}
function CreateMessage(e){
try {
var spreadsheet, columns;
var my_message;
//fetch the column names
spreadsheet = SpreadsheetApp.getActiveSheet();
columns = spreadsheet.getRange(1, 1, 1, spreadsheet.getLastColumn()).getValues()[0];
// Only include form values that are not blank
for (var keys in columns) {
var key = columns[keys];
var val = e.namedValues[key] ? e.namedValues[key].toString() : "";
if (val !== "") {
my_message +=key + ' :: ' + val + '\n';
}
}
SendSlackMessage(my_message);
} catch (e) {
Logger.log(e.toString());
}
}
function TestSlack(){
SendSlackMessage("testing my stuff");
}
function SendSlackMessage(message){
var url = "https://slack.com/api/chat.postMessage";
var payload =
{
"token" : "CHANGE TO YOU KEY",
"as_user" :"false",
"text" : "New Request\n" + message,
"channel" : "#YOUR-CHANNEL",
"attachments" : [{"pretext": "Notification", "text": message}],
"type" : "post",
};
var options =
{
"method" : "POST",
"payload" : payload,
"followRedirects" : false,
"muteHttpExceptions": true
};
var result = UrlFetchApp.fetch(url, options);
if (result.getResponseCode() == 200) {
var params = JSON.parse(result.getContentText());
Logger.log(params);
}
}