-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextWithGoogleSheets.gs
More file actions
123 lines (103 loc) · 3.25 KB
/
TextWithGoogleSheets.gs
File metadata and controls
123 lines (103 loc) · 3.25 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
/* usage:
This tutorial follows: https://www.twilio.com/blog/2016/02/send-sms-from-a-google-spreadsheet.html
• create a Twilio account, add $20 in credit and get a phone number. You'll need the acountid, auth token and phone number to fill in to
• Create a spreadsheet in GoogleSheets with a 3 column header:
Phone Number, Message, Status (this can be one sheet in many tabs)
• Fill in the fields below the header in the sheet
• Go to Tools->Script Editor
• Enter the below script.
• Make sure your sheet is the only one open, can be in another tab, and that the correct tab of the sheet is open, go back to the code
• Test by entering a number of your choice to the sendSmsTest after filling everything out, and select Run->sendSmsTest from the script editor
• When you are ready, Select Run->SendAll
*/
function sendSms(to, body) {
var messages_url = "https://api.twilio.com/2010-04-01/Accounts/YOURACCOUNTSID/Messages.json";
var payload = {
"To": to,
"Body" : body,
"From" : "YOURTWILIONUMBER"
};
var options = {
"method" : "post",
"payload" : payload
};
options.headers = {
"Authorization" : "Basic " + Utilities.base64Encode("YOURACCOUNTSID:YOURAUTHTOKEN")
};
UrlFetchApp.fetch(messages_url, options);
}
function sendAll() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2;
var numRows = sheet.getLastRow() - 1;
var dataRange = sheet.getRange(startRow, 1, numRows, 2)
var data = dataRange.getValues();
for (i in data) {
var row = data[i];
try {
response_data = sendSms(row[0], row[1]);
status = "sent";
} catch(err) {
Logger.log(err);
status = "error";
}
sheet.getRange(startRow + Number(i), 3).setValue(status);
}
}
function myFunction() {
sendAll();
}
function sendSmsTest() {
var sheet = SpreadsheetApp.getActiveSheet();
var messages_url = "https://api.twilio.com/2010-04-01/Accounts/YOURACCOUNTSID/Messages.json";
var payload = {
"To": "YOURTESTNUMBER",
"Body" : "just another test",
"From" : "YOURTWILIONUMBER"
};
var options = {
"method" : "post",
"payload" : payload
};
options.headers = {
"Authorization" : "Basic " + Utilities.base64Encode("YOURACCOUNTSID:YOURAUTHTOKEN")
};
UrlFetchApp.fetch(messages_url, options);
}
function sendSms(to, body) {
var messages_url = "https://api.twilio.com/2010-04-01/Accounts/YOURACCOUNTSID/Messages.json";
var payload = {
"To": to,
"Body" : body,
"From" : "YOURTWILIONUMBER"
};
var options = {
"method" : "post",
"payload" : payload
};
options.headers = {
"Authorization" : "Basic " + Utilities.base64Encode("YOURACCOUNTSID:YOURAUTHTOKEN")
};
UrlFetchApp.fetch(messages_url, options);
}
function sendAll() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2;
var numRows = sheet.getLastRow() - 1;
var dataRange = sheet.getRange(startRow, 1, numRows, 2)
var data = dataRange.getValues();
for (i in data) {
var row = data[i];
try {
response_data = sendSms(row[0], row[1]);
status = "sent";
} catch(err) {
Logger.log(err);
status = "error";
}
sheet.getRange(startRow + Number(i), 3).setValue(status);
}
}
function myFunction() {
sendAll();
}