POST 使用 nodejs 调用消息时,Facebook Graph Api 上的套接字挂起异常

Socket hang up exception on Facebook Graph Api on POST call to messages using nodejs

以下是我用来在订阅了 webhooks 的 nodejs (express) 应用程序中发送消息响应的代码。

我收到来自 apiAiClient 的成功随机消息 来自fb页面的Fb Messenger上的消息,但其post回调到fb页面抛出以下错误:

{ Error: socket hang up
at TLSSocket.onHangUp (_tls_wrap.js:1135:19)
at Object.onceWrapper (events.js:313:30)
at emitNone (events.js:111:20)
at TLSSocket.emit (events.js:208:7)
at endReadableNT (_stream_readable.js:1056:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
code: 'ECONNRESET',
path: null,
host: 'graph.facebook.com',
port: 443,
localAddress: undefined }

下面是发送 post 调用到 facebook api 的代码:

const sendTextMessage = (senderId, text) => {
  let req = {
    url: 'https://graph.facebook.com/v2.6/me/messages',
    qs: { access_token: FACEBOOK_ACCESS_TOKEN },
    method: 'POST',
    json: {
      messaging_type: "UPDATE",
      recipient: { id: senderId },
      message: { text: text }
    }
  };
  request(req, response => {
    console.log(response);
  });
};

module.exports = (event) => {
  const senderId = event.sender.id;
  const message = event.message.text;
  const apiaiSession = apiAiClient.textRequest(message, { sessionId: 'total-poc-bot' });
  apiaiSession.on('response', (response) => {
    const result = response.result.fulfillment.speech;
    sendTextMessage(senderId, result);
  });
  apiaiSession.on('error', error => console.log(error));
  apiaiSession.end();
};

这是我的节点应用程序

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

const verificaton = require('./controllers/verification');
const messageWebhook = require('./controllers/messageWebhook');

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

app.listen(3000, () => console.log('Webhook server is listening, port 3000'));
app.get('/', verificaton);
app.post('/', messageWebhook);

提前致谢。期待回复。

问题已解决,我发现 facebook 在我的 IP 上被阻止了,所以它给了 Error: socket hang up 。我解锁了 facebook,api 回应,

谢谢