Skip to content

Instantly share code, notes, and snippets.

@henrik
Created March 3, 2016 22:57
Show Gist options
  • Save henrik/cffa4b465fa50b612bb1 to your computer and use it in GitHub Desktop.
Save henrik/cffa4b465fa50b612bb1 to your computer and use it in GitHub Desktop.
"Send on Facebook Messenger" command for Alfred.app.

Send on Messenger

Usage

Assumes you have node.js and npm.

Once, on install

Create a file ~/.facebook-auth containing your login email and password in the format [email protected]:my-password.

E.g.

echo "[email protected]:my-password" > ~/.facebook-auth

Install dependencies:

npm install

Run it manually

./send.js "Mark Zuckerberg" "Hello there"

Integrate it with Alfred.app

Make a workflow (left as an exercise to the reader) e.g. "im my message" to send "my message" to your favourite person.

In the "Run Script" action of that workflow, have it do something like:

/usr/local/bin/node /Users/henrik/Dropbox/send_on_messenger/send.js "Mark Zuckerberg" "{query}" > /tmp/fb.log 2> /tmp/fb.error.log
{
"name": "send_on_messenger",
"version": "1.0.0",
"main": "send.js",
"dependencies": {
"facebook-chat-api": "^1.0.7"
},
"devDependencies": {},
"author": "Henrik Nyh",
"license": "MIT"
}
#!/usr/bin/env node
var fs = require("fs");
var exec = require('child_process').exec;
// https://github.com/Schmavery/facebook-chat-api/blob/master/DOCS.md
var login = require("facebook-chat-api");
var appStatePath = "/tmp/send_on_messenger.appstate.json";
var home = process.env.HOME; // Works on OS X. YMMV.
var auth = fs.readFileSync(home + "/.facebook-auth", "utf8").trim().split(":");
var email = auth[0],
password = auth[1];
var args = process.argv.slice(2);
var sendToUser = args[0],
message = args[1];
if (!sendToUser) return console.error("Please specify a recipient name as the first argument.");
if (!message) return console.error("Please specify a message as the second argument.");
// If we have stored appState, logging in that way is a little faster (ca 3 secs vs 5 secs).
if (fs.existsSync(appStatePath)) {
// Log in via appState.
var appState = JSON.parse(fs.readFileSync(appStatePath, "utf8"));
login({ appState: appState }, function(err, api) {
if (err) return console.error(err);
console.log("sending via appState");
sendIt(api);
});
} else {
// Log in with email/pw.
login({ email: email, password: password }, function(err, api) {
if (err) return console.error(err);
fs.writeFileSync(appStatePath, JSON.stringify(api.getAppState()));
console.log("sending via email/pw");
sendIt(api);
});
}
function sendIt(api) {
api.getUserID(sendToUser, function(err, data) {
if (err) return console.error(err);
var threadID = data[0].userID;
api.sendMessage(message, threadID);
// Play a sound to let us know we sent it in the background. OS X specific.
exec("afplay /System/Library/Sounds/Tink.aiff");
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment