TwilioException {message: "31002: Connection Declined"} 即使呼叫通过

问题描述

每当我使用 Twilio 拨打出站电话时,我都会收到“TwilioException {message: "31002: Connection Declined"}”。但是,电话会转到我的电话,我接听并听到认消息。似乎一切正常,但我仍然收到错误消息。我去了 Twilio 控制台查看调试器。它说,“尝试连接到此 URL 时出现网络故障 ### HTTP 连接失败”。但不知何故它到达了“/voice”,因为我在手机上接到了电话。

"use strict";

require("dotenv-safe").load();
const http = require("http");
const express = require("express");
const { urlencoded } = require("body-parser");
const twilio = require("twilio");
const ClientCapability = twilio.jwt.ClientCapability;
const VoiceResponse = twilio.twiml.VoiceResponse;
const accountSid = process.env.TWILIO_ACCOUNT_SID;
const authToken = process.env.TWILIO_AUTH_TOKEN;

let app = express();

// Generate a Twilio Client capability token
app.get("/token",(request,response) => {
  const capability = new ClientCapability({
    accountSid,authToken,});

  capability.addScope(
    new ClientCapability.OutgoingClientScope({
      applicationSid: process.env.TWILIO_TWIML_APP_SID,})
  );

  const token = capability.toJwt();
  // Include token in a JSON response
  response.send({
    token: token,});
});

app.use(express.static(__dirname + "/public"));
app.use(urlencoded({ extended: false }));

app.post("/voice",(request) => {
  const client = new twilio(accountSid,authToken);

  console.log(`## Making a call to ${request.body.number}##`);
  console.log(request.body);
  console.log("************************************");

  client.calls
    .create({
      url: "http://demo.twilio.com/docs/voice.xml",from: process.env.TWILIO_NUMBER,to: request.body.number,})
    .then((call) => console.log(call.sid));});



let server = http.createServer(app);
let port = process.env.PORT || 3000;
server.listen(port,() => {
  console.log(`Express Server listening on *:${port}`);
});

module.exports = app;

解决方法

这里是 Twilio 开发者布道者。

我猜您正在尝试从 Twilio ClientTwilio Voice SDK 发出呼叫。虽然您确实收到了来自 Twilio 的电话,电话播放了演示消息,但我猜您打算将浏览器/应用程序连接到电话。

这里发生的事情是您在客户端进行调用,这会导致 Twilio 向您的 TwiML 应用程序中设置的 URL 发出 Webhook 请求。该 URL 是您的 /voice 端点,它应该以 TwiML 进行响应,以告诉 Twilio 下一步该做什么。相反,您的代码从不响应此请求,尽管它确实使用 REST API 生成新调用,即您收到的调用。

因此,我们不应使用 REST API 发出调用,而应使用 <Dial> 返回 TwiML 以连接调用。请尝试以下操作:

app.post("/voice",(request,response) => {
  const voiceResponse = new twilio.twiml.VoiceResponse();

  console.log(`## Making a call to ${request.body.number}##`);
  console.log(request.body);
  console.log("************************************");

  voiceResponse.dial(request.body.number);

  response.setContentType("text/xml");
  response.send(voiceResponse.toString());
});

编辑

在评论中聊天后,这里的目标似乎不是从浏览器向另一个人拨打电话,而只是触发外拨电话。在这种情况下,您根本不需要 Twilio 客户端或生成访问令牌。您只需向后端发出 ajax 请求,即可使用 REST API 生成调用。

在后端,这应该可以工作:

app.post("/voice",response) => {
  const client = new twilio(accountSid,authToken);

  console.log(`## Making a call to ${request.body.number}##`);
  console.log(request.body);
  console.log("************************************");

  client.calls
    .create({
      url: "http://demo.twilio.com/docs/voice.xml",from: process.env.TWILIO_NUMBER,to: request.body.number,})
    .then((call) => {
      console.log(call.sid);
      // return the CallSid to the front-end as proof of success
      response.json({ callSid: call.sid });
    })
    .catch(error => {
      console.error(error);
      response.status(500).send({ error: error.message });
    });
});

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...