0

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

我在apiAiClient从 fb 页面发送来自 Fb Messenger 上的消息的任何消息时获得成功的随机消息,但它对 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 call 发送到 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);

提前致谢。期待答复。

4

1 回答 1

0

问题解决了,我发现facebook在我的IP上被屏蔽了,所以它是给Error: socket hang up的。我解锁了 facebook 并且 api 响应了,

谢谢

于 2018-06-25T12:14:27.587 回答