login
api.addUserToGroup
api.changeAdminStatus
api.changeArchivedStatus
api.changeBlockedStatus
api.changeGroupImage
api.changeNickname
api.changeThreadColor
api.changeThreadEmoji
api.createPoll
api.deleteMessage
api.deleteThread
api.forwardAttachment
api.getAppState
api.getCurrentUserID
api.getEmojiUrl
api.getFriendsList
api.getThreadHistory
api.getThreadInfo
api.getThreadList
api.getThreadPictures
api.getUserID
api.getUserInfo
api.handleMessageRequest
api.listen
api.listenMqtt
api.logout
api.markAsDelivered
api.markAsRead
api.markAsReadAll
api.muteThread
api.removeUserFromGroup
api.resolvePhotoUrl
api.searchForThread
api.sendMessage
api.sendTypingIndicator
api.setMessageReaction
api.setOptions
api.setTitle
api.threadColors
api.unsendMessage
Read this before you copy+paste examples from below.
You should not store Facebook password in your scripts. There are few good reasons:
- People who are standing behind you may look at your "code" and get your password if it is on the screen
- Backups of source files may be readable by someone else. "There is nothing secret in my code, why should I ever password protect my backups"
- You can't push your code to Github (or any onther service) without removing your password from the file. Remember: Even if you undo your accidential commit with password, Git doesn't delete it, that commit is just not used but is still readable by everybody.
- If you change your password in the future (maybe it leaked because someone stored password in source fileโฆ ohโฆ wellโฆ) you will have to change every occurrence in your scripts
Preferred method is to have login.js
that saves AppState
to a file and then use that file from all your scripts.
This way you can put password in your code for a minute, login to facebook and then remove it.
If you want to be even more safe: login.js can get password with require("readline")
or with environment variables like this:
var credentials = {
email: process.env.FB_EMAIL,
password: process.env.FB_PASSWORD
}
FB_EMAIL="[email protected]"
FB_PASSWORD="MySuperHardP@ssw0rd"
nodejs login.js
This function is returned by require(...)
and is the main entry point to the API.
It allows the user to log into facebook given the right credentials.
If it succeeds, callback
will be called with a null
object (for potential errors) and with an object containing all the available functions.
If it fails, callback
will be called with an error object.
Arguments
credentials
: An object containing the fieldsemail
andpassword
used to login, or an object containing the fieldappState
.options
: An object representing options to use when logging in (as described in api.setOptions).callback(err, api)
: A callback called when login is done (successful or not).err
is an object containing a fielderror
.
Example (Email & Password)
const login = require("facebook-chat-api");
login({email: "FB_EMAIL", password: "FB_PASSWORD"}, (err, api) => {
if(err) return console.error(err);
// Here you can use the api
});
Example (Email & Password then save appState to file)
const fs = require("fs");
const login = require("facebook-chat-api");
login({email: "FB_EMAIL", password: "FB_PASSWORD"}, (err, api) => {
if(err) return console.error(err);
fs.writeFileSync('appstate.json', JSON.stringify(api.getAppState()));
});
Example (AppState loaded from file)
const fs = require("fs");
const login = require("facebook-chat-api");
login({appState: JSON.parse(fs.readFileSync('appstate.json', 'utf8'))}, (err, api) => {
if(err) return console.error(err);
// Here you can use the api
});
Login Approvals (2-Factor Auth): When you try to login with Login Approvals enabled, your callback will be called with an error 'login-approval'
that has a continue
function that accepts the approval code as a string
or a number
.
Example:
const fs = require("fs");
const login = require("facebook-chat-api");
const readline = require("readline");
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const obj = {email: "FB_EMAIL", password: "FB_PASSWORD"};
login(obj, (err, api) => {
if(err) {
switch (err.error) {
case 'login-approval':
console.log('Enter code > ');
rl.on('line', (line) => {
err.continue(line);
rl.close();
});
break;
default:
console.error(err);
}
return;
}
// Logged in!
});
Review Recent Login: Sometimes Facebook will ask you to review your recent logins. This means you've recently logged in from a unrecognized location. This will will result in the callback being called with an error 'review-recent-login'
by default. If you wish to automatically approve all recent logins, you can set the option forceLogin
to true
in the loginOptions
.
Adds a user (or array of users) to a group chat.
Arguments
userID
: User ID or array of user IDs.threadID
: Group chat ID.callback(err)
: A callback called when the query is done (either with an error or with no arguments).
Given a adminID, or an array of adminIDs, will set the admin status of the user(s) to adminStatus
.
Arguments
threadID
: ID of a group chat (can't use in one-to-one conversations)adminIDs
: The id(s) of users you wish to admin/unadmin (string or an array).adminStatus
: Boolean indicating whether the user(s) should be promoted to admin (true
) or demoted to a regular user (false
).callback(err)
: A callback called when the query is done (either with an error or null).
Example
const fs = require("fs");
const login = require("facebook-chat-api");
login({appState: JSON.parse(fs.readFileSync('appstate.json', 'utf8'))}, (err, api) => {
if (err) return console.error(err);
let threadID = "0000000000000000";
let newAdmins = ["111111111111111", "222222222222222"];
api.changeAdminStatus(threadID, newAdmins, true, editAdminsCallback);
let adminToRemove = "333333333333333";
api.changeAdminStatus(threadID, adminToRemove, false, editAdminsCallback);
});
function editAdminsCallback(err) {
if (err) return console.error(err);
}
Given a threadID, or an array of threadIDs, will set the archive status of the threads to archive
. Archiving a thread will hide it from the logged-in user's inbox until the next time a message is sent or received.
Arguments
threadOrThreads
: The id(s) of the threads you wish to archive/unarchive.archive
: Boolean indicating the new archive status to assign to the thread(s).callback(err)
: A callback called when the query is done (either with an error or null).
Example
const fs = require("fs");
const login = require("facebook-chat-api");
login({appState: JSON.parse(fs.readFileSync('appstate.json', 'utf8'))}, (err, api) => {
if(err) return console.error(err);
api.changeArchivedStatus("000000000000000", true, (err) => {
if(err) return console.error(err);
});
});
Prevents a user from privately contacting you. (Messages in a group chat will still be seen by both parties).
Arguments
userID
: User ID.block
: Boolean indicating whether to block or unblock the user (true for block).callback(err)
: A callback called when the query is done (either with an error or with no arguments).
Will change the group chat's image to the given image.
Arguments
image
: File stream of image.threadID
: String representing the ID of the thread.callback(err)
: A callback called when the change is done (either with an error or null).
Example
const fs = require("fs");
const login = require("facebook-chat-api");
login({appState: JSON.parse(fs.readFileSync('appstate.json', 'utf8'))}, (err, api) => {
if(err) return console.error(err);
api.changeGroupImage(fs.createReadStream("./avatar.png"), "000000000000000", (err) => {
if(err) return console.error(err);
});
});
Will change the thread user nickname to the one provided.
Arguments
nickname
: String containing a nickname. Leave empty to reset nickname.threadID
: String representing the ID of the thread.participantID
: String representing the ID of the user.callback(err)
: An optional callback called when the change is done (either with an error or null).
Example
const fs = require("fs");
const login = require("facebook-chat-api");
login({appState: JSON.parse(fs.readFileSync('appstate.json', 'utf8'))}, (err, api) => {
if(err) return console.error(err);
api.changeNickname("Example", "000000000000000", "000000000000000", (err) => {
if(err) return console.error(err);
});
});
Will change the thread color to the given hex string color ("#0000ff"). Set it to empty string if you want the default.
Note: the color needs to start with a "#".
Arguments
color
: String representing a theme ID (a list of theme ID can be found atapi.threadColors
).threadID
: String representing the ID of the thread.callback(err)
: A callback called when the change is done (either with an error or null).
Example
const fs = require("fs");
const login = require("facebook-chat-api");
login({appState: JSON.parse(fs.readFileSync('appstate.json', 'utf8'))}, (err, api) => {
if(err) return console.error(err);
api.changeThreadColor("#0000ff", "000000000000000", (err) => {
if(err) return console.error(err);
});
});
Will change the thread emoji to the one provided.
Note: The UI doesn't play nice with all emoji.
Arguments
emoji
: String containing a single emoji character.threadID
: String representing the ID of the thread.callback(err)
: A callback called when the change is done (either with an error or null).
Example
const fs = require("fs");
const login = require("facebook-chat-api");
login({appState: JSON.parse(fs.readFileSync('appstate.json', 'utf8'))}, (err, api) => {
if(err) return console.error(err);
api.changeThreadEmoji("๐ฏ", "000000000000000", (err) => {
if(err) return console.error(err);
});
});
Creates a poll with the specified title and optional poll options, which can also be initially selected by the logged-in user.
Arguments
title
: String containing a title for the poll.threadID
: String representing the ID of the thread.options
: An optionalstring : bool
dictionary to specify initial poll options and their initial states (selected/not selected), respectively.callback(err)
: An optional callback called when the poll is posted (either with an error or null) - can omit theoptions
parameter and use this as the third parameter if desired.
Example
const fs = require("fs");
const login = require("facebook-chat-api");
login({appState: JSON.parse(fs.readFileSync('appstate.json', 'utf8'))}, (err, api) => {
if(err) return console.error(err);
api.createPoll("Example Poll", "000000000000000", {
"Option 1": false,
"Option 2": true
}, (err) => {
if(err) return console.error(err);
});
});
Takes a messageID or an array of messageIDs and deletes the corresponding message.
Arguments
messageOrMessages
: A messageID string or messageID string arraycallback(err)
: A callback called when the query is done (either with an error or null).
Example
const fs = require("fs");
const login = require("facebook-chat-api");
login({appState: JSON.parse(fs.readFileSync('appstate.json', 'utf8'))}, (err, api) => {
if(err) return console.error(err);
api.listen((err, message) => {
if(message.body) {
api.sendMessage(message.body, message.threadID, (err, messageInfo) => {
if(err) return console.error(err);
api.deleteMessage(messageInfo.messageID);
});
}
});
});
Given a threadID, or an array of threadIDs, will delete the threads from your account. Note that this does not remove the messages from Facebook's servers - anyone who hasn't deleted the thread can still view all of the messages.
Arguments
threadOrThreads
- The id(s) of the threads you wish to remove from your account.callback(err)
- A callback called when the operation is done, maybe with an object representing an error.
Example
const fs = require("fs");
const login = require("facebook-chat-api");
login({appState: JSON.parse(fs.readFileSync('appstate.json', 'utf8'))}, (err, api) => {
if(err) return console.error(err);
api.deleteThread("000000000000000", (err) => {
if(err) return console.error(err);
});
});
Forwards corresponding attachment to given userID or to every user from an array of userIDs
Arguments
attachmentID
: The ID field in the attachment object. Recorded audio cannot be forwarded.userOrUsers
: A userID string or usersID string arraycallback(err)
: A callback called when the query is done (either with an error or null).
Returns current appState which can be saved to a file or stored in a variable.
Returns the currently logged-in user's Facebook user ID.
Returns the URL to a Facebook Messenger-style emoji image asset.
note: This function will return a URL regardless of whether the image at the URL actually exists. This can happen if, for example, Messenger does not have an image asset for the requested emoji.
Arguments
c
- The emoji charactersize
- The width and height of the emoji image; supported sizes are 32, 64, and 128pixelRatio
- The pixel ratio of the emoji image; supported ratios are '1.0' and '1.5' (default is '1.0')
Example
const fs = require("fs");
const login = require("facebook-chat-api");
login({appState: JSON.parse(fs.readFileSync('appstate.json', 'utf8'))}, (err, api) => {
if(err) return console.error(err);
// Prints https://static.xx.fbcdn.net/images/emoji.php/v8/z9c/1.0/128/1f40d.png
console.log('Snake emoji, 128px (128x128 with pixel ratio of 1.0');
console.log(api.getEmojiUrl('\ud83d\udc0d', 128));
// Prints https://static.xx.fbcdn.net/images/emoji.php/v8/ze1/1.5/128/1f40d.png
console.log('Snake emoji, 192px (128x128 with pixel ratio of 1.5');
console.log(api.getEmojiUrl('\ud83d\udc0d', 128, '1.5'));
});
Returns an array of objects with some information about your friends.
Arguments
callback(err, arr)
- A callback called when the query is done (either with an error or with an confirmation object).arr
is an array of objects with the following fields:alternateName
,firstName
,gender
,userID
,isFriend
,fullName
,profilePicture
,type
,profileUrl
,vanity
,isBirthday
.
Example
const fs = require("fs");
const login = require("facebook-chat-api");
login({appState: JSON.parse(fs.readFileSync('appstate.json', 'utf8'))}, (err, api) => {
if(err) return console.error(err);
api.getFriendsList((err, data) => {
if(err) return console.error(err);
console.log(data.length);
});
});
Takes a threadID, number of messages, a timestamp, and a callback.
note: if you're getting a 500 error, it's possible that you're requesting too many messages. Try reducing that number and see if that works.
Arguments
threadID
: A threadID corresponding to the target chatamount
: The amount of messages to requesttimestamp
: Used to described the time of the most recent message to load. If timestamp isundefined
, facebook will load the most recent messages.callback(error, history)
: If error is null, history will contain an array of message objects.
Example
To load 50 messages at a time, we can use undefined
as the timestamp to retrieve the most recent messages and use the timestamp of the earliest message to load the next 50.
var timestamp = undefined;
function loadNextThreadHistory(api){
api.getThreadHistory(threadID, 50, timestamp, (err, history) => {
if(err) return console.error(err);
/*
Since the timestamp is from a previous loaded message,
that message will be included in this history so we can discard it unless it is the first load.
*/
if(timestamp != undefined) history.pop();
/*
Handle message history
*/
timestamp = history[0].timestamp;
});
}
Takes a threadID and a callback. Works for both single-user and group threads.
Arguments
threadID
: A threadID corresponding to the target thread.callback(err, info)
: Iferr
isnull
,info
will contain the following properties: *callback(err, arr)
: A callback called when the query is done (either with an error or with an confirmation object).arr
is an array of thread object containing the following properties:
Key | Description |
---|---|
threadID | ID of the thread |
participantIDs | Array of user IDs in the thread |
name | Name of the thread. Usually the name of the user. In group chats, this will be empty if the name of the group chat is unset. |
nicknames | Map of nicknames for members of the thread. If there are no nicknames set, this will be null. |
unreadCount | Number of unread messages |
messageCount | Number of messages |
imageSrc | URL to the group chat photo. Null if unset or a 1-1 thread. |
timestamp | |
muteUntil | Timestamp at which the thread will no longer be muted. The timestamp will be -1 if the thread is muted indefinitely or null if the thread is not muted. |
isGroup | boolean, true if this thread is a group thread (more than 2 participants). |
isSubscribed | |
folder | The folder that the thread is in. Can be one of:
|
isArchived | True if the thread is archived, false if not |
cannotReplyReason | If you cannot reply to this thread, this will be a string stating why. Otherwise it will be null. |
lastReadTimestamp | Timestamp of the last message that is marked as 'read' by the current user. |
emoji | Object with key 'emoji' whose value is the emoji unicode character. Null if unset. |
color | String form of the custom color in hexadecimal form. |
adminIDs | Array of user IDs of the admins of the thread. Empty array if unset. |
Returns information about the user's threads.
Arguments
limit
: Limit the number of threads to fetch.timestamp
: Request threads before this date.null
means nowtags
: An array describing which folder to fetch. It should be one of these:["INBOX"]
(same as[]
)["ARCHIVED"]
["PENDING"]
["OTHER"]
["INBOX", "unread"]
["ARCHIVED", "unread"]
["PENDING", "unread"]
["OTHER", "unread"]
if you find something new, let us know
callback(err, list)
: Callback called when the query is done (either with an error or with a proper result).list
is an array with objects with the following properties:
Thread list
Key | Description |
---|---|
threadID | ID of the thread |
name | The name of the thread |
unreadCount | Amount of unread messages in thread |
messageCount | Amount of messages in thread |
imageSrc | Link to the thread's image or null |
emoji | The default emoji in thread (classic like is null ) |
color | Thread's message color in RRGGBB (default blue is null ) |
nicknames | An array of {"userid": "1234", "nickname": "John Doe"} |
muteUntil | Timestamp until the mute expires or null |
participants | An array of participants. See below |
adminIDs | An array of thread admin IDs |
folder | INBOX , ARCHIVED , PENDING or OTHER |
isGroup | true or false |
customizationEnabled | false in one-to-one conversations with Page or ReducedMessagingActor |
participantAddMode | currently "ADD" for groups and null otherwise |
reactionsMuteMode | REACTIONS_NOT_MUTED or REACTIONS_MUTED |
mentionsMuteMode | MENTIONS_NOT_MUTED or MENTIONS_MUTED |
isArchived | true or false |
isSubscribed | true or false |
timestamp | timestamp in miliseconds |
snippet | Snippet's text message |
snippetAttachments | Attachments in snippet |
snippetSender | ID of snippet sender |
lastMessageTimestamp | timestamp in milliseconds |
lastReadTimestamp | timestamp in milliseconds or null |
cannotReplyReason | null , "RECIPIENTS_NOT_LOADABLE" or "BLOCKED" |
participants
format
accountType
is one of the following:
"User"
"Page"
"UnavailableMessagingActor"
"ReducedMessagingActor"
(there might be more)
Account type | Key | Description |
---|---|---|
"User" |
userID | ID of user |
name | Full name of user | |
shortName | Short name of user (most likely first name) | |
gender | Either
"MALE" ,
"FEMALE" ,
"NEUTER" or
"UNKNOWN"
|
|
url | URL of the user's Facebook profile | |
profilePicture | URL of the profile picture | |
username | Username of user or
null
|
|
isViewerFriend | Is the user a friend of you? | |
isMessengerUser | Does the user use Messenger? | |
isVerified | Is the user verified? (Little blue tick mark) | |
isMessageBlockedByViewer | Is the user blocking messages from you? | |
isViewerCoworker | Is the user your coworker? | |
"Page" |
userID | ID of the page |
name | Name of the fanpage | |
url | URL of the fanpage | |
profilePicture | URL of the profile picture | |
username | Username of user or
null
|
|
acceptsMessengerUserFeedback | ||
isMessengerUser | Does the fanpage use Messenger? | |
isVerified | Is the fanpage verified? (Little blue tick mark) | |
isMessengerPlatformBot | Is the fanpage a bot | |
isMessageBlockedByViewer | Is the fanpage blocking messages from you? | |
"ReducedMessagingActor" (account requres verification, messages are hidden) |
userID | ID of the user |
name | Name of the user | |
url |
null
|
|
profilePicture | URL of the default Facebook profile picture | |
username | Username of user | |
acceptsMessengerUserFeedback | ||
isMessageBlockedByViewer | Is the user blocking messages from you? | |
"UnavailableMessagingActor" (account disabled/removed) |
userID | ID of the user |
name | Facebook User in user's language | |
url | null |
|
profilePicture | URL of the default **male** Facebook profile picture | |
username | null |
|
acceptsMessengerUserFeedback | ||
isMessageBlockedByViewer | Is the user blocking messages from you? |
In a case that some account type is not supported, we return just this (but you can't rely on it) and log a warning to the console:
Key | Description |
---|---|
accountType | type, can be anything |
userID | ID of the account |
name | Name of the account |
Returns pictures sent in the thread.
Arguments
threadID
: A threadID corresponding to the target chatoffset
: Start index of picture to retrieve, where 0 is the most recent picturelimit
: Number of pictures to get, incrementing from the offset indexcallback(err, arr)
: A callback called when the query is done (either with an error or with an confirmation object).arr
is an array of objects withuri
,width
, andheight
.
Given the full name or vanity name of a Facebook user, event, page, group or app, the call will perform a Facebook Graph search and return all corresponding IDs (order determined by Facebook).
Arguments
name
- A string being the name of the item you're looking for.callback(err, obj)
- A callback called when the search is done (either with an error or with the resulting object).obj
is an array which contains all of the items that facebook graph search found, ordered by "importance". Each item in the array has the following properties:userID
,photoUrl
,indexRank
,name
,isVerified
,profileUrl
,category
,score
,type
(type is generally user, group, page, event or app).
Example
const fs = require("fs");
const login = require("facebook-chat-api");
login({appState: JSON.parse(fs.readFileSync('appstate.json', 'utf8'))}, (err, api) => {
if(err) return console.error(err);
api.getUserID("Marc Zuckerbot", (err, data) => {
if(err) return console.error(err);
// Send the message to the best match (best by Facebook's criteria)
var msg = "Hello!"
var threadID = data[0].userID;
api.sendMessage(msg, threadID);
});
});
Will get some information about the given users.
Arguments
ids
- Either a string/number for one ID or an array of strings/numbers for a batched query.callback(err, obj)
- A callback called when the query is done (either with an error or with an confirmation object).obj
is a mapping from userId to another object containing the following properties:name
,firstName
,vanity
(user's chosen facebook handle, if any),thumbSrc
,profileUrl
,gender
,type
(type is generally user, group, page, event or app),isFriend
,isBirthday
,searchTokens
,alternateName
.
Example
const fs = require("fs");
const login = require("facebook-chat-api");
login({appState: JSON.parse(fs.readFileSync('appstate.json', 'utf8'))}, (err, api) => {
if(err) return console.error(err);
api.getUserInfo([1, 2, 3, 4], (err, ret) => {
if(err) return console.error(err);
for(var prop in ret) {
if(ret.hasOwnProperty(prop) && ret[prop].isBirthday) {
api.sendMessage("Happy birthday :)", prop);
}
}
});
});
A dictionary mapping names of all currently valid thread themes to their theme ID that are accepted by api.changeThreadColor
. These themes, listed below, are the ones present in the palette UI used for selecting thread themes on the Messenger client.
- DefaultBlue:
196241301102133
- HotPink:
169463077092846
- AquaBlue:
2442142322678320
- BrightPurple:
234137870477637
- CoralPink:
980963458735625
- Orange:
175615189761153
- Green:
2136751179887052
- LavenderPurple:
2058653964378557
- Red:
2129984390566328
- Yellow:
174636906462322
- TealBlue:
1928399724138152
- Aqua:
417639218648241
- Mango:
930060997172551
- Berry:
164535220883264
- Citrus:
370940413392601
- Candy:
205488546921017
- StarWars:
809305022860427
Accept or ignore message request(s) with thread id threadID
.
Arguments
threadID
: A threadID or array of threadIDs corresponding to the target thread(s). Can be numbers or strings.accept
: Boolean indicating the new status to assign to the message request(s); true for inbox, false to others.callback(err)
: A callback called when the query is done (with an error or with null).
Will call callback
when a new message is received on this account.
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})
. This returns stopListening
that will stop the listen
loop and is guaranteed to prevent any future calls to the callback given to listen
. An immediate call to stopListening
when an error occurs will prevent the listen function to continue.
Arguments
callback(error, message)
: A callback called every time the logged-in account receives a new message.
The message object will contain different fields based on its type (as determined by its type
field). By default, the only type that will be listened for is message
. If enabled through setOptions, the message object may alternatively represent an event e.g. a read receipt. The available event types are as follows:
Attachments
Similar to how messages can vary based on their type
, so too can the attachments
within "message"
events. Each attachment will consist of an object of one of the following types:
Attachment Type | Fields |
---|---|
"sticker" |
ID , url , packID , spriteUrl , spriteUrl2x , width , height , caption , description , frameCount , frameRate , framesPerRow , framesPerCol |
"file" |
ID , filename , url , isMalicious , contentType |
"photo" |
ID , filename , thumbnailUrl , previewUrl , previewWidth , previewHeight , largePreviewUrl , largePreviewWidth , largePreviewHeight |
"animated_image" |
ID , filename , previewUrl , previewWidth , previewHeight , url , width , height |
"video" |
ID , filename , previewUrl , previewWidth , previewHeight , url , width , height , duration , videoType |
"audio" |
ID , filename , audioType , duration , url , isVoiceMail |
"share" |
ID , url , title , description , source , image , width , height , playable , duration , playableUrl , subattachments , properties |
Example
const fs = require("fs");
const login = require("facebook-chat-api");
// Simple echo bot. He'll repeat anything 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.listen((err, event) => {
if(err) return console.error(err);
switch(event.type) {
case "message":
if(event.body === '/stop') {
api.sendMessage("Goodbye...", event.threadID);
return stopListening();
}
api.markAsRead(event.threadID, (err) => {
if(err) console.log(err);
});
api.sendMessage("TEST BOT: " + event.body, event.threadID);
break;
case "event":
console.log(event);
break;
}
});
});
Same as api.listen
but uses MQTT to recieve data.
Will call callback
when a new message is received on this account.
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})
. This returns stopListening
that will stop the listen
loop and is guaranteed to prevent any future calls to the callback given to listenMqtt
. An immediate call to stopListening
when an error occurs will prevent the listen function to continue.
Arguments
callback(error, message)
: A callback called every time the logged-in account receives a new message.
Messages and Events are the same as api.listen
Logs out the current user.
Arguments
callback(err)
: A callback called when the query is done (either with an error or with null).
Given a threadID and a messageID will mark that message as delivered. If a message is marked as delivered that tells facebook servers that it was recieved.
You can also mark new messages as delivered automatically. This is enabled by default. See api.setOptions.
Arguments
threadID
- The id of the thread in which you want to mark the message as delivered.messageID
- The id of the message want to mark as delivered.callback(err)
- A callback called when the operation is done maybe with an object representing an error.
Example
const fs = require("fs");
const login = require("facebook-chat-api");
login({appState: JSON.parse(fs.readFileSync('appstate.json', 'utf8'))}, (err, api) => {
if(err) return console.error(err);
api.listen((err, message) => {
if(err) return console.error(err);
// Marks messages as delivered immediately after they're received
api.markAsDelivered(message.threadID, message.messageID);
});
});
Given a threadID will mark all the unread messages as read. Facebook will take a couple of seconds to show that you've read the messages.
You can also mark new messages as read automatically. See api.setOptions.
Arguments
threadID
- The id of the thread in which you want to mark the messages as read.read
- An optional boolean wheretrue
means to mark the message as being "read" andfalse
means to mark the message as being "unread".callback(err)
- A callback called when the operation is done maybe with an object representing an error.
Example
const fs = require("fs");
const login = require("facebook-chat-api");
login({appState: JSON.parse(fs.readFileSync('appstate.json', 'utf8'))}, (err, api) => {
if(err) return console.error(err);
api.listen((err, message) => {
if(err) return console.error(err);
// Marks messages as read immediately after they're received
api.markAsRead(message.threadID);
});
});
This function will mark all of messages in your inbox readed.
Mute a chat for a period of time, or unmute a chat.
Arguments
threadID
- The ID of the chat you want to mute.muteSeconds
- Mute the chat for this amount of seconds. Use0
to unmute a chat. Use '-1' to mute a chat indefinitely.callback(err)
- A callback called when the operation is done maybe with an object representing an error.
Example
const fs = require("fs");
const login = require("facebook-chat-api");
login({appState: JSON.parse(fs.readFileSync('appstate.json', 'utf8'))}, (err, api) => {
if(err) return console.error(err);
api.listen((err, message) => {
if(err) return console.error(err);
// Mute all incoming chats for one minute
api.muteThread(message.threadID, 60);
});
});
Removes a user from a group chat.
Arguments
userID
: User ID.threadID
: Group chat ID.callback(err)
: A callback called when the query is done (either with an error or with no arguments).
Resolves the URL to the full-size photo, given its ID. This function is useful for retrieving the full-size photo URL
of image attachments in messages, returned by api.getThreadHistory
.
Arguments
photoID
: Photo ID.callback(err, url)
: A callback called when the query is done (either with an error or with the photo's URL).url
is a string with the photo's URL.
This part is outdated. see #396
Takes a chat title (thread name) and returns matching results as a formatted threads array (ordered according to Facebook).
Arguments
name
: A messageID string or messageID string arraycallback(err, obj)
: A callback called when the query is done (either with an error or a thread object). The object passed in the callback has the following shape:threadID
,,participants
participantIDs
,formerParticipants
,name
,nicknames
,snippet
,snippetHasAttachment
,snippetAttachments
,snippetSender
,unreadCount
,messageCount
,imageSrc
,timestamp
,serverTimestamp
,muteSettings
,isCanonicalUser
,isCanonical
,canonicalFbid
,isSubscribed
,rootMessageThreadingID
,folder
,isArchived
,recipientsLoadable
,hasEmailParticipant
,readOnly
,canReply
,composerEnabled
,blockedParticipants
,lastMessageID
Sends the given message to the threadID.
Arguments
message
: A string (for backward compatibility) or a message object as described below.threadID
: A string, number, or array representing a thread. It happens to be someone's userID in the case of a one to one conversation or an array of userIDs when starting a new group chat.callback(err, messageInfo)
: (Optional) A callback called when sending the message is done (either with an error or with an confirmation object).messageInfo
contains thethreadID
where the message was sent and amessageID
, as well as thetimestamp
of the message.messageID
: (Optional) A string representing a message you want to reply.
Message Object:
Various types of message can be sent:
- Regular: set field
body
to the desired message as a string. - Sticker: set a field
sticker
to the desired sticker ID. - File or image: Set field
attachment
to a readable stream or an array of readable streams. - URL: set a field
url
to the desired URL. - Emoji: set field
emoji
to the desired emoji as a string and set fieldemojiSize
with size of the emoji (small
,medium
,large
) - Mentions: set field
mentions
to an array of objects. Objects should have thetag
field set to the text that should be highlighted in the mention. The object should have anid
field, where theid
is the user id of the person being mentioned. The instance oftag
that is highlighted is determined through indexOf, an optionalfromIndex
can be passed in to specify the start index to start searching for thetag
text inbody
(default=0). (See below for an example.)
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 fs = require("fs");
const login = require("facebook-chat-api");
login({appState: JSON.parse(fs.readFileSync('appstate.json', 'utf8'))}, (err, api) => {
if(err) return console.error(err);
var yourID = "000000000000000";
var msg = {body: "Hey!"};
api.sendMessage(msg, yourID);
});
Example (File upload)
const fs = require("fs");
const login = require("facebook-chat-api");
login({appState: JSON.parse(fs.readFileSync('appstate.json', 'utf8'))}, (err, api) => {
if(err) return console.error(err);
// 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);
});
Example (Mention)
const login = require("facebook-chat-api");
login({email: "EMAIL", password: "PASSWORD"}, (err, api) => {
if(err) return console.error(err);
api.listen((err, message) => {
if (message && message.body) {
// Getting the actual sender name from ID involves calling
// `api.getThreadInfo` and `api.getUserInfo`
api.sendMessage({
body: 'Hello @Sender! @Sender!',
mentions: [{
tag: '@Sender',
id: message.senderID,
fromIndex: 9, // Highlight the second occurrence of @Sender
}],
}, message.threadID);
}
});
});
Sends a "USERNAME is typing" indicator to other members of the thread indicated by threadID
. This indication will disappear after 30 second or when the end
function is called. The end
function is returned by api.sendTypingIndicator
.
Arguments
threadID
: Group chat ID.callback(err)
: A callback called when the query is done (with an error or with null).
Sets reaction on message
Arguments
reaction
: A string containing either an emoji, an emoji in unicode, or an emoji shortcut (see list of supported emojis below). The string can be left empty ("") in order to remove a reaction.messageID
: A string representing the message ID.callback(err)
- A callback called when sending the reaction is done.
Supported Emojis
Emoji | Text | Unicode | Shortcuts |
---|---|---|---|
๐ | ๐ |
\uD83D\uDE0D |
:love: , :heart_eyes: |
๐ | ๐ |
\uD83D\uDE06 |
:haha: , :laughing: |
๐ฎ | ๐ฎ |
\uD83D\uDE2E |
:wow: , :open_mouth: |
๐ข | ๐ข |
\uD83D\uDE22 |
:sad: , :cry: |
๐ | ๐ |
\uD83D\uDE20 |
:angry: |
๐ | ๐ |
\uD83D\uDC4D |
:like: , :thumbsup: |
๐ | ๐ |
\uD83D\uDC4E |
:dislike: , :thumbsdown: |
Sets various configurable options for the api.
Arguments
options
- An object containing the new values for the options that you want to set. If the value for an option is unspecified, it is unchanged. The following options are possible.logLevel
: The desired logging level as determined by npmlog. Choose from either"silly"
,"verbose"
,"info"
,"http"
,"warn"
,"error"
, or"silent"
.selfListen
: (Defaultfalse
) Set this totrue
if you want your api to receive messages from its own account. This is to be used with caution, as it can result in loops (a simple echo bot will send messages forever).listenEvents
: (Defaultfalse
) Will make api.listen also handle events (look at api.listen for more details).pageID
: (Default empty) Makes api.listen only receive messages through the page specified by that ID. Also makessendMessage
andsendSticker
send from the page.updatePresence
: (Defaultfalse
) Will make api.listen also returnpresence
(api.listen for more details).forceLogin
: (Defaultfalse
) Will automatically approve of any recent logins and continue with the login process.userAgent
: (DefaultMozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/600.3.18 (KHTML, like Gecko) Version/8.0.3 Safari/600.3.18
) The desired simulated User Agent.autoMarkDelivery
: (Defaulttrue
) Will automatically mark new messages as delivered. See api.markAsDelivered.autoMarkRead
: (Defaultfalse
) Will automatically mark new messages as read/seen. See api.markAsRead.
Example
const fs = require("fs");
const login = require("facebook-chat-api");
// Simple echo bot. This will send messages forever.
login({appState: JSON.parse(fs.readFileSync('appstate.json', 'utf8'))}, (err, api) => {
if(err) return console.error(err);
api.setOptions({
selfListen: true,
logLevel: "silent"
});
api.listen((err, message) => {
if(err) return console.error(err);
// Ignore empty messages (photos etc.)
if (typeof message.body === "string") {
api.sendMessage(message.body, message.threadID);
}
});
});
Sets the title of the group chat with thread id threadID
to newTitle
.
Note: This will not work if the thread id corresponds to a single-user chat or if the bot is not in the group chat.
Arguments
newTitle
: A string representing the new title.threadID
: A string or number representing a thread. It happens to be someone's userID in the case of a one to one conversation.callback(err, obj)
- A callback called when sending the message is done (either with an error or with an confirmation object).obj
contains only the threadID where the message was sent.
Revokes a message from anyone that could see that message with messageID
Note: This will only work if the message is sent by you and was sent less than 10 minutes ago.
Arguments
messageID
: Message ID you want to unsend.callback(err)
: A callback called when the query is done (with an error or with null).