This code automates the process of sending messages on Facebook, handling thread navigation, multi-line message formatting, and error handling using web automation techniques.
npm run import -- "send facebook message"
function sendFacebookMessage(message, thread) {
var result;
if(typeof thread !== 'undefined') {
result = client.getUrl()
.then(url => url.indexOf(thread) > -1
? []
: client.url(thread));
} else {
result = client;
}
const parts = message.split('\n');
return result
.click('//*[contains(@aria-label, "Type a message")]')
.keys('Control')
.keys('a')
.keys('NULL')
.keys('Delete')
.then(() => importer.runAllPromises(parts.map((t, i) => resolve => {
if(i === parts.length - 1) {
return client.keys(t).then(() => resolve());
}
return client
.keys(t)
.keys('Shift')
.keys('Enter')
.keys('NULL')
.catch(e => console.log(e))
.then(() => resolve());
})))
.keys('\uE007')
.catch(e => console.log(e))
}
module.exports = sendFacebookMessage;
/**
* Sends a Facebook message with the given text, optionally to a specific thread.
*
* @param {string} message - The message to send.
* @param {string} [thread] - The ID of the thread to send the message in.
* @returns {Promise} A promise that resolves when the message has been sent.
*/
async function sendFacebookMessage(message, thread = null) {
try {
// Get the client object, or throw an error if it's not available
const client = client.getUrl();
if (thread) {
// If a thread ID is specified, navigate to it
if (!client.url(thread).then(url => url.indexOf(thread) > -1)) {
throw new Error(`Thread not found: ${thread}`);
}
}
// Split the message into parts
const parts = message.split('\n');
// Send each part of the message
await Promise.all(parts.map((t, i) => {
// If it's the last part, just send it
if (i === parts.length - 1) {
return client.keys(t);
}
// Otherwise, send it and then press enter
return client.keys(t).keys('Shift').keys('Enter').keys('NULL');
}));
// Send the final part and press enter
await client.keys(parts[parts.length - 1]).keys('Shift').keys('Enter').keys('NULL');
// Press enter to send the message
await client.keys('\uE007');
} catch (error) {
// Log any errors that occur
console.error(error);
}
}
module.exports = sendFacebookMessage;
This code snippet defines a function sendFacebookMessage
that automates the process of sending a message on Facebook.
Here's a breakdown:
Initialization:
message
(the text to send) and thread
(the URL of the thread to send it to).result
variable to handle navigation to the correct thread.Thread Navigation:
thread
URL is provided, it checks if the current URL is already on that thread. If not, it navigates to the specified thread using Selenium-like commands.thread
is provided, it assumes the current context is already in a valid thread.Message Formatting and Sending:
message
into lines using \n
as a delimiter.Control
+ a
selects all existing text (if any).NULL
simulates deleting the selected text.keys()
.Shift
+ Enter
to create a new line, and then simulates deleting the extra newline character.Sending the Message:
\uE007
).Error Handling:
catch(e => console.log(e))
to handle any errors during the process.Export:
sendFacebookMessage
function is exported as a module, allowing it to be used in other parts of the application.Key Points:
Let me know if you have any other questions.