I'm very new to nodejs and I am trying to create an interactive shell script that will reply posts on my facebook wall. The problem is when I'm using the Readline module inside a loop, it's not stoping and just continue to loop.
Here's my whole code
var access_token = 'XXX';
var myId = 'XXX';
var FB = require('fb');
var url = require('url');
var utf8 = require('utf8');
var readline = require('readline');
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
FB.setAccessToken(access_token);
function getWallFeeds(feedLink, args) {
FB.api(feedLink, 'get', args, function (res) {
if (!res || res.error) {
console.log(!res ? 'error occurred' : res.error);
return;
}
processMessage(res.data);
var nextLinkParts = url.parse(res.paging.next, true);
var args = {
limit: nextLinkParts.query.limit,
until: nextLinkParts.query.until,
access_token: nextLinkParts.query.access_token
}
getWallFeeds(feedLink, args);
});
}
function processMessage(data) {
for (i in data) {
if (data[i].from.id!=myId) {
name = data[i].from.name;
message = data[i].message;
post_id = data[i].id;
console.log(post_id + ') ' + name+': '+utf8.encode(message));
rl.question("Please enter you reply: \n\n", function(answer) {
// Use answer and call FB api to post to comment..
rl.close();
});
}
}
}
feedLink = 'me/feed';
getWallFeeds(feedLink, {});
Here's the sample result
1020284792662_1020291351526) John: ....
Please enter you reply:
1020284792662_1020290585671) Ace: How are you!
Please enter you reply:
1020284792662_1020290581815) Nic: Hello there
Please enter you reply: