问题
I am trying to send and receive SMS through Google Apps Script (GAS). GAS doesn't provide native support for sending text messages or checking Google Voice, so I have been looking at integrating with third party APIs - in this case Twilio.
I've been in touch with tech support at Twilio and they haven't been able to answer the question yet. I have also looked far and wide and have not found a functioning GAS example that works with Twilio successfully.
I followed the tutorial at https://developers.google.com/live/shows/11404657/. Then copied a non-functioning code example from the Google Apps Script team that parallels the code in the tutorial: https://github.com/entaq/GoogleAppsScript/blob/master/Twilio/RecieveSMS/RecieveSMS.gs -
My code is copied below:
The second last command, appendrow() works fine, implying that GAS GETs from the Twilio API correctly. The last command, createTextOutput(), however, does not work, which suggests GAS is unable to POST to Twilio in the right format... right?
function doGet(args) {
var spreadsheetID = "<< your spreadsheet ID >>";
var vote = args.parameter.Body;
var from = args.parameter.From;
var actualVote;
switch (vote.toLowerCase()) {
case "a":
actualVote = 'Giants';
break;
case "b":
actualVote = 'Jets';
break;
default:
actualVote = 'Dont care';
}
SpreadsheetApp.openById(spreadsheetID).appendRow([from,actualVote,vote]);
return ContentService.createTextOutput(LanguageApp.translate(args.parameter.Body, "en", "es")).setMimeType(ContentService.MimeType.TEXT);
};
I would be happy to use a workaround (email to SMS for example) BUT I have to both send and receive SMS AND I don't want to have to transfer over to another programming platform as we have already spent 4 months developing our internal operations software through Google Apps Script so the transfer over would be very onerous.
NOTE that this is not a problem with "chunked" XML responses - as was found in this thread: Connect Twilio wtih Google Apps Script - Twilio tech team told me yesterday that they upgraded their servers very recently to accept chunked responses.
Thanks!
回答1:
OK so Twilio tech Alex Chan came through with the answer!
Turns out, as Arun was saying, it is a different method for GETting and POSTing an SMS. In Alex Chan's words:
"Sending SMS messages with Twilio is different. Instead of implementing a "doGet" method, you have to invoke Twilio's REST API. The REST API operation for sending SMS messages is documented here:
http://www.twilio.com/docs/api/rest/sending-sms"
I am now using a variant of the below and it is working swimmingly:
function sendSms() {
// Get account SID and auth token here:
// https://www.twilio.com/user/account
var accountSid = "AC...";
var authToken = "...";
var url = "https://api.twilio.com/2010-04-01/Accounts/" + accountSid + "/SMS/Messages.json";
var options = {
method: "post",
headers: {
Authorization: "Basic " + Utilities.base64Encode(accountSid + ":" + authToken)
},
payload: {
// From is one of your Twilio phone numbers
From: "+12025551212",
To: "+14155551212",
Body: "Test from Google Apps Script"
}
};
var response = UrlFetchApp.fetch(url, options);
Logger.log(response);
}
Arun, thanks for your help and for posting the original code on gitHub!
回答2:
Its unclear what the question is - are you trying to send a message via Twilio or receive one? They are two very different code paths. Sending is a lot easier. Receiving a message into Apps Script requires you to deploy your script as a web app and then include that as the POST back URL in the Twilio console.
Also ensure that your web app is deployed as running as you and accesible anonymously.
回答3:
I've written a wrapper around the twilio nodejs lib for Google Apps Script you can check it out on github: https://github.com/illiatdesdindes/twilio-gas
Here's an example for sending an sms:
var account_sid = 'ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
var auth_token = 'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy';
var client = new twiliogas.RestClient(account_sid, auth_token);
client.sendSms({
to:'+16515556677', // Any number Twilio can deliver to
from: '+14506667788', // A number you bought from Twilio and can use for outbound communication
body: 'word to your mother.' // body of the SMS message
});
来源:https://stackoverflow.com/questions/15788594/twilio-send-and-receive-sms-through-google-apps-script