我正在尝试从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:


到底是怎么回事?我从未见过像这样的错误。任何帮助是极大的赞赏。

笔记:


我的页面已订阅我的应用程序。
Webhooks已成功建立。我的应用程序可以接收消息。

最佳答案

EAI_AGAIN是由网络超时引起的,与Messenger Platform API不相关。如果您处于免费(火花计划)层,则需要升级Firebase计划。

关于javascript - 尝试从Firebase功能使用Facebook Messenger平台发送消息时出错,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56192924/

10-09 09:28