Schmavery / facebook-chat-api

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

Getting error while uploading attachment as a page #133

Open smithkre opened 8 years ago

smithkre commented 8 years ago

Can't upload image by using

var msg = {
   attachment: fs.createReadStream(__dirname + '/kitty.jpg')
};

localAPI.sendMessage(msg, "0000000000", function callback(obj, other) {
});

Error message on

ERR! ERROR in sendMessage --> { error: 'JSON.parse error. Check the detail property on this error.', ERR! ERROR in sendMessage --> detail: [SyntaxError: Unexpected end of input], ERR! ERROR in sendMessage --> res: '' }

In sendMessage.js defaultFuncs .post("https://www.facebook.com/ajax/mercury/send_messages.php", ctx.jar, form) .then(utils.parseAndCheckLogin)

If I send message. It call utils.parseAndCheckLogin 1 time But when I upload an attachment. Error happen.

Thank you :D

bsansouci commented 8 years ago

Hey! This looks similar to #125. Can you check if you can upload that jpg manually on FB chat? Can you print the whole stack trace too?

Schmavery commented 8 years ago

@smithkre I'm assuming that the file you named kitty.jpg actually exists in that directory and is actually a jpg. If not, that will probably cause this error. @bsansouci I'd imagine this is the same issue as #125 as the tests do check image upload. That said, I just looked and we're uploading a png in the test rather than a jpg. Thanks for your latest commit 90a06ed that checks the response code before attempting to parse (though we might want to talk about a more helpful error message there, as people will hit it a fair bit).

bsansouci commented 8 years ago

@smithkre Can you pull and check to see if that works now?

smithkre commented 8 years ago

Thanks for an update. I got this error.

[Error: parseAndCheckLogin got status code: 500. Bailing out of trying to parse response.]

This is my code.

