我可以仅使用 twiml bin 将 SMS 转发到电子邮件吗? [英] Can I forward SMS to an email using ONLY a twiml bin?

查看:24
本文介绍了我可以仅使用 twiml bin 将 SMS 转发到电子邮件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很清楚您应该如何使用 twilio 将 SMS 转发到电子邮件地址 - 他们有一个很好的例子,可以使用托管在第三方服务器上的 php 代码.

It's very clear how you should forward an SMS to an email address using twilio - they have a good example of doing it with php code hosted on a third party server.

但是,我想仅使用 twiml 应用程序而不使用其他第三方请求/代码将短信转发到电子邮件地址.我尝试使用该功能:

However, I would like to forward an SMS to an email address using ONLY a twiml app and no other third party request / code. I attempted this using the function:

<Response>
<Message to="user@example.com">{{From}}: {{Body}}</Message>
</Response>

...但这不起作用,大概是因为只接受电话号码作为to:".

... but this did not work, presumably because only accepts a phone number as the "to:".

是否可以仅使用 twiml bin 来执行此操作?

Is it possible to do this using only a twiml bin ?

推荐答案

好吧,你说没有 3rd 方请求/代码,但是向 Google 请求和你自己的代码怎么样?

OK you said no 3rd party request/code, but how about a request to Google and your own code?

如果您有 Gmail 帐户,则可以使用 Google Scripts 来完成.他们为您托管代码,这很容易.

If you have a Gmail account you can do it with Google Scripts. They host the code for you and it's pretty easy.

转到 https://script.google.com 并创建一个新项目,删除窗口并粘贴以下内容,将电子邮件地址替换为您要接收电子邮件的地址.

Go to https://script.google.com and create a new project, delete the code in the window and paste in the following, replacing the email address with the one you want to get the emails sent to.

function doGet(e){
 var from = e.parameter.From;
 var to = e.parameter.To;
 var body = e.parameter.Body;
 var emailAddress = 'your@email.com';
 var message = body;
 var subject = 'SMS from ' + from + ' to ' + to + ' received';
 MailApp.sendEmail(emailAddress, subject, message);
 var output = ContentService.createTextOutput('<Response/>');
 output.setMimeType(ContentService.MimeType.XML);
 return output;
}

从发布"菜单中选择部署为 Web 应用程序".将应用程序执行为:设置为我,然后将谁有权访问应用程序设置为任何人,甚至匿名.

From the Publish menu choose deploy as web app. Set Execute the app as: to Me, then set Who has access to the app: to Anyone, even anonymous.

现在点击部署,Google 会要求您验证您的脚本,以允许它以您的身份发送电子邮件(但不能访问您收到的电子邮件).一旦您授予权限,它就会为您提供脚本的 URL.

Now click deploy and Google will ask you to authenticate your script to allow it to send email as you (but not access your incoming email). Once you have granted permission it will give you the URL for your script.

将此 URL 粘贴到您的 Twilio 控制台中作为消息传入时的 webhook 处理程序并将方法设置为 HTTP GET

Paste this URL into your Twilio console as the webhook handler for when a message comes in and set the method to HTTP GET

所有这一切都只是通过电子邮件向您发送短信.更改下面行中的 <Response/> 以包含 TwiML,以便在消息传入时回复发件人或您希望 Twilio 执行的任何其他操作.

All this will do is email you the SMS. Change the <Response/> in the line below to contain TwiML for replying to the sender or whatever else you want Twilio to do when messages come in.

var output = ContentService.createTextOutput('<Response/>');

显然,这只是一个基本示例,没有身份验证/安全性等,因此对您的脚本 URL 的任何调用都会向您发送一封电子邮件.您应该真正保护它,尽管 Google 生成的网址不是任何人都可能偶然发现的那种.

Obviously this is just a basic example, there's no authentication/security etc so any call to your script URL will send you an email. You should really secure it, although the URL Google generates isn't the kind anyone is likely to stumble upon.

这篇关于我可以仅使用 twiml bin 将 SMS 转发到电子邮件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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