我正在尝试从Firebase功能使用Messenger平台发送消息。参考:https://developers.facebook.com/docs/messenger-platform/send-messages/。
无论如何,我的函数中处理此问题的部分如下:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }))
app.use(cors({ origin: true }));
app.post('/send-message', (req, res) => {
...
var accessToken = <PAGE_ACCESS_TOKEN>; // Getting it from environment variable
var url = 'https://graph.facebook.com/v3.3/me/messages?access_token='+accessToken;
axios.post(url, {
messaging_type: 'text',
recipient: {
id: webhook_event.sender.id
},
message: {
text: 'hello, world!'
}
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
});
exports.main = functions.https.onRequest(app);
在我的日志中显示的错误如下:
Error: getaddrinfo EAI_AGAIN graph.facebook.com:443
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:67:26)
errno: 'EAI_AGAIN',
code: 'EAI_AGAIN',
syscall: 'getaddrinfo',
hostname: 'graph.facebook.com',
host: 'graph.facebook.com',
port: 443,
config:
{ adapter: [Function: httpAdapter],
transformRequest: { '0': [Function: transformRequest] },
transformResponse: { '0': [Function: transformResponse] },
timeout: 0,
xsrfCookieName: 'XSRF-TOKEN',
xsrfHeaderName: 'X-XSRF-TOKEN',
maxContentLength: -1,
validateStatus: [Function: validateStatus],
headers:
{ Accept: 'application/json, text/plain, */*',
'Content-Type': 'application/json;charset=utf-8',
'User-Agent': 'axios/0.18.0',
'Content-Length': 98 },
method: 'post',
url: 'https://graph.facebook.com/v3.3/me/messages?access_token=<PAGE_ACCESS_TOKEN>',
data: '{"messaging_type":"text","recipient":{"id":"2251519781590874"},"message":{"text":"hello, world!"}}' },
request:
Writable {
_writableState:
WritableState {
objectMode: false,
highWaterMark: 16384,
finalCalled: false,
needDrain: false,
ending: false,
ended: false,
finished: false,
destroyed: false,
decodeStrings: true,
defaultEncoding: 'utf8',
length: 0,
writing: false,
corked: 0,
sync: true,
bufferProcessing: false,
onwrite: [Function: bound onwrite],
writecb: null,
writelen: 0,
bufferedRequest: null,
lastBufferedRequest: null,
pendingcb: 0,
prefinished: false,
errorEmitted: false,
bufferedRequestCount: 0,
corkedRequestsFree: [Object] },
writable: true,
domain:
Domain {
domain: null,
_events: [Object],
_eventsCount: 1,
_maxListeners: undefined,
members: [] },
_events:
{ response: [Function: handleResponse],
error: [Function: handleRequestError] },
_eventsCount: 2,
_maxListeners: undefined,
_options:
{ maxRedirects: 21,
maxBodyLength: 10485760,
protocol: 'https:',
path: '/v3.3/me/messages?access_token=<PAGE_ACCESS_TOKEN>',
method: 'post',
headers: [Object],
agent: undefined,
auth: undefined,
hostname: 'graph.facebook.com',
port: null,
nativeProtocols: [Object],
pathname: '/v3.3/me/messages',
search: '?access_token=<PAGE_ACCESS_TOKEN>' },
_ended: false,
_ending: true,
_redirectCount: 0,
_redirects: [],
_requestBodyLength: 98,
_requestBodyBuffers: [ [Object] ],
_onNativeResponse: [Function],
_currentRequest:
ClientRequest {
domain: [Object],
_events: [Object],
_eventsCount: 6,
_maxListeners: undefined,
output: [],
outputEncodings: [],
outputCallbacks: [],
outputSize: 0,
writable: true,
_last: true,
upgrading: false,
chunkedEncoding: false,
shouldKeepAlive: false,
useChunkedEncodingByDefault: true,
sendDate: false,
_removedConnection: false,
_removedContLen: false,
_removedTE: false,
_contentLength: null,
_hasBody: true,
_trailer: '',
finished: false,
_headerSent: true,
socket: [Object],
connection: [Object],
_header: 'POST /v3.3/me/messages?access_token=<PAGE_ACCESS_TOKEN> HTTP/1.1\r\nAccept: application/json, text/plain, */*\r\nContent-Type: application/json;charset=utf-8\r\nUser-Agent: axios/0.18.0\r\nContent-Length: 98\r\nHost: graph.facebook.com\r\nConnection: close\r\n\r\n',
_onPendingData: [Function: noopPendingOutput],
agent: [Object],
socketPath: undefined,
timeout: undefined,
method: 'POST',
path: '/v3.3/me/messages?access_token=<PAGE_ACCESS_TOKEN>',
_ended: false,
res: null,
aborted: undefined,
timeoutCb: null,
upgradeOrConnect: false,
parser: null,
maxHeadersCount:
到底是怎么回事?我从未见过像这样的错误。任何帮助是极大的赞赏。
注意:
答案 0 :(得分:1)
EAI_AGAIN是由网络超时引起的,并且与Messenger Platform API不相关。如果您处于免费(火花计划)层,则需要升级Firebase计划。