Twilio - 未处理来电(快速入门)

时间:2018-04-20 00:18:55

标签: node.js twilio twilio-api

我已经注册开始使用Twilio并且我正在尝试设置快速启动(https://www.twilio.com/docs/voice/client/javascript/quickstart)并且它几乎正常工作但未接收来电:

客户端代码(在getTokenCapabilities之后在浏览器上使用):

  Twilio.Device.incoming(function (conn) {
    log('Incoming connection from ' + conn.parameters.From);
    var archEnemyPhoneNumber = '+12093373517';

    if (conn.parameters.From === archEnemyPhoneNumber) {
      conn.reject();
      log('It\'s your nemesis. Rejected call.');
    } else {
      // accept the incoming connection and start two-way audio
      conn.accept();
    }
  });

Twilio上的代码用于语音呼叫的功能(控制台总是打印,否则不会调用条件:

exports.handler = function(context, event, callback) {
    let twiml = new Twilio.twiml.VoiceResponse();
    console.log('entrou aqui');
    if(event.To) {
        console.log('entrou ali');
      // Wrap the phone number or client name in the appropriate TwiML verb
      // if is a valid phone number
      const attr = isAValidPhoneNumber(event.To) ? 'number' : 'client';

      const dial = twiml.dial({
        callerId: context.CALLER_ID,
      });
      dial[attr]({}, event.To);
    } else {
      twiml.say('Thanks for calling!');
    }
    console.log('callback');
     callback(null, twiml);
};

/**
* Checks if the given value is valid as phone number
* @param {Number|String} number
* @return {Boolean}
*/
function isAValidPhoneNumber(number) {
  return /^[\d\+\-\(\) ]+$/.test(number);
}

我已将我的电话号码包含在验证来电显示中,从Twilio获取了一个号码,并使用模板Twilio Client Quickstart创建了这些功能。

在Twilio客户端快速入门上,我将TwiML SID粘贴为TWIML_APP_SID,并尝试将我的电话号码和Twilio中的号码用作CALLER_ID。

此外,我在TwiML配置中更改了VOICE URL,并将电话号码上的VOICE URL更改为twilio配置。

关于什么是缺失或什么是错误的任何想法?当我在浏览器 http://127.0.0.1:8080/ 上打开时,可以拨打电话,但是当我拨打twilio号码时,我没有在浏览器上收到任何电话。

1 个答案:

答案 0 :(得分:1)

要接听电话,您需要在VoiceResponse中的标记中具有令牌名称身份,这是一个示例。

exports.incomingVoiceResponse = function voiceResponse( to ) {
  // Create a TwiML voice response
  const twiml = new VoiceResponse();

    // Wrap the phone number or client name in the appropriate TwiML verb
    // if is a valid phone number
    const attr = isAValidPhoneNumber(to) ? 'client' : 'number';

    const dial = twiml.dial({
      callerId: to,
    });
    dial[attr]({}, 'jesus');
  console.log(twiml.toString())
  return twiml.toString();
};

看到我放置的“耶稣”客户标签吗?这是令牌生成器端:

exports.tokenGenerator = function tokenGenerator() {
  const identity = 'jesus';
  const capability = new ClientCapability({
    accountSid: config.accountSid,
    authToken: config.authToken,
  });

  capability.addScope(new ClientCapability.IncomingClientScope(identity));
  capability.addScope(new ClientCapability.OutgoingClientScope({
    applicationSid: config.twimlAppSid,
    clientName: identity,
  }));

  // Include identity and token in a JSON response
  return {
    identity: identity,
    token: capability.toJwt(),
  };
};

这对我来说可以直接使用节点快速入门并更改这两个功能。

但是,不要伪造在voiceResponse函数中将“ number”从“ number”更改为“ client”,因为这是呼入而不是呼出。

  const attr = isAValidPhoneNumber(to) ? 'client' : 'number';

代替

  const attr = isAValidPhoneNumber(to) ? 'number' : 'client';

由于Twilio的client-quickstart-node中的默认nameGenerator会生成一个随机名称,因此在接收传入呼叫时未正确设置该名称。