当用户进行特定选择时,我如何(使用Twilio函数示例关键字处理程序)将邮政请求发送到外部网站



在响应用户消息到twilio函数时,如果他们发送了6位数字集,我想发送这6位数字以及将它们发送到另一个网站的电话张贴身份验证请求。

我是这种集成类型的新手

/*
   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);
};

没有错误,但是我不确定我可以做什么将邮政请求发送给此功能的非twilio url,仅以其中一个响应。我想我可以使用正则表达式来查找6位数字时的情况6位数authcode(。

Twilio文档建议将以下内容添加到发布后请求,但我不确定在处理程序中将其添加到正确发送请求并接收响应(也许是在案件之后?或我需要声明最顶部的变量,然后将其余的变量放在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)
});

有几种方法:

  1. twilio函数在nodejs上运行,因此您可以使用本机节点模块(例如http(提出后请求并同步等待响应。
  2. 您可以使用重定向在Twilio函数之外传递对消息响应的控制。使用重定向的twiml将要求您的外部功能返回有效的twiml。

twilio开发人员在这里。

twilio函数只是在运行node.js,因此您可以使用本机HTTP模块(如Aaron建议(或在Twilio Console的配置部分中添加依赖项(如got(并使用它们。p>执行异步操作(例如提出HTTP请求(,在Twilio函数中,在操作完成后调用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);
    }
};

让我知道这是否有帮助。

最新更新