在响应用户对Twilio功能的消息时,如果他们发送一组6位数的号码,我想发送这6位数以及通过POST请求进行身份验证将其发送到另一个网站的电话。
我对这种类型的集成还很陌生,但是只能回复短信发送者,而没有向外部网站发送请求。
/*
After you have deployed your Function, head to your phone number and configure the inbound SMS handler to this Function
*/
exports.handler = function(context, event, callback) {
let twiml = new Twilio.twiml.MessagingResponse();
const body = event.Body ? event.Body.toLowerCase() : null;
switch (body) {
case 'authcode':
twiml.message("Authenticate your account phone number to app.machinesaver.io by respond to this number with the 6-digit AuthCode sent when you signed up for an account at app.machinesaver.io/create-admin-account.");
break;
case 'help':
twiml.message("You can ask me ABOUT, ADDRESS, PHONE, or EMAIL");
break;
case 'about':
twiml.message("---------- is a technology company located in -------, -- USA.");
break;
case 'address':
twiml.message("Our address is: Address");
break;
case 'email':
twiml.message("Sales: \nService: \nAccounting: \n");
break;
case 'phone':
twiml.message("Main: PhoneNumber");
break;
default:
twiml.message("Sorry, I only understand HELP, ABOUT, ADDRESS, PHONE, AUTHCODE and EMAIL");
break;
}
callback(null, twiml);
};
没有错误,但我不确定下一步该怎么做,才能从此函数中将POST请求发送到非Twilio URL,仅响应之一。我想我可以使用正则表达式来查找6位数的短信时的情况,但此后我不确定如何发送POST请求并等待其他网站的响应(基本上是通过验证该用户的电话号码6位验证码)。
Twilio文档建议添加以下内容以发出POST请求,但我不确定在处理程序中的何处添加此内容以正确发送请求并接收响应(可能在这种情况之后?还是我需要声明变量放在顶部,其余的放在AUTHCODE后面吗?):
var got = require('got');
var requestPayload = {foo: 'bar'};
got.post('https://your-api.com/endpoint',
{ body: JSON.stringify(requestPayload),
headers: {
'accept': 'application/json'
},
json: true
}).then(function(response) {
console.log(response.body)
callback(null, response.body);
}).catch(function(error) {
callback(error)
});
答案 0 :(得分:0)
有两种方法:
答案 1 :(得分:0)
这里是Twilio开发人员的传播者。
Twilio Functions仅在运行Node.js,因此您可以使用本机HTTP模块(如Aaron建议)或通过在config section of your Twilio console中添加依赖项(如got
)发出HTTP请求。并使用它们。
在Twilio函数中执行异步操作(例如发出HTTP请求)的重要事情是在操作完成后调用callback
函数,否则该操作可能在完成前被切断。
以您的示例为例,并对其进行扩展,发送可选的HTTP请求可能如下所示:
const got = require('got');
exports.handler = function(context, event, callback) {
let twiml = new Twilio.twiml.MessagingResponse();
const body = event.Body ? event.Body.toLowerCase() : null;
switch (body) {
case 'authcode':
twiml.message("Authenticate your account phone number to app.machinesaver.io by respond to this number with the 6-digit AuthCode sent when you signed up for an account at app.machinesaver.io/create-admin-account.");
break;
case 'help':
twiml.message("You can ask me ABOUT, ADDRESS, PHONE, or EMAIL");
break;
case 'about':
twiml.message("---------- is a technology company located in -------, -- USA.");
break;
case 'address':
twiml.message("Our address is: Address");
break;
case 'email':
twiml.message("Sales: \nService: \nAccounting: \n");
break;
case 'phone':
twiml.message("Main: PhoneNumber");
break;
default:
twiml.message("Sorry, I only understand HELP, ABOUT, ADDRESS, PHONE, AUTHCODE and EMAIL");
break;
}
if (// some condition that triggers the HTTP request) {
got.post(url, {
body: JSON.stringify({ example: 'parameters' }),
json: true
}).then(response => {
callback(null, twiml);
}).catch(error => {
callback(error);
});
} else {
callback(null, twiml);
}
};
让我知道是否有帮助。