Twilio通过Google Apps脚本发送和接收短信 [英] Twilio send and receive SMS through Google Apps Script

查看:160
本文介绍了Twilio通过Google Apps脚本发送和接收短信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试通过Google Apps脚本(GAS)发送和接收短信。 GAS不提供发送短信或检查Google语音的原生支持,因此我一直在寻找与第三方API(在本例中为Twilio)的集成。



我已经与Twilio的技术支持人员联系过,他们还没有能够回答这个问题。我也看了很多,还没有找到一个能够成功与Twilio一起使用的GAS示例。

我在 https://developers.google.com/live/shows/11404657/ 。然后从Google Apps脚本团队中复制一个与本教程代码相似的非功能代码示例: https://github.com/entaq/GoogleAppsScript/blob/master/Twilio/RecieveSMS/RecieveSMS.gs -



我的代码复制如下:



最后一个命令appendrow()正常工作,这意味着正确地从Twilio API获取GAS GET。然而,最后一条命令createTextOutput()不起作用,这表明GAS无法以正确的格式向Twilio发布邮件......对吗?

 函数doGet(args){
var spreadsheetID =<<您的电子表格ID>>;

var vote = args.parameter.Body;
var from = args.parameter.From;

var actualVote;
switch(vote.toLowerCase()){
casea:
actualVote ='Giants';
休息;
案例b:
actualVote ='Jets';
休息;
默认值:
actualVote ='不关心';
}

SpreadsheetApp.openById(spreadsheetID).appendRow([from,actualVote,vote]);

返回ContentService.createTextOutput(LanguageApp.translate(args.parameter.Body,en,es))。setMimeType(ContentService.MimeType.TEXT);
};

我很乐意使用一种解决方法(例如,以电子邮件发送短信),但我必须同时发送并收到短信,我不想转移到另一个编程平台,因为我们已经花了4个月的时间通过Google Apps脚本开发我们的内部操作软件,因此转移将非常麻烦。



请注意,这对于分块的XML响应没有问题 - 正如在此主题中发现的那样:

谢谢!

好的,Twilio科技亚历克斯陈出现在答案中!



结果如Arun所说,这是一种获取和发布短信的不同方法。用Alex Chan的话说:

使用Twilio发送SMS消息是不同的,不必实施doGet方法,而必须调用Twilio的REST API。发送短信的信息记录在这里:

http://www.twilio.com/docs/api/rest/sending-sms



我现在使用的变体下面,它正在游泳:

 函数sendSms(){
//在此处获取帐户SID和身份验证令牌:
// 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:{
授权:Basic+ Utilities.base64Encode(accountSid +:+ authToken)
},
有效载荷:{
// From是您的Twilio电话号码之一
从:+12025551212,
收件人:+14155551212,
正文:Google Apps脚本测试
}
};
var response = UrlFetchApp.fetch(url,options);
Logger.log(response);

$ / code


$ b Arun,感谢您的帮助和在gitHub上发布原始代码! / p>

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!

解决方案

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!

这篇关于Twilio通过Google Apps脚本发送和接收短信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