Unofficial Facebook Chat API for Node.js - Interact with Facebook Messenger programmatically for ST-BOT
Enhanced & Maintained by ST | Sheikh Tamim
- β¨ Enhanced MQTT connection logging
- π Auto-reconnect with configurable intervals
- π Better connection status indicators
- π¨ Improved console output with colors
- π Enhanced security and stability
- π Automatic update checking and installation
- π‘ Better error handling and debugging
npm install stfcaOr with yarn:
yarn add stfcaST-FCA includes an automatic update system that keeps your package up-to-date seamlessly:
- π Automatic Check: Checks for updates when you start your bot
- π Shows Changes: Displays recent changelog updates
- π¦ NPM Update: Runs
npm install stfca@latestautomatically - π Auto-Restart: Restarts your bot to apply changes
If you're using ST-FCA in your bot project (like ST-BOT), the package will:
- β Detect when a new version is available
- β Automatically update to the latest version via npm
- β
Update your
node_modules/stfcafolder - β Restart your bot with the new version
You can also update manually:
npm install stfca@latestOr check for updates programmatically:
const { checkForFCAUpdate } = require('stfca/checkUpdate.js');
await checkForFCAUpdate();The auto-update system will:
- Show the current and latest versions
- Display recent changes from the changelog
- Inform you when the update is complete
- Automatically restart your application
Note: Updates are non-blocking and won't interrupt your bot's startup if the update check fails.
We are not responsible if your account gets banned for spammy activities such as:
- Sending lots of messages to people you don't know
- Sending messages very quickly
- Sending spammy looking URLs
- Logging in and out very quickly
Recommendation: Use Firefox browser or this website to reduce logout issues, especially for iOS users.
Support: If you encounter errors, contact us here
Facebook now has an official API for chat bots, however it's only available for Facebook Pages.
stfca is the only API that allows you to automate chat functionalities on a user account by emulating the browser. This means:
- Making the exact same GET/POST requests as a browser
- Does not work with auth tokens
- Requires Facebook account credentials (email/password) or AppState
npm install stfca@latestconst login = require("stfca");
login({ appState: [] }, (err, api) => {
if (err) return console.error(err);
api.listenMqtt((err, event) => {
if (err) return console.error(err);
// Echo back the received message
api.sendMessage(event.body, event.threadID);
});
});const login = require("stfca");
login({ appState: [] }, (err, api) => {
if (err) {
console.error("Login Error:", err);
return;
}
let yourID = "000000000000000"; // Replace with actual Facebook ID
let msg = "Hey!";
api.sendMessage(msg, yourID, err => {
if (err) console.error("Message Sending Error:", err);
else console.log("Message sent successfully!");
});
});Tip: To find your Facebook ID, look inside the cookies under the name c_user
const login = require("stfca");
const fs = require("fs");
login({ appState: [] }, (err, api) => {
if (err) {
console.error("Login Error:", err);
return;
}
let yourID = "000000000000000";
let imagePath = __dirname + "/image.jpg";
// Check if file exists
if (!fs.existsSync(imagePath)) {
console.error("Error: Image file not found!");
return;
}
let msg = {
body: "Hey!",
attachment: fs.createReadStream(imagePath)
};
api.sendMessage(msg, yourID, err => {
if (err) console.error("Message Sending Error:", err);
else console.log("Message sent successfully!");
});
});| Type | Usage |
|---|---|
| Regular text | { body: "message text" } |
| Sticker | { sticker: "sticker_id" } |
| File/Image | { attachment: fs.createReadStream(path) } or array of streams |
| URL | { url: "https://example.com" } |
| Large emoji | { emoji: "π", emojiSize: "large" } (small/medium/large) |
Note: A message can only be a regular message (which can be empty) and optionally one of the following: a sticker, an attachment, or a URL.
const fs = require("fs");
const login = require("stfca");
const credentials = { appState: [] };
login(credentials, (err, api) => {
if (err) {
console.error("Login Error:", err);
return;
}
try {
const appState = JSON.stringify(api.getAppState(), null, 2);
fs.writeFileSync("appstate.json", appState);
console.log("β
AppState saved successfully!");
} catch (error) {
console.error("Error saving AppState:", error);
}
});const fs = require("fs");
const login = require("stfca");
login(
{ appState: JSON.parse(fs.readFileSync("appstate.json", "utf8")) },
(err, api) => {
if (err) {
console.error("Login Error:", err);
return;
}
console.log("β
Logged in successfully!");
// Your code here
}
);Alternative: Use c3c-fbstate to get fbstate.json
const fs = require("fs");
const login = require("stfca");
login(
{ appState: JSON.parse(fs.readFileSync("appstate.json", "utf8")) },
(err, api) => {
if (err) {
console.error("Login Error:", err);
return;
}
// Enable listening to events (join/leave, title change, etc.)
api.setOptions({ listenEvents: true });
const stopListening = api.listenMqtt((err, event) => {
if (err) {
console.error("Listen Error:", err);
return;
}
// Mark as read
api.markAsRead(event.threadID, err => {
if (err) console.error("Mark as read error:", err);
});
// Handle different event types
switch (event.type) {
case "message":
if (event.body && event.body.trim().toLowerCase() === "/stop") {
api.sendMessage("Goodbyeβ¦", event.threadID);
stopListening();
return;
}
api.sendMessage(`TEST BOT: ${event.body}`, event.threadID);
break;
case "event":
console.log("Event Received:", event);
break;
}
});
}
);api.setOptions({
listenEvents: true, // Receive events (join/leave, rename, etc.)
selfListen: true, // Receive messages from yourself
logLevel: "silent" // Disable logs (silent/error/warn/info/verbose)
});By default:
listenEventsisfalse- won't receive events like joining/leaving chat, title changesselfListenisfalse- will ignore messages sent by the current account
- ST-BOT - Enhanced version of GoatBot V2, a powerful and customizable Facebook Messenger bot with advanced features, plugin support, and automatic updates. This is the main project that ST-FCA was designed for.
ST-FCA can be used for any Facebook Messenger bot project or automation tool. If you want to create your own messenger bot or use this API for other purposes, feel free to integrate it into your project.
See DOCS.md for detailed information about:
- All available API methods
- Parameters and options
- Event types
- Error handling
- Advanced usage examples
// Send message
api.sendMessage(message, threadID, callback);
// Send typing indicator
api.sendTypingIndicator(threadID, callback);
// Mark as read
api.markAsRead(threadID, callback);
// Get user info
api.getUserInfo(userID, callback);
// Get thread info
api.getThreadInfo(threadID, callback);
// Change thread color
api.changeThreadColor(color, threadID, callback);
// Change thread emoji
api.changeThreadEmoji(emoji, threadID, callback);
// Set message reaction
api.setMessageReaction(reaction, messageID, callback);Contributions are welcome! Please:
- Fork the repository
- Create a new branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
MIT License - See LICENSE for details.
ST | Sheikh Tamim - Facebook
If this project is helpful, please give it a β on GitHub!
Disclaimer: This is an unofficial API and is not officially supported by Facebook. Use responsibly and comply with Facebook Terms of Service.