Twilio打电话时如何接收号码?

时间:2018-05-01 03:52:35

标签: node.js twilio

语言:节点js

  1. 关注此make_phone_call.js,并将url替换为我的create voice.xml
  2. // Download the Node helper library from twilio.com/docs/node/install
    // These consts are your accountSid and authToken from twilio.com/user/account
    const accountSid = 'AC36861400a21a3ee42437f446015cd183';
    const authToken = 'your_auth_token';
    const Twilio = require('twilio');
    const client = new Twilio(accountSid, authToken);
    
    client.api.calls
      .create({
        url: 'http://127.0.0.1/docs/voice.xml',
        to: '+14155551212',
        from: '+15017122661',
      })
      .then(call => console.log(call.sid));

    1. voice.xml
    2. <?xml version="1.0" encoding="UTF-8"?>
      <Response>
          <Gather timeout="10" numDigits="1">
              <Say>Please press any key to continue.</Say>
          </Gather>
      </Response> 

      1. 但是当我执行“node make_phone_call.js”并返回sid时。
      2. -(~/proj/nodejs/twilio)
        └> node make_call_response.js
        CA5bbd4ef648d6f26b3302486ed0ff14ac

        1. 如果客户用户按“9”,我不知道,我在哪里收到号码?

1 个答案:

答案 0 :(得分:1)

Twilio开发者传道者在这里。

Generalhenry在评论中提出了一些好处,我只是想将这一切联系在一起。

首先,在您拨打电话的代码中,您将网址设置为http://127.0.0.1/docs/voice.xml。当呼叫连接时,Twilio将尝试向该URL发出HTTP请求(webhook)。但是,127.0.0.1是您的本地IP地址,并且无法公开访问,因此Twilio无法访问它。

我建议安装ngrok,这是一个工具,可以为您提供可以通过隧道传输到开发服务器的公共URL。 I'm a big fan of ngrok myself

设置了ngrok之后,您需要执行其他一些操作才能使用<Gather>

我们需要先更新您的TwiML。当<Gather>收到用户的输入时,它会向您使用action attribute定义的网址发出请求。如果没有定义该URL,它将向现有URL发出请求。我们添加action属性:

<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <Gather timeout="10" numDigits="1" action="/twiml/action">
        <Say>Please press any key to continue.</Say>
    </Gather>
</Response> 

看起来你只是在那里托管一个静态XML文件。这对于您的初始XML来说很好,但是当您想要检索用户输入的数字时,您将需要一个Web应用程序。由于您使用的是Node.js,我建议使用Express

我们需要构建一个Twilio可以发出请求的端点,允许您提取按下的数字。 Twilio将使用包含密钥的body参数Digits发送请求。让我们看看如何使用Express来提取该参数。

const app = require('express')();
const bodyParser = require('body-parser');

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

app.post('/twiml/action', (req, res) => {
  console.log(req.body.Digits);

  // return more TwiML
});

app.listen()

Digits是一个url编码参数集,作为正文的一部分。我们使用body-parser从body解析它,然后你可以用结果做你想做的事。

让我知道这是否有帮助。

相关问题