Unfortunately I have zero knowledge on node.js because till now I used Ruby and its REPL called Pry. I discovered that node.js also has such packages, which can be installed by "npm" package manager. My reason to do this is the node.js package "facebook-chat-api" which is useful for sending facebook chat messages programmatically, and as far as I know this can't be achieved in Ruby (or maybe in other languages too). I installed the package found here https://www.npmjs.com/package/facebook-chat-api and tried it with success, help to the examples (face.js and I have run it with "node face.js"):
var login = require("facebook-chat-api");
login({email: "[email protected]", password: "XXXXXX"}, function(err,api) {
if(err) return console.error(err);
var yourID = "000000000000000";
var msg = {body: "Hey! My first programmatic message!"};
api.sendMessage(msg, yourID);
});
After setting the right ID for a user it worked and sent the message without flaws. Then I installed a REPL too, called "locus" (https://www.npmjs.com/package/locus), because I would like to stop the node.js script after the message is sent, and to send another one from the REPL command line. So my script became the following:
var login = require("facebook-chat-api");
var locus = require('locus')
login({email: "[email protected]", password: "XXXXXX"}, function(err,api) {
if(err) return console.error(err);
var yourID = "000000000000000";
var msg = {body: "Hey! My first programmatic message!"};
api.sendMessage(msg, yourID);
eval(locus);
});
Unfortunately my second script doesn't work as I expected. I really get a "locus" REPL prompt, but the facebook chat message isn't sent until I quit the REPL with the command "quit". I would like to stop my script exactly after the message was sent, I want to get a REPL promt, and then call again "api.sendMessage" from the REPL if possible. What can I do or how can I restructure my script to make it work as I excpect. Maybe to put the anonymous function to a real named function, but I don't know how to do that properly.
I made a small test that uses setTimeout for an async request and fake sends requests while you are still in locus.
This is the code:
var locus = require('locus');
function login () {
setTimeout(function () {
console.log('message sent');
},2000);
}
login();
eval(locus);
And this is the console with me typing a few commands in.
——————————————————————————————————————————————————————————————————————————
3 : function login () {
4 : setTimeout(function () {
5 : console.log('message sent');
6 : },2000);
7 : }
8 :
9 : login();
10 :
ʆ: message sent // 2 seconds after the repl opened the first message sent
typeof login
'function' // locus is aware of the login function
ʆ: login();
login(); // run the login function
undefined
ʆ: message sent // the message was (fake) sent without quitting
login(); // test a second send
undefined
ʆ: message sent // another message was sent.
If the above code shows the behavior you expected, your code could be:
var login = require("facebook-chat-api");
var locus = require('locus');
login({email: "[email protected]", password: "XXXXXX"}, loginHandler);
eval(locus);
function loginHandler (err,api) {
if(err) return console.error(err);
var yourID = "000000000000000";
var msg = {body: "Hey! My first programmatic message!"};
api.sendMessage(msg, yourID);
}