Schmavery / facebook-chat-api

Unofficial Facebook Chat API for Nodejs
MIT License
1.93k stars 596 forks source link

Error: parseAndCheckLogin got status code: 408 - When trying to upload gif #353

Closed marcelvanworkum closed 7 years ago

marcelvanworkum commented 7 years ago

Trying to replicate Slack's random Giphy integration for my personal chatbot. I am grabbing a gif from their api, downloading that locally, and then trying to send that using your api.

Here is the code I am using to do this:

var file = fs.createWriteStream("meme.gif");
var targetUrl = ...;
var gifRequest = http.get(targetUrl, function(gifResponse) {
  gifResponse.pipe(file);

  var message = {
    body: "Here's that fresh meme ya ordered!",
    attachment: fs.createReadStream(__dirname + '/meme.gif')
  }

  api.sendMessage(message, threadID);
});

This results in the following error: (Note I have added the console.log(data.body); to the utils.js as mentioned in #152

WARN parseAndCheckLogin: Got status code 500 retrying...
for (;;); {"t":"heartbeat"}

info Got answer in  50416
<!DOCTYPE html>
<html lang="en" id="facebook">
  <head>
    <title>Facebook | Error</title>
    <meta charset="utf-8">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="cache-control" content="no-store">
    <meta http-equiv="cache-control" content="max-age=0">
    <meta http-equiv="expires" content="-1">
    <meta http-equiv="pragma" content="no-cache">
    <meta name="robots" content="noindex,nofollow">
    <style>
      html, body {
        color: #141823;
        background-color: #e9eaed;
        font-family: Helvetica, Lucida Grande, Arial,
                     Tahoma, Verdana, sans-serif;
        margin: 0;
        padding: 0;
        text-align: center;
      }

      #header {
        height: 30px;
        padding-bottom: 10px;
        padding-top: 10px;
        text-align: center;
      }

      #icon {
        width: 30px;
      }

      h1 {
        font-size: 18px;
      }

      p {
        font-size: 13px;
      }

      #footer {
        border-top: 1px solid #ddd;
        color: #9197a3;
        font-size: 12px;
        padding: 5px 8px 6px 0;
      }
    </style>
  </head>
  <body>
    <div id="header">
      <a href="//www.facebook.com/">
        <img id="icon" src="//static.xx.fbcdn.net/images/logos/facebook_2x.png" />
      </a>
    </div>
    <div id="core">
      <h1 id="sorry">Sorry, something went wrong.</h1>
      <p id="promise">
        We're working on it and we'll get it fixed as soon as we can.
      </p>
      <p id="back-link">
        <a id="back" href="//www.facebook.com/">Go Back</a>
      </p>
      <div id="footer">
        Facebook
        <span id="copyright">
          &copy; 2016
        </span>
        <span id="help-link">
          &#183;
          <a id="help" href="//www.facebook.com/help/">Help Center</a>
        </span>
      </div>
    </div>
    <script>
      document.getElementById('back').onclick = function() {
        if (history.length > 1) {
          history.back();
          return false;
        }
      };

      // Adjust the display based on the window size
      if (window.innerHeight < 80 || window.innerWidth < 80) {
        // Blank if window is too small
        document.body.style.display = 'none';
      };
      if (window.innerWidth < 200 || window.innerHeight < 150) {
        document.getElementById('back-link').style.display = 'none';
        document.getElementById('help-link').style.display = 'none';
      };
      if (window.innerWidth < 200) {
        document.getElementById('sorry').style.fontSize = '16px';
      };
      if (window.innerWidth < 150) {
        document.getElementById('promise').style.display = 'none';
      };
      if (window.innerHeight < 150) {
        document.getElementById('sorry').style.margin = '4px 0 0 0';
        document.getElementById('sorry').style.fontSize = '14px';
        document.getElementById('promise').style.display = 'none';
      };
    </script>
  </body>
</html>

ERR! Error in uploadAttachment Error: parseAndCheckLogin got status code: 408. Bailing out of trying to parse response.
ERR! Error in uploadAttachment     at /Users/marcel/Development/facebook-chat-bot/node_modules/facebook-chat-api/utils.js:539:42
ERR! Error in uploadAttachment     at tryCatcher (/Users/marcel/Development/facebook-chat-bot/node_modules/bluebird/js/main/util.js:26:23)
ERR! Error in uploadAttachment     at Function.Promise.attempt.Promise.try (/Users/marcel/Development/facebook-chat-bot/node_modules/bluebird/js/main/method.js:31:24)
ERR! Error in uploadAttachment     at /Users/marcel/Development/facebook-chat-bot/node_modules/facebook-chat-api/utils.js:523:24
ERR! Error in uploadAttachment     at tryCatcher (/Users/marcel/Development/facebook-chat-bot/node_modules/bluebird/js/main/util.js:26:23)
ERR! Error in uploadAttachment     at Promise._settlePromiseFromHandler (/Users/marcel/Development/facebook-chat-bot/node_modules/bluebird/js/main/promise.js:510:31)
ERR! Error in uploadAttachment     at Promise._settlePromiseAt (/Users/marcel/Development/facebook-chat-bot/node_modules/bluebird/js/main/promise.js:584:18)
ERR! Error in uploadAttachment     at Promise._settlePromises (/Users/marcel/Development/facebook-chat-bot/node_modules/bluebird/js/main/promise.js:700:14)
ERR! Error in uploadAttachment     at Async._drainQueue (/Users/marcel/Development/facebook-chat-bot/node_modules/bluebird/js/main/async.js:123:16)
ERR! Error in uploadAttachment     at Async._drainQueues (/Users/marcel/Development/facebook-chat-bot/node_modules/bluebird/js/main/async.js:133:10)
ERR! Error in uploadAttachment     at Immediate.Async.drainQueues (/Users/marcel/Development/facebook-chat-bot/node_modules/bluebird/js/main/async.js:15:14)
ERR! Error in uploadAttachment     at runCallback (timers.js:574:20)
ERR! Error in uploadAttachment     at tryOnImmediate (timers.js:554:5)
ERR! Error in uploadAttachment     at processImmediate [as _immediateCallback] (timers.js:533:5)
ERR! Error in uploadAttachment  Error: parseAndCheckLogin got status code: 408. Bailing out of trying to parse response.
ERR! Error in uploadAttachment     at /Users/marcel/Development/facebook-chat-bot/node_modules/facebook-chat-api/utils.js:539:42
ERR! Error in uploadAttachment     at tryCatcher (/Users/marcel/Development/facebook-chat-bot/node_modules/bluebird/js/main/util.js:26:23)
ERR! Error in uploadAttachment     at Function.Promise.attempt.Promise.try (/Users/marcel/Development/facebook-chat-bot/node_modules/bluebird/js/main/method.js:31:24)
ERR! Error in uploadAttachment     at /Users/marcel/Development/facebook-chat-bot/node_modules/facebook-chat-api/utils.js:523:24
ERR! Error in uploadAttachment     at tryCatcher (/Users/marcel/Development/facebook-chat-bot/node_modules/bluebird/js/main/util.js:26:23)
ERR! Error in uploadAttachment     at Promise._settlePromiseFromHandler (/Users/marcel/Development/facebook-chat-bot/node_modules/bluebird/js/main/promise.js:510:31)
ERR! Error in uploadAttachment     at Promise._settlePromiseAt (/Users/marcel/Development/facebook-chat-bot/node_modules/bluebird/js/main/promise.js:584:18)
ERR! Error in uploadAttachment     at Promise._settlePromises (/Users/marcel/Development/facebook-chat-bot/node_modules/bluebird/js/main/promise.js:700:14)
ERR! Error in uploadAttachment     at Async._drainQueue (/Users/marcel/Development/facebook-chat-bot/node_modules/bluebird/js/main/async.js:123:16)
ERR! Error in uploadAttachment     at Async._drainQueues (/Users/marcel/Development/facebook-chat-bot/node_modules/bluebird/js/main/async.js:133:10)
ERR! Error in uploadAttachment     at Immediate.Async.drainQueues (/Users/marcel/Development/facebook-chat-bot/node_modules/bluebird/js/main/async.js:15:14)
ERR! Error in uploadAttachment     at runCallback (timers.js:574:20)
ERR! Error in uploadAttachment     at tryOnImmediate (timers.js:554:5)
ERR! Error in uploadAttachment     at processImmediate [as _immediateCallback] (timers.js:533:5)
for (;;); {"t":"heartbeat"}

I can upload the gif fine using messenger.com.

Also, if I change attachment: fs.createReadStream(__dirname + '/meme.gif') to instead send '/meme.jpg' with a valid meme.jpg in that same directory it works perfectly.

Is this an issue with the way the api handles gifs, or perhaps am I creating the read stream incorrectly?

Any insight would be greatly appreciated.

Schmavery commented 7 years ago

Hmm, as you can see with #152, we've had some unresolved issues with gifs in the past. Unfortunately the error facebook returns doesn't give us much indication as to the nature of the problem.

In your other situation, you're putting a hardcoded meme.jpg in the dirname folder and running `attachment: fs.createReadStream(dirname + '/meme.gif')` and it's working?

It strikes me that your read stream and your write stream don't have their locations specified the same way ("meme.gif") vs (__dirname + '/meme.gif'). Not sure why (though I'd hope that wouldn't be a problem)?

I suggested a workaround in #347 that you might find interesting. @ivkos seems to be trying to solve a fairly similar problem, maybe we can pool resources.

Schmavery commented 7 years ago

Any luck with this issue?

bsansouci commented 7 years ago

Hey I just got that to work using the code below. I think the issue was using a writeStream instead of simply the response from the get request which is a readstream.

  var targetUrl = "http://media.giphy.com/media/JhncGNdBoyeKk/giphy.gif";
  var gifRequest = require('http').get(targetUrl, function(gifResponse) {
    var message = {
      body: "Here's that fresh meme ya ordered!",
      attachment: gifResponse
    }

    api.sendMessage(message, threadID);
  });

That's just another way to do it without saving to disk. It seems like waiting for the writeStream to be done works too:

  var file = fs.createWriteStream("meme.gif");
  var targetUrl = "http://media.giphy.com/media/JhncGNdBoyeKk/giphy.gif";
  var gifRequest = require('http').get(targetUrl, function(gifResponse) {
    gifResponse.pipe(file);
    file.on('finish', function() {
        var message = {
          body: "Here's that fresh meme ya ordered!",
          attachment: fs.createReadStream(__dirname + '/meme.gif')
        }

        api.sendMessage(message, threadID);
    });
  });

Since this works I'm closing this issue. Feel free to re-open if you have any other issue.