From c1953321d08834d276ad1efd81a7e85f960c9ff0 Mon Sep 17 00:00:00 2001 From: Luke Bonaccorsi Date: Sun, 29 May 2016 00:08:02 +0100 Subject: [PATCH] Initial commit for facebook messenger interface --- index.js | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 23 +++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 index.js create mode 100644 package.json diff --git a/index.js b/index.js new file mode 100644 index 0000000..55e6957 --- /dev/null +++ b/index.js @@ -0,0 +1,53 @@ +var bluebird = require('bluebird'), + messenger = bluebird.promisify(require('facebook-chat-api')), + facebook = function(){ + this.name = 'facebook'; + this.displayname = 'Facebook Messenger'; + this.description = 'Send messages to woodhouse via Facebook Messenger'; + + this.defaultPrefs = [{ + name: 'username', + displayname: 'Username', + type: 'text', + value: '' + },{ + name: 'password', + displayname: 'Password', + type: 'password', + value: '' + }]; + }; + +facebook.prototype.init = function(){ + this.getPrefs().done(function(prefs){ + this.prefs = prefs; + this.connect(); + }.bind(this)); +} + +facebook.prototype.connect = function() { + messenger({ + email: this.prefs.username, + password: this.prefs.password + }, { + logLevel: 'silent' + }).then(function(api) { + this.api = bluebird.promisifyAll(api); + + this.api.listenAsync().then(function(message) { + this.messageRecieved(message.threadID, message.body, message.senderID); + }.bind(this)); + + this.addMessageSender(function(message, to) { + this.api.sendMessage(message, to); + }.bind(this)); + }.bind(this)); +} + +facebook.prototype.exit = function(){ + if (this.api) { + this.api.logout(); + } +} + +module.exports = facebook; diff --git a/package.json b/package.json new file mode 100644 index 0000000..be2f4bd --- /dev/null +++ b/package.json @@ -0,0 +1,23 @@ +{ + "name": "woodhouse-interface-facebook-messenger", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/Woodhouse/woodhouse-interface-facebook-messenger.git" + }, + "author": "", + "license": "MIT", + "bugs": { + "url": "https://github.com/Woodhouse/woodhouse-interface-facebook-messenger/issues" + }, + "homepage": "https://github.com/Woodhouse/woodhouse-interface-facebook-messenger#readme", + "dependencies": { + "bluebird": "^3.4.0", + "facebook-chat-api": "^1.1.0" + } +}