Last active
May 5, 2021 23:13
-
-
Save robjshaw/bd9c85a266cc0c6d528a0a47bafe9f37 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function onOpen() { | |
var spreadsheet = SpreadsheetApp.getActive(); | |
var menuItems = [ | |
{name: 'Send SMS...', functionName: 'sendSMSFunction_'} | |
]; | |
spreadsheet.addMenu('| Twilio SMS', menuItems); | |
} | |
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" : "<PHONENUMBER>" | |
}; | |
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"; | |
error = ''; | |
} catch(err) { | |
Logger.log(err); | |
status = "error"; | |
error = err; | |
} | |
sheet.getRange(startRow + Number(i), 3).setValue(status); | |
sheet.getRange(startRow + Number(i), 4).setValue(Utilities.formatDate(new Date(), "GMT+10", "dd/MM/yyyy HH:mm:ss")) | |
sheet.getRange(startRow + Number(i), 5).setValue(Session.getActiveUser().getEmail()); | |
sheet.getRange(startRow + Number(i), 6).setValue(error); | |
} | |
} | |
function sendSMSFunction_() { | |
var ui = SpreadsheetApp.getUi(); // Same variations. | |
var result = ui.alert( | |
'Please confirm', | |
'Are you sure you want to continue?', | |
ui.ButtonSet.YES_NO); | |
// Process the user's response. | |
if (result == ui.Button.YES) { | |
// User clicked "Yes". | |
ui.alert('Confirmation received. Sending Now...'); | |
sendAll(); | |
} else { | |
// User clicked "No" or X in the title bar. | |
ui.alert('Permission denied.'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment