Ad

Error Trying To Send Messages Using Facebook Messenger Platform From A Firebase Function

I'm trying to send messages using the Messenger Platform from my firebase function. Reference: https://developers.facebook.com/docs/messenger-platform/send-messages/.

Anyway, the part of my function which handles this is the following:

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);

The error thats being printed in my logs is the following:

 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: 

What exactly is going on? I have never seen an error that looks like this. Any help is greatly appreciated.

Notes:

  • My page is subscribed to my app.
  • Webhooks have been set up successfully. My app can receive messages.
Ad

Answer

EAI_AGAIN is caused by a network timeout and not related to the Messenger Platform API. If you are on Free (Spark Plan) tier, you need to upgrade your firebase plan.

Ad
source: stackoverflow.com
Ad