hong4rc / node-facebook

Facebook simulator web browser
MIT License
10 stars 4 forks source link

Ability to access thread messages. #7

Open 23phy opened 5 years ago

23phy commented 5 years ago

Hello.

First of all, cool work 😁 I'm no longer frustrated with wrappers and promisifying things for facebook-chat-api.

Description It wouldn't be a complete facebook api if there isn't any way of pulling the messages of a thread. It could be like api.getThreadMessages() or even like in facebook-chat-api: api.getThreadHistory()

hong4rc commented 5 years ago

I am creating loader for graphqlbatch api, it can create some method for api (not just getThreadHistory) Please wait next version.

Thank you for your consideration.

23phy commented 5 years ago

Any updates?

hong4rc commented 5 years ago

Any updates?

Thank for your remind, I will do it this weekend(busy for now) and mention you.

I re-write this module (old version has problem with login multi-user)

Wait for my good news.

hong4rc commented 5 years ago

Small update add getThreadList and getThreadMessage.

Can you give a review of that?

23phy commented 5 years ago

Yep, both getThreadList and getThreadMessage work as expected... but I've seen a weird error just before my account was blocked for suspicious activity and I was forced to change my password.

This is the code used:

const fs = require('fs');
const dotenv = require('dotenv');
const Facebook = require('node-facebook');

dotenv.config();

const me = new Facebook({
  state: JSON.parse(fs.readFileSync('state.json', 'utf8'))
  // email: process.env.FB_USERNAME,
  // pass: process.env.FB_PASSWORD
});

me.login()
  .then(async (api) => {
    try {
      const threadList = await api.getThreadList(10, null, ["INBOX"]);
      threadList.forEach(async (thread) => {
        fs.writeFileSync(`threads/${thread.allParticipants[0].name}.json`, JSON.stringify(thread, null, '\t'));
        try {
          const messages = await api.getThreadMessages(thread.threadId, 50);
          fs.writeFileSync(`messages/${thread.allParticipants[0].name}.json`, JSON.stringify(messages, null, '\t'));
        } catch (error) {
          console.log(error);
        }
      });
    } catch (error) {
      console.log(e);
    }
    // fs.writeFileSync('state.json', JSON.stringify(api.getState()));
  })

And this is the "weird" error:

zephi@Arc MINGW64 /C/Dev/Projects/NodePlayground/node-facebook-test
$ npm start

> node-facebook-test@1.0.0 start C:\Dev\Projects\NodePlayground\node-facebook-test
> node index.js

(node:16928) UnhandledPromiseRejectionWarning: Error: Not found cookie with name c_user
    at e.getCookie (C:\Dev\Projects\NodePlayground\node-facebook-test\node_modules\node-facebook\dist\utils\browser.js:1:4440)
    at new i (C:\Dev\Projects\NodePlayground\node-facebook-test\node_modules\node-facebook\dist\api.js:1:8105)
    at t.<anonymous> (C:\Dev\Projects\NodePlayground\node-facebook-test\node_modules\node-facebook\dist\facebook.js:1:2417)
    at C:\Dev\Projects\NodePlayground\node-facebook-test\node_modules\node-facebook\dist\facebook.js:1:1352
    at Object.next (C:\Dev\Projects\NodePlayground\node-facebook-test\node_modules\node-facebook\dist\facebook.js:1:1457)
    at r (C:\Dev\Projects\NodePlayground\node-facebook-test\node_modules\node-facebook\dist\facebook.js:1:193)
    at process._tickCallback (internal/process/next_tick.js:68:7)
(node:16928) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:16928) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js
process with a non-zero exit code.

Yes, I am very aware that it's an UnhandledPromiseRejectionWarning because I haven't catched it but the error message: Error: Not found cookie with name c_user seems weird to me.

The first time I logged in I used my dotenv username and password, and saved my state to a file, and after that I used my state to login.

hong4rc commented 5 years ago

Error: Not found cookie with name c_user seems weird to me

Maybe that cookie was blocked and Facebook send set-cookie c_user=deleted, it will delete your c_user

I will try to fix error account was blocked for suspicious activity