if(object.attachment) {
        //var uuid1 = uuid.v1();
        //var file_name = uuid1 + '.png';
        //var file = fs.createWriteStream(file_name);
        //http.get(object.attachment, function(response) {
        //    response.pipe(file);
            object.attachment = fs.createReadStream(__dirname + '/img.jpg');

            console.log(object);

            api.sendMessage(object, threadId, function callback(obj, messageInfo) {
                if(obj) {
                    console.error(obj);
                    res.json({complete: false});
                }
                else {
                    res.json({complete: true, info: messageInfo});
                }
            });
        //});

My img.jpg can upload manually.

I don't know which step got an error. As I walk though the source code. Image upload to facebook server is not wrong. I got an image url and object id from facebook. But It's error when try to send all message to facebook server.

Schmavery commented 8 years ago

Hey @smithkre, we are having a hard time reproing this issue. Can you attach the image you're trying to upload?

smithkre commented 8 years ago

Thank you @Schmavery

This is stack trace

ERR! Error in uploadAttachment Error: parseAndCheckLogin got status code: 500. Bailing out of trying to parse response. ERR! Error in uploadAttachment at /Users/Smith/Http/ec-helper/src/chat/engine/node_modules/facebook-chat-api/utils.js:396:40 ERR! Error in uploadAttachment at tryCatcher (/Users/Smith/Http/ec-helper/src/chat/engine/node_modules/bluebird/js/main/util.js:26:23) ERR! Error in uploadAttachment at Function.Promise.attempt.Promise.try (/Users/Smith/Http/ec-helper/src/chat/engine/node_modules/bluebird/js/main/method.js:31:24) ERR! Error in uploadAttachment at parseAndCheckLogin (/Users/Smith/Http/ec-helper/src/chat/engine/node_modules/facebook-chat-api/utils.js:395:22) ERR! Error in uploadAttachment at tryCatcher (/Users/Smith/Http/ec-helper/src/chat/engine/node_modules/bluebird/js/main/util.js:26:23) ERR! Error in uploadAttachment at Promise._settlePromiseFromHandler (/Users/Smith/Http/ec-helper/src/chat/engine/node_modules/bluebird/js/main/promise.js:507:31) ERR! Error in uploadAttachment at Promise._settlePromiseAt (/Users/Smith/Http/ec-helper/src/chat/engine/node_modules/bluebird/js/main/promise.js:581:18) ERR! Error in uploadAttachment at Promise._settlePromises (/Users/Smith/Http/ec-helper/src/chat/engine/node_modules/bluebird/js/main/promise.js:697:14) ERR! Error in uploadAttachment at Async._drainQueue (/Users/Smith/Http/ec-helper/src/chat/engine/node_modules/bluebird/js/main/async.js:123:16) ERR! Error in uploadAttachment at Async._drainQueues (/Users/Smith/Http/ec-helper/src/chat/engine/node_modules/bluebird/js/main/async.js:133:10) ERR! Error in uploadAttachment at Immediate.Async.drainQueues as _onImmediate ERR! Error in uploadAttachment at processImmediate as _immediateCallback ERR! Error in uploadAttachment [Error: parseAndCheckLogin got status code: 500. Bailing out of trying to parse response.]

I chang to .png img.png.zip

bsansouci commented 8 years ago

Can you turn on verbose logging, try again and show us the logs. There should be a log starting with "parseAndCheckLogin: " containing the body of the response.

smithkre commented 8 years ago

@bsansouci, No logs show on screen. It's only has error messages.

I inspected in chrome while uploading an image. First time it's always get 400 error Then messenger request with OPTIONS method and header

Access-Control-Request-Method: POST Access-Control-Request-Headers: content-type

screen shot 2559-01-12 at 10 35 06 am

After I did this I try to POST data again I got 408 Error

defaultFuncs
        .options("https://upload.facebook.com/ajax/mercury/upload.php", ctx.jar, {})
        .then(function (data) {

          console.log(data.headers);
          defaultFuncs
            .postFormData("https://upload.facebook.com/ajax/mercury/upload.php", ctx.jar, form, {})
            //.then(utils.parseAndCheckLogin)
            .then(function(resData){
              console.log(resData);
              //if (resData.error) {
              //  throw resData;
              //}
              //
              //return resData.payload.metadata[0];
            });
        });

I add options method (by copy get method in utils.js).

PS. I am very new to javascript and nodejs.

bsansouci commented 8 years ago

Could you add console.log(data.body) here and copy paste the result here? I'm not sure OPTIONS is the right kind of request to be made here. In the end, to send the image you need to make a POST request for sure. @Schmavery any ideas?

smithkre commented 8 years ago

OPTIONS does not return message.

Do you don't get error while upload right?

My options look like this.

var options = {
                    pageID: activePageId,
                    listenEvents: true,
                    selfListen: true,
                    logLevel: "verbose"
                };

I will try to figure out again.

Schmavery commented 8 years ago

Ahh @bsansouci pretty sure our tests don't cover login as page.

sauravtom commented 8 years ago

I am also getting the same error, here's the verbose stack trace

ERR! ERROR in sendMessage -->  Error: parseAndCheckLogin got status code: 500. Bailing out of trying to parse response.
ERR! ERROR in sendMessage -->      at /Users/saurav/Desktop/jalebi/node_modules/facebook-chat-api/utils.js:396:40
ERR! ERROR in sendMessage -->      at tryCatcher (/Users/saurav/Desktop/jalebi/node_modules/facebook-chat-api/node_modules/bluebird/js/main/util.js:26:23)
ERR! ERROR in sendMessage -->      at Function.Promise.attempt.Promise.try (/Users/saurav/Desktop/jalebi/node_modules/facebook-chat-api/node_modules/bluebird/js/main/method.js:31:24)
ERR! ERROR in sendMessage -->      at parseAndCheckLogin (/Users/saurav/Desktop/jalebi/node_modules/facebook-chat-api/utils.js:395:22)
ERR! ERROR in sendMessage -->      at tryCatcher (/Users/saurav/Desktop/jalebi/node_modules/facebook-chat-api/node_modules/bluebird/js/main/util.js:26:23)
ERR! ERROR in sendMessage -->      at Promise._settlePromiseFromHandler (/Users/saurav/Desktop/jalebi/node_modules/facebook-chat-api/node_modules/bluebird/js/main/promise.js:507:31)
ERR! ERROR in sendMessage -->      at Promise._settlePromiseAt (/Users/saurav/Desktop/jalebi/node_modules/facebook-chat-api/node_modules/bluebird/js/main/promise.js:581:18)
ERR! ERROR in sendMessage -->      at Promise._settlePromises (/Users/saurav/Desktop/jalebi/node_modules/facebook-chat-api/node_modules/bluebird/js/main/promise.js:697:14)
ERR! ERROR in sendMessage -->      at Async._drainQueue (/Users/saurav/Desktop/jalebi/node_modules/facebook-chat-api/node_modules/bluebird/js/main/async.js:123:16)
ERR! ERROR in sendMessage -->      at Async._drainQueues (/Users/saurav/Desktop/jalebi/node_modules/facebook-chat-api/node_modules/bluebird/js/main/async.js:133:10)
ERR! ERROR in sendMessage -->      at Immediate.Async.drainQueues [as _onImmediate] (/Users/saurav/Desktop/jalebi/node_modules/facebook-chat-api/node_modules/bluebird/js/main/async.js:15:14)
ERR! ERROR in sendMessage -->      at processImmediate [as _immediateCallback] (timers.js:358:17)
ERR! ERROR in sendMessage -->   [Error: parseAndCheckLogin got status code: 500. Bailing out of trying to parse response.]

I am able to upload the same file (image.png) through messenger on chrome.

Schmavery commented 8 years ago

@sauravtom are you also using our api as a page?

sauravtom commented 8 years ago

@Schmavery , yes I am using as a page.

P.S I just tried it without page, and it is working. But not able to get it working when using the API as page.

Schmavery commented 8 years ago

Ok great, I think it's pretty clear now that we fixed this issue for regular messages but not pages. Thanks.

Schmavery commented 8 years ago

We've done some fairly extensive looking into fixing this with no promising results yet, unfortunately.

andrecordeiro commented 8 years ago

Hy @Schmavery , I'm the same mistake:

my facebook account works in my client's not page.

[Error: parseAndCheckLogin got status code: 500. Bailing out of trying to parse response.]

Schmavery commented 8 years ago

Yes, something is still wrong with this request, but so far, we are unable to identify what is causing this.

andrecordeiro commented 8 years ago

No problems. thank you my friend!