【问题标题】:Twilio TwinML inside Node.js AppNode.js 应用程序中的 Twilio TwinML
【发布时间】:2018-05-06 21:44:00
【问题描述】:

如何实现并插入 Twilio 库中的参数?以下页面仅显示 Twiml 代码。

https://www.twilio.com/docs/sms/twiml

在本例中为 FromCountry 参数。我现在正在破坏我的头 5 个小时,但我找不到答案。这是我的代码:

const http = require('http');

const express = require('express');

const MessagingResponse = require('twilio').twiml.MessagingResponse;

const app = express();

app.post('/sms', (req, res) => {
  const twiml = new MessagingResponse();

  twiml.message(`Hi! It looks like your phone number was born in {FromCountry should go here}`);

  res.writeHead(200, {'Content-Type': 'text/xml'});
  res.end(twiml.toString());
});

http.createServer(app).listen(1337, () => {
  console.log('Express server listening on port 1337');
});

【问题讨论】:

  • 嗨,欢迎来到 Stackoverflow,我对您的问题进行了一些编辑,以删除问候和感谢。两者都不是必需的,因为 Stackoverflow 是一个用于编程相关问题的问答网站,而回答问题正是我们来这里的目的!

标签: javascript node.js express parameters twilio


【解决方案1】:

您要查找的内容在 req.body 中,因此您需要:

const MessagingResponse = require('twilio').twiml.MessagingResponse;
const bodyParser = require('body-parser'); 

const app = express();
app.use(bodyParser.urlencoded({ extended: false }));

然后:

app.post('/sms', (req, res) => {
    const twiml = new MessagingResponse();

    twiml.message('It looks like your phone number was born in ' + req.body.FromCountry);

    res.writeHead(200, { 'Content-Type': 'text/xml' });
    res.end(twiml.toString());
});

根据文档,FromCountry 可能并不总是可用:
(https://www.twilio.com/docs/sms/twiml#request-parameters)

Twilio 还尝试根据“发件人”和“收件人”电话号码查找地理数据。 Twilio 发送以下参数(如果可用):

FromCity        // The city of the sender
FromState       // The state or province of the sender.
FromZip         // The postal code of the called sender.
FromCountry     // The country of the called sender.
ToCity          // The city of the recipient.
ToState         // The state or province of the recipient.
ToZip           // The postal code of the recipient.
ToCountry       // The country of the recipient.

此博客(如何在 Node.js 中接收短信...)也可能会有所帮助: (https://www.twilio.com/blog/2016/08/how-to-receive-an-sms-in-node-js-with-twilio-and-hyperdev.html)

注意: @Alan 在 cmets 中建议链接到 Express req.body (https://expressjs.com/en/api.html#req.body)

【讨论】:

    【解决方案2】:

    您需要将这些行添加到您的应用程序中 -

    const bodyParser = require('body-parser');
    app.use(bodyParser.urlencoded({extended: false}));
    

    否则您的应用程序将不会解析请求中的参数。需要 URL 编码才能将此数据转换为字符串。从那里,您将能够看到 req.body,它看起来像这样 -

    { ToCountry: 'US',
      ToState: 'HI',
      SmsMessageSid: 'SM6d9aaa6e3f9dc2gjhasgd98sa9d963f',
      NumMedia: '0',
      ToCity: '',
      FromZip: '94105',
      SmsSid: 'SM6dva08hjhasashasgdj6ehjags82363f',
      FromState: 'CA',
      SmsStatus: 'received',
      FromCity: 'SAN FRANCISCO',
      Body: 'Make sure you add bodyParser',
      FromCountry: 'US',
      To: '+18081111111',
      ToZip: '',
      NumSegments: '1',
      MessageSid: 'SM6d999999sdhgjuasjdgasd63f',
      AccountSid: 'ACa131b11110nbasbdjkasghd45e0fd9',
      From: '+14159990000',
      ApiVersion: '2010-04-01' 
    }
    

    希望对您有所帮助。

    【讨论】:

      【解决方案3】:

      twilio 说他们在application/x-www-form-urlencoded 中向您发送参数

      因此,请使用 express 解析编码请求并从正文中挑选您需要的属性。

      示例:https://github.com/expressjs/body-parser#express-route-specific

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-08-03
        • 2018-11-10
        • 2016-04-07
        • 2016-12-07
        • 1970-01-01
        • 2018-11-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多