-
Notifications
You must be signed in to change notification settings - Fork 0
/
twilio.js
149 lines (121 loc) · 4.84 KB
/
twilio.js
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
146
147
148
149
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spreadsheet.getSheets()[0];
var senderRange = sheet.getRange("D1:H1");
// The row and column here are relative to the range
// getCell(1,1) in this code returns the cell at B2, B2
var authId_cell = senderRange.getCell(1, 2);
var authId = authId_cell.getValue();
var passwd_cell = senderRange.getCell(1, 4);
var passwd = passwd_cell.getValue();
function getCredentials() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spreadsheet.getSheets()[0];
var senderRange = sheet.getRange("D1:H1");
// The row and column here are relative to the range
// getCell(1,1) in this code returns the cell at B2, B2
var authId_cell = senderRange.getCell(1, 2);
var authId = authId_cell.getValue();
var passwd_cell = senderRange.getCell(1, 4);
var passwd = passwd_cell.getValue();
Logger.log("AuthID: " +authId+" PASSWORD: "+passwd1);
//return authId, passwd;
}
function sendSms(sender, to, body) {
var messages_url = "https://api.twilio.com/2010-04-01/Accounts/"+authId+"/Messages.json";
var payload = {
"To": to,
"Body" : body,
"From" : sender
};
var options = {
"method" : "post",
"payload" : payload
};
options.headers = {
"Authorization" : "Basic " + Utilities.base64Encode(""+authId+":"+passwd+"")
};
var response = UrlFetchApp.fetch(messages_url, options);
return response.getContentText();
}
function checkSMSstatus(SID) {
var messages_url = "https://api.twilio.com/2010-04-01/Accounts/"+authId+"/SMS/Messages/"+SID+".json";
var options = {
"method" : "get"
};
options.headers = {
"Authorization" : "Basic " + Utilities.base64Encode(""+authId+":"+passwd+"")
};
var response = UrlFetchApp.fetch(messages_url, options);
return response.getContentText();
}
function sendAll() {
//var sheet = SpreadsheetApp.getActiveSheet();
// The code below makes the 2nd sheet active in the active spreadsheet.
//var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spreadsheet.getSheets()[0];
var senderRange = sheet.getRange("B1:B1");
// The row and column here are relative to the range
// getCell(1,1) in this code returns the cell at B2, B2
var cellSender = senderRange.getCell(1, 1);
var sender = cellSender.getValue();
// SpreadsheetApp.setActiveSheet(sheet.getSheets()[0]);
var startRow = 3;
var numRows = sheet.getLastRow() - 1;
var dataRange = sheet.getRange(startRow, 1, numRows, 5)
var data = dataRange.getValues();
for (i in data) {
var row = data[i];
sheet.getRange(startRow + Number(i), 3).setValue("waiting...");
sheet.getRange(startRow + Number(i), 4).setValue("waiting..");
sheet.getRange(startRow + Number(i), 5).setValue("waiting..");
try {
response_data = sendSms(sender, row[0], row[1]);
var parseIt = JSON.parse(response_data);
// var smsStatus = JSON.parse(checkSMSstatus(parseIt.sid));
//var status = smsStatus.status;
var myStatus = parseIt.status;
sheet.getRange(startRow + Number(i), 3).setValue(parseIt.status);
sheet.getRange(startRow + Number(i), 4).setValue(parseIt.sid);
// sheet.getRange(startRow + Number(i), 5).setValue(status);
} catch(err) {
Logger.log(err);
sheet.getRange(startRow + Number(i), 3).setValue(err);
}
}
}
function checkSMSstatusAll() {
// var sheet = SpreadsheetApp.getActiveSheet();
// var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
// SpreadsheetApp.setActiveSheet(sheet.getSheets()[0]);
var sheet = spreadsheet.getSheets()[0];
var startRow = 3;
var numRows = sheet.getLastRow() - 1;
var dataRange = sheet.getRange(startRow, 1, numRows, 5)
var data = dataRange.getValues();
Logger.log("Data var "+data);
// Logger.log(data)
for (i in data) {
//var row = data[i];
var SID = sheet.getRange(startRow + Number(i), 4).getValue();
Logger.log("SID is "+SID)
if ((SID == "waiting..") || (SID == "")) {
continue;
}
var messages_url = "https://api.twilio.com/2010-04-01/Accounts/"+authId+"/SMS/Messages/"+SID+".json";
var options = {
"method" : "get"
};
options.headers = {
"Authorization" : "Basic " + Utilities.base64Encode(""+authId+":"+passwd+"")
};
try {
//var response = UrlFetchApp.fetch(messages_url, options);
var response = UrlFetchApp.fetch(messages_url, options);
var smsStatus = JSON.parse(response);
sheet.getRange(startRow + Number(i), 5).setValue(smsStatus.status);
} catch(err) {
Logger.log(err);
sheet.getRange(startRow + Number(i), 5).setValue(err);
}
}
}