facebook messaging | sync facebook threads | Get messages from facebook | Search

This code automates the process of collecting and managing URLs of Facebook message threads, storing them in a JSON file for later use. It utilizes web automation tools to interact with Facebook and handles both regular and archived threads.

Run example

npm run import -- "List Facebook threads"

List Facebook threads

var fs = require('fs');

var PROFILE_PATH = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE;
var project = PROFILE_PATH + '/Conversations';

function getFacebookThreads(threads) {
    return client
        .getAllUntil('//*[contains(@class, "scrollable")]',
                     '//*[contains(@data-href, "messages")]/@data-href',
                     threads,
                     (a, b) => a === b,
                     i => i < 20)
};

function listArchivedFacebook(threads) {
    return client
        .then(url => url.indexOf('/messages/archived') == -1
            ? client.url('https://www.facebook.com/messages/archived')
                .pause(1000)
                .click('.scrollable a[href="#"]')
            : Promise.resolve([]))
        .pause(2000)
        .then(() => getFacebookThreads(threads))
}

var threads, results;
function listFacebookThreads(threads, archived = false) {
    try {
        if(threads !== false) {
            threads = JSON.parse(fs.readFileSync(project + '/facebook-threads.json'));
        } else {
            threads = [];
        }
    }
    catch (e) {
        threads = [];
    }
    return client.getUrl()
        .then(url => url.indexOf('/messages/t') == -1
            ? client
                .click('[data-tooltip-content="Messages"]')
                .click('a[href*="/messages/t"]')
            : Promise.resolve([]))
        .pause(2000)
        .then(() => getFacebookThreads(threads))
        .then(r => threads = r)
        .then(() => archived ? listArchivedFacebook(threads) : threads)
        // list all message threads
        .then(r => {
            threads = r;
            fs.writeFileSync(
                project + '/facebook-threads.json',
                JSON.stringify(r, null, 4));
            return threads;
        })
        .catch(e => console.log(e))
};
module.exports = listFacebookThreads;
listFacebookThreads;

What the code could have been:

/**
 * @module listFacebookThreads
 * @description Lists Facebook message threads and saves them to a JSON file.
 * @author [Your Name]
 */

const fs = require('fs');
const { Client } = require('playwright'); // Ensure the browser client is properly defined
const { join } = require('path');

const PROFILE_PATH = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE;
const PROJECT_PATH = join(PROFILE_PATH, 'Conversations');
const FILE_PATH = join(PROJECT_PATH, 'facebook-threads.json');

/**
 * Retrieves Facebook threads.
 * @param {object} client - The browser client.
 * @param {array} threads - The array of threads to retrieve.
 * @returns {Promise} A Promise that resolves with the retrieved threads.
 */
async function getFacebookThreads(client, threads) {
  try {
    // Get all threads until the scrollable element is reached
    return client
     .getAllUntil('//*[contains(@class, "scrollable")]',
                   '//*[contains(@data-href, "messages")]/@data-href',
                   threads,
                   (a, b) => a === b,
                   i => i < 20);
  } catch (e) {
    console.error(e);
    return []; // Return an empty array if an error occurs
  }
}

/**
 * Lists archived Facebook threads.
 * @param {object} client - The browser client.
 * @param {array} threads - The array of threads to retrieve.
 * @returns {Promise} A Promise that resolves with the retrieved threads.
 */
async function listArchivedFacebook(client, threads) {
  try {
    // Check if the archived URL is not already loaded
    if (await client.url().then(url => url.indexOf('/messages/archived') === -1)) {
      // Navigate to the archived URL and click the scrollable element
      return client
       .url('https://www.facebook.com/messages/archived')
       .pause(1000)
       .click('.scrollable a[href="#"]');
    }
    return Promise.resolve([]); // Return an empty array if the archived URL is already loaded
  } catch (e) {
    console.error(e);
    return []; // Return an empty array if an error occurs
  }
}

/**
 * Lists Facebook threads.
 * @param {array} threads - The array of threads to retrieve.
 * @param {boolean} [archived=false] - Whether to list archived threads.
 * @returns {Promise} A Promise that resolves with the retrieved threads.
 */
async function listFacebookThreads(threads = [], archived = false) {
  try {
    // Load the threads from the JSON file if it exists
    if (fs.existsSync(FILE_PATH)) {
      threads = JSON.parse(fs.readFileSync(FILE_PATH));
    }
  } catch (e) {
    console.error(e);
    threads = [];
  }

  try {
    // Get the current URL and click the Messages button if necessary
    const client = await new Client().launch();
    const url = await client.getUrl();
    if (url.indexOf('/messages/t') === -1) {
      await client
       .click('[data-tooltip-content="Messages"]')
       .click('a[href*="/messages/t"]');
    }

    // Get the Facebook threads
    const facebookThreads = await getFacebookThreads(client, threads);

    // Add the retrieved threads to the array
    threads.push(...facebookThreads);

    // List the archived threads if necessary
    if (archived) {
      const archivedThreads = await listArchivedFacebook(client, threads);
      threads.push(...archivedThreads);
    }

    // Save the threads to the JSON file
    fs.writeFileSync(FILE_PATH, JSON.stringify(threads, null, 4));

    return threads;
  } catch (e) {
    console.error(e);
    return [];
  } finally {
    await (client || {}).close();
  }
}

module.exports = listFacebookThreads;

This code snippet is designed to scrape and manage Facebook message threads.

Here's a breakdown:

  1. Setup:

  2. Helper Functions:

  3. Main Function:

  4. Export:

Key Points:

Let me know if you have any other questions.