How to Use Google Script with Google Spreadsheet t

2019-03-17 00:41发布

问题:

This Google Script page says that stackoverflow is the place to post Google Script questions, so here I am.

I want to use a Google Script to automatically send text messages (SMS) to all cell phone numbers in a Google Spreadsheet.

It is possible to send e-mails to all e-mail addresses in a spreadsheet by following this tutorial but when I try using phone numbers in a spreadsheet in place of e-mail addresses I get an error that those are not valid e-mail addresses. Fair enough. If I knew the cell phone companies tied to each of those cell phone numbers I could convert those phone numbers into e-mail addresses (a common option with several guides explaining it, like this one) but I don't have that cell phone provider information.

I am certainly not the first person to try to automatically send text messages from a computer rather than a cell phone. This question is one of the more popular (but also older) questions asking about it. Twilio pops up in several answers I've looked through but that is not a free service (although it is pretty inexpensive, I will admit.) Google has free options, though, so I figured I'd try those. Google Voice can send free text messages and you can even send free text messages through Gmail.

I know that many have searched for an API to use Google services to send text messages and there doesn't seem to be one. There are some interesting projects like google-voice-java but these are more work arounds than anything.

Thus I figured I'd just use one Google service (a Google Docs spreadsheet) to call another (Google Voice or GMail) through the use of a 3rd (Google Script). Is there some way to do this? Is there a way to get a Google Script to send text messages? For example, the MailApp.sendEmail sends e-mails - is there one to send text messages (SMS)? If not, can MailApp.sendEmail be massaged/jury rigged to send texts (without needing to turn the phone number into an e-mail address like [phonenumber]@txt.att.net)? Could some other scripting option be used, perhaps related to Google Chat (since the GMail texting option is related to Google Chat)?

回答1:

I was recently part of a team that produced a Google add-on named Text gBlaster that accomplishes what I believe you are asking for. It makes like easy so feel free to give it a try.

Aside from that, if you want to do it yourself without it sending via the carrier's email service (that Bill mentions above, which is a fine solution) then you will need to use a third party like Twilio (which is who we are using). You basically use the spreadsheet to send the request to Twilio and then Twilio sends the messages out.

Here is some basic code you can use with Twilio if you go that route. You will need to define toNumber, bodyMessage, SID, Token, and twilioNumber. toNumber is the number that is receiving the message, bodyMessage is the body of the text message, SID & Token are provided to you when you create a Twilio account, and twilioNumber is the phone number that you have through Twilio that the message will be sent from.

    function sendSms(toNumber,bodyMessage, SID, Token, twilioNumber) {
    var url = "https://api.twilio.com/2010-04-01/Accounts/" +            // URL used to enter correct Twilio acct
      SID + "/SMS/Messages.json";                                           
    var options = {                                                      // Specify type of message             
      method: "post",                                                    // Post rather than Get since we are sending
      headers: {                                                         
        Authorization: "Basic " + 
        Utilities.base64Encode(SID + ":" + Token)
      },
      payload: {                                                         // SMS details
        From: twilioNumber,
        To: toNumber,
        Body: bodyMessage
      }
    };
    var response = UrlFetchApp.fetch(url, options);                   // Invokes the action
    Logger.log(response);
}


回答2:

It depends on the cell phone carrier. To send a text to a Sprint number, for example, put the message in an email addressed in this manner:

2013456789@messaging.sprintpcs.com

(yes, they still have the "pcs" in the domain name...)