twilio API会继续发送请求,直到发生套接字挂起错误

时间:2020-10-07 11:44:39

标签: node.js twilio whatsapp twilio-api

我很久以来一直在尝试解决此错误。我在网上找不到类似的问题。我正在通过邮递员向twilio API发送whatsapp的请求。一切似乎还好。 Promise应该发送JSOM对象作为响应,但是它会一直发送请求,直到套接字挂起错误发生为止。这是我的代码

const dotenv = require('dotenv').config();
const express = require('express');
const { response } = require('express');
const app = express();
app.use(express.json());

exports.sendMessages = function(sender, reciever, message) {
  const accountSid = process.env.ACCOUNT_S_ID;
  const authToken = process.env.AUTH_TOKEN;
  const client = require('twilio')(accountSid, authToken); 
  client.messages
  .create({
    from: 'whatsapp:+'+sender,
    body: message,
    to: 'whatsapp:+'+reciever
  })

  .then(response => {
    return {
      data: JSON.stringify(response),
  }})
  .catch(e => { console.error('Got an error:', e.code, e.message); });
}

调用API

const express = require('express');
const app = express();
app.use(express.json());
// for parsing application/json
const send = require('./index');
let endPoint = process.env.ENDPOINT;
app.post(endPoint, function (req, res) {
send.sendMessages('14155238886', '393200149462','test message');
});
const port = process.env.PORT; 
app.listen(port);
console.log('Successfully connected to ' +port);

0 个答案:

没有答案