以下是我用于在nodejs(express)app中发送消息响应的代码,该应用已订阅webhooks。
我发送任何来自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 }
以下是向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);
提前感谢。期待回复。
答案 0 :(得分:0)
问题已解决,我发现facebook被我的IP阻止,因此它给出了Error: socket hang up
。我取消阻止了Facebook,并且api做出了响应
谢谢