Twilio 设备设置 - 注册、连接到 TWiML 应用程序,但不接听电话

Twilio Device Setup - Registers, Connects to TWiML App, but won't accept calls

我正在尝试通过 TWiML 应用在浏览器中设置软 phone。

我创建了以下内容:

设备设置正确,我可以正确地注册和连接。
该应用程序工作 - 当我按下 TWiML app page 上的“使用 Twilio 呼叫”按钮时,phone 在 +111 响铃,当我拨打指向 TWiML 应用程序的号码时,以及当我按下我的按钮时自己的页面。耶!

我想不通的是:

以下是所有相关页面:

calls.pug:

doctype html
html(lang="en")
  head
    meta(charset="utf-8")
      title Browser Calls
    body
      input#call(type="submit" value="call")
      input#register(type="submit" value="register")
      pre
      script(src="/js/twilio-sdk/twilio.js")
      script(type="module" src="/js/calls.js")

[客户]calls.js

const $ = css => document.querySelector(css);
const echo = msg => $('pre').innerText += msg + "\n";
    
var device;
    
async function setupDevice(){
    
    let data = await fetch('/token/new');
    data = await data.json();
    
    device = new Twilio.Device(data.token);
    device.on("error",
        err => echo("Twilio.Device Error: " + err.message));
    
    echo ('Device setup complete.')
    }
    
$('#register').addEventListener('click', async e => {
    if (!device) await setupDevice();
    
    device.on("registered", conn =>
        echo('Ready to accept incoming calls.')
        );
    device.on("incoming", call =>
        echo('Incoming call to device')
        );
    device.register()
    });
    
$('#call').addEventListener('click', async e => {
    if (!device) await setupDevice();
    let params = { To: '+22222222222' }
    device.connect({ params })
    });

[节点]token.js

const express = require('express');
const router = express.Router();
    
const AccessToken = require('twilio').jwt.AccessToken;
const VoiceGrant = AccessToken.VoiceGrant;
    
const config = require('../config');
router.get('/new', function (req, res) {
    const accessToken = new AccessToken(
        config.accountSid, config.apiKey, config.apiSecret, {identity: 'SG'}
        );
    
    const grant = new VoiceGrant({
    outgoingApplicationSid: config.appSid,
        incomingAllow: true,
        });
    accessToken.addGrant(grant);
    
    res.setHeader('Content-Type', 'application/json');
    res.send(JSON.stringify({ token: accessToken.toJwt() }));
    });
module.exports = router;

这里是 Twilio 开发人员布道者。

  • How to answer the phone using the Twilio Device.

    The device registers with no issue, so why doesn't it get incoming calls?

要直接调用设备,您需要 return TwiML <Dial>s the <Client> 使用您的访问令牌 ID(在本例中为“SG”)。

因此,如果您希望将对您的 Twilio 号码的呼叫定向到此客户端,您应该更新 Twilio 号码的传入 webhook 以指向 URL,return 是这样的:

<Response>
  <Dial>
    <Client>
      <Identity>SG</Identity>
    </Client>
  </Dial>
</Response>
  • How to call other numbers aside from the one in my app. I did in device.connect({params: {To: '+2222222222'}}), why doesn't it call that number?

目前,听起来您将设备指向一个 TwiML 应用程序,该应用程序使用静态 TwiML 进行响应,该应用程序拨打您的示例号码“+11111111111”。要使用这些参数,您需要使 TwiML 动态化,方法是从您的节点服务器 returning 来代替。参数被发送到 TwiML 终结点,因此您可以在响应中使用它们。

这是一个适合您的示例 Express 端点:

const express = require('express');
const router = express.Router();
const { VoiceResponse } = require("twilio").twiml;
const config = require('../config');

router.use(express.urlencoded({ extended: false }));

router.post("/call", function(res, res) {
  const To = req.body;
  const twiml = new VoiceResponse();
  const dial = twiml.dial({ callerId: config.twilioNumber });
  dial.number(To);
  res.set("Content-Type", "text/xml");
  res.send(twiml.toString());
});

module.exports = router;