facebook messaging | get unread threads facebook | sync facebook threads | Search

This code automates Facebook message responses by identifying messages containing a specific command pattern ("megamind [command]"), processing the commands, and sending automated replies. It uses a custom module to interact with Facebook and manage the command processing and response generation.

Run example

npm run import -- "scan commands facebook"

scan commands facebook

var importer = require('../Core');
var runSeleniumCell = importer.import("selenium cell");
var {
    filterCommand,
    storeResult
} = importer.import("filter command permission",
"store rpc result");

var getUnreadThreads, sendFacebookMessage;
function scanCommandsFacebook() {
    return runSeleniumCell([
        'unread threads facebook',
        'send facebook message',
    ])
        .then(r => {
            getUnreadThreads = r.getUnreadThreads;
            sendFacebookMessage = r.sendFacebookMessage;
            return getUnreadThreads();
        })
        .then(friends => {
            const hasCommands = friends.map(f => Object.assign(f, {
                messages: f.messages
                    .filter(m => m.from !== 'Brian' && (/megamind\s*(.*)/ig).exec(m.message))
            })).filter(f => f.messages.length > 0).map(f => Object.assign(f, {
                name: f.participants[0].name.split('\n')[0],
                command: (/megamind\s*(.*)/ig).exec(f.messages[0].message)[1].trim(),
                date: f.messages[0].time,
                id: 'Facebook: ' + f.thread.replace(/^\/|\/$/ig, '').split('/').pop()
                    + f.messages[0].time
            }));
            return importer.runAllPromises(hasCommands.map(f => resolve => {
                return filterCommand(f.command, f.date, f.id, f.name)
                    .then(props => resolve(Object.assign(f, props)));
            }))
        })
        // generate and send responses
        .then(friends => {
            return importer.runAllPromises(friends.map(f => resolve => {
                return storeResult(f)
                    .then(response => sendFacebookMessage(JSON.stringify(response, null, 4) + '\n Mm\n', f.thread))
                    .catch(e => console.log(e))
                    .then(r => resolve(r))
            }))
        })
        .catch(e => console.log(e))
}
module.exports = scanCommandsFacebook;

if(typeof $ !== 'undefined') {
    $.async();
    scanCommandsFacebook()
        .then(r => $.sendResult(r))
        .catch((e) => $.sendError(e))
}

What the code could have been:

// Import required modules
const importer = require('../Core');
const { runSeleniumCell, filterCommand, storeResult } = importer.import([
   'selenium cell',
    'filter command permission',
   'store rpc result',
]);

// Function to scan Facebook commands
/**
 * Scan Facebook commands and send responses.
 * 
 * @return {Promise} A promise that resolves with the result.
 */
function scanCommandsFacebook() {
    // Define functions to get unread threads and send Facebook message
    let getUnreadThreads, sendFacebookMessage;

    // Run Selenium cell to retrieve functions
    return runSeleniumCell([
        'unread threads facebook',
       'send facebook message',
    ])
       .then(response => {
            // Get functions from response
            getUnreadThreads = response.getUnreadThreads;
            sendFacebookMessage = response.sendFacebookMessage;
            return getUnreadThreads();
        })
        // Get unread threads
       .then(friends => friends)
        // Filter friends with messages matching the pattern
       .then(friends => friends.filter(friend => {
            const messages = friend.messages.filter(message => {
                return message.from!== 'Brian' && (/megamind\s*(.*)/ig).exec(message.message);
            });
            return messages.length > 0;
        }))
        // Map friends to objects with name, command, date, and id
       .then(friends => friends.map(friend => ({
            name: friend.participants[0].name.split('\n')[0],
            command: (/megamind\s*(.*)/ig).exec(friend.messages[0].message)[1].trim(),
            date: friend.messages[0].time,
            id: 'Facebook:'+ friend.thread.replace(/^\/|\/$/ig, '').split('/').pop() + friend.messages[0].time,
        }))
        // Filter friends with valid command
       .then(friends => friends.filter(friend =>!!friend.command))
        // Filter commands
       .then(friends => Promise.all(friends.map(friend => filterCommand(friend.command, friend.date, friend.id, friend.name))))
       .then(props => friends.map((friend, index) => ({...friend,...props[index] })))
        // Store results and send responses
       .then(friends => runSeleniumCell([
           'store rpc result',
        ])
           .then(response => storeResult(response))
           .then(response => {
                return Promise.all(friends.map((friend, index) => {
                    return storeResult(response)
                       .then(() => sendFacebookMessage(JSON.stringify(response, null, 4) + '\n Mm\n', friend.thread))
                       .catch(e => console.log(e))
                       .then(() => response);
                }));
            }))
       .catch(e => console.log(e));
}

// Export function
module.exports = scanCommandsFacebook;

// Send result to GUI
if (typeof $!== 'undefined') {
    $.async();
    scanCommandsFacebook()
       .then(r => $.sendResult(r))
       .catch((e) => $.sendError(e));
}

This code snippet is designed to scan Facebook messages for specific commands, filter them, and generate automated responses.

Here's a breakdown:

  1. Setup: It imports necessary modules for interacting with Facebook, running commands, and storing results.

  2. scanCommandsFacebook Function:

  3. Export: The scanCommandsFacebook function is exported, making it callable from other parts of the application.