Facebook chat API that doesn't rely on XMPP. Will NOT be deprecated April 30th 2015.
Facebook now has an official API for chat bots here. This API is still the only way to automate chat functionalities on a user account. We do this by emulating the browser. This means doing the exact same GET/POST requests and tricking Facebook into thinking we're accessing the website normally. Because we're doing it this way, this API won't work with an auth token but requires the credentials of a Facebook account.
Disclaimer: 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... Be responsible Facebook citizens.
See below for projects using this API.
npm install facebook-chat-apivar login = ; // Create simple echo bot ;loginapi.addUserToGroupapi.changeArchivedStatusapi.changeGroupImageapi.changeThreadColorapi.changeThreadEmojiapi.changeNicknameapi.deleteMessageapi.deleteThreadapi.getAppStateapi.getCurrentUserIDapi.getFriendsListapi.getThreadHistoryapi.getThreadInfoapi.getThreadListapi.getUserIDapi.getUserInfoapi.handleMessageRequestapi.listenapi.logoutapi.markAsReadapi.muteThreadapi.removeUserFromGroupapi.searchForThreadapi.sendMessageapi.sendTypingIndicatorapi.setOptionsapi.setTitleVarious types of message can be sent:
body to the desired message as a string.sticker to the desired sticker ID.attachment to a readable stream or an array of readable streams.url to the desired URL.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)
;Example (File upload)
;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
// Simple echo bot. He'll repeat anything that you say. // Will stop when you say '/stop' ;For tests, create a
test-config.jsonfile that resemblesexample-config.jsonand put it in thetestdirectory. From the root >directory, runnpm test.
sendMessage always 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.
login doesn'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.
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.
Yes, set the pageID option on login (this doesn't work if you set it using api.setOptions, it affects the login process).
SyntaxError: Unexpected token [!!!## Projects using this API - [Kassy](https://github.com/mrkno/Kassy) - Kassy is a modular, easily extensible general purpose chat bot - [Marc Zuckerbot](https://github.com/bsansouci/marc-zuckerbot) - Facebook chat bot - [Marc Thuckerbot](https://github.com/bsansouci/lisp-bot) - Programmable lisp bot - [MarkovsInequality](https://github.com/logicx24/MarkovsInequality) - Extensible chat bot adding useful functions to Facebook Messenger - [AllanBot](https://github.com/AllanWang/AllanBot-Public) - Extensive module that combines the facebook api with firebase to create numerous functions; no coding experience is required to implement this. - [Larry Pudding Dog Bot](https://github.com/Larry850806/facebook-chat-bot) - A facebook bot you can easily customize the response - [fbash](https://github.com/avikj/fbash) - Run commands on your computer's terminal over Facebook MessengerPlease try to update your version of node.js before submitting an issue of this nature. We like to use new language features.