Vi: MetaCore đã quay trở lại, được lọc những tính năng thừa thải và sẽ tập trung phát triển những tính năng cần thiết
En: MetaCore is back, filtered for redundant features and will focus on developing necessary features
Facebook now has an official API for chat bots here.
Original Project(Deprecated): https://github.com/Schmavery/facebook-chat-api
getUID
getOnlineTime
shareContact
uploadAttachment
editMessage
shareLink
This project is only available for bots that use listenMqtt
If you just want to use metacore, you should use this command:
npm install metacoreIt will download metacore from NPM repositories
If you want to use bleeding edge (directly from github) to test new features or submit bug report, this is the command for you:
npm install shinchan0911/metacoreIf you want to test your bots without creating another account on Facebook, you can use Facebook Whitehat Accounts.
const login = require("metacore");
// Create simple echo bot
login({email: "FB_EMAIL", password: "FB_PASSWORD"}, (err, api) => {
if(err) return console.error(err);
api.listen((err, message) => {
api.sendMessage(message.body, message.threadID);
});
});Result:
You can see it here.
Various types of message can be sent:
- Regular: set field
bodyto the desired message as a string. - Sticker: set a field
stickerto the desired sticker ID. - File or image: Set field
attachmentto a readable stream or an array of readable streams. - URL: set a field
urlto the desired URL. - Emoji: set field
emojito the desired emoji as a string and set fieldemojiSizewith size of the emoji (small,medium,large)
Note that 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.
Tip: to find your own ID, you can look inside the cookies. The userID is under the name c_user.
Example (Basic Message)
const login = require("metacore");
login({email: "FB_EMAIL", password: "FB_PASSWORD"}, (err, api) => {
if(err) return console.error(err);
var yourID = "000000000000000";
var msg = "Hey!";
api.sendMessage(msg, yourID);
});Example (File upload)
const login = require("metacore");
login({email: "FB_EMAIL", password: "FB_PASSWORD"}, (err, api) => {
if(err) return console.error(err);
// Note this example uploads an image called image.jpg
var yourID = "000000000000000";
var msg = {
body: "Hey!",
attachment: fs.createReadStream(__dirname + '/image.jpg')
}
api.sendMessage(msg, yourID);
});To avoid logging in every time you should save AppState (cookies etc.) to a file, then you can use it without having password in your scripts.
Example
const fs = require("fs");
const login = require("metacore");
var credentials = {email: "FB_EMAIL", password: "FB_PASSWORD"};
login(credentials, (err, api) => {
if(err) return console.error(err);
fs.writeFileSync('appstate.json', JSON.stringify(api.getAppState()));
});Alternative: Use c3c-fbstate to get fbstate.json (appstate.json)
Listen watches for messages sent in a chat. By default this won't receive events (joining/leaving a chat, title change etc…) but it can be activated with api.setOptions({listenEvents: true}). This will by default ignore messages sent by the current account, you can enable listening to your own messages with api.setOptions({selfListen: true}).
Example
const fs = require("fs");
const login = require("metacore");
// Simple echo bot. It will repeat everything that you say.
// Will stop when you say '/stop'
login({appState: JSON.parse(fs.readFileSync('appstate.json', 'utf8'))}, (err, api) => {
if(err) return console.error(err);
api.setOptions({listenEvents: true});
var stopListening = api.listenMqtt((err, event) => {
if(err) return console.error(err);
api.markAsRead(event.threadID, (err) => {
if(err) console.error(err);
});
switch(event.type) {
case "message":
if(event.body === '/stop') {
api.sendMessage("Goodbye…", event.threadID);
return stopListening();
}
api.sendMessage("TEST BOT: " + event.body, event.threadID);
break;
case "event":
console.log(event);
break;
}
});
});- How do I run tests?
For tests, create a
test-config.jsonfile that resemblesexample-config.jsonand put it in thetestdirectory. From the root >directory, runnpm test.
- Why doesn't
sendMessagealways work when I'm logged in as a page?
Pages can't start conversations with users directly; this is to prevent pages from spamming users.
- What do I do when
logindoesn't work?
First check that you can login to Facebook using the website. If login approvals are enabled, you might be logging in incorrectly. For how to handle login approvals, read our docs on
login.
- How can I avoid logging in every time? Can I log into a previous session?
We support caching everything relevant for you to bypass login.
api.getAppState()returns an object that you can save and pass into login as{appState: mySavedAppState}instead of the credentials object. If this fails, your session has expired.
- Do you support sending messages as a page?
Yes, set the pageID option on login (this doesn't work if you set it using api.setOptions, it affects the login process).
login(credentials, {pageID: "000000000000000"}, (err, api) => { … }
- I'm getting some crazy weird syntax error like
SyntaxError: Unexpected token [!!!
Please try to update your version of node.js before submitting an issue of this nature. We like to use new language features.
- I don't want all of these logging messages!
You can use
api.setOptionsto silence the logging. You get theapiobject fromlogin(see example above). Doapi.setOptions({ logLevel: "silent" });
