Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Source code of a Facebook Chat Bot, published at http://wp.me/p40uwy-1E
/**
* Sends a message to all your online Facebook contacts.
*
* @author Bryan Horna <bryanjhv@gmail.com>
* @version 2.0
*/
/* Constants */
/**
* The message you want to send.
*
* @type {String}
*/
var MESSAGE = "Hi! I'm trying my Facebook Chat Bot v2. Would you like to " +
"build your own? Go to: http://wp.me/p40uwy-1E",
TO_ALL = false;
/**
* Wrapper for unnecessary functions.
*/
(function (g) {
/* Helpers */
/**
* Iterates over an Array.
*
* @param {Array|NodeList} arr
* @param {Function} callback
*/
var forEach = function (arr, callback) {
[].forEach.call(arr, callback);
};
/**
* Parses HTML.
*
* @param {String} html
* @returns {HTMLDocument}
*/
var parseHtml = function (html) {
var parser = new DOMParser();
return parser.parseFromString(html, 'text/html');
};
/**
* Gets Object keys.
*
* @type {Function}
*/
var keys = Object.keys;
/**
* Makes an AJAX request.
*
* Options object:
* - url: The URL to fetch.
* - data: Data as hash. (optional)
* - method: HTTP verb. (default 'GET')
* - success: Callback on success.
* - error: Callback on error.
*
* @param {Object} options
*/
var request = function (options) {
var nop = function () {},
url = options.url || '',
data = options.data || {},
method = options.method || 'GET',
success = options.success || nop,
error = options.error || nop;
if (!url) {
return;
}
var params = [],
eUC = encodeURIComponent;
forEach(keys(data), function (key) {
params.push(eUC(key) + '=' + eUC(data[key]));
});
var textData = params.join('&');
var xhr = new XMLHttpRequest();
xhr.open(method, url);
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
success(xhr.response, data);
} else {
error(xhr.status, data);
}
}
};
xhr.send(method === 'GET' ? null : textData);
};
/**
* Fetchs the full name given an ID.
*
* @param {Object} fields
* @param {Function} callback
*/
var userName = function (fields, callback) {
var id = '';
forEach(keys(fields), function (field) {
if (field.indexOf('ids') + 1) {
id = fields[field];
}
});
var success = function (data) {
data = JSON.parse(data);
callback(data.name, id);
};
if (id) {
request({
url: 'https://graph.facebook.com/' + id,
success: success
});
}
};
/**
* Notifies events.
*
* @param {String} msg
*/
var notify = function (msg) {
alert(msg + '.');
};
/* Callbacks */
/**
* Gets called when message was sent.
*
* @param {String} response
* @param {Object} fields Fields sent.
*/
var msgSuccess = function (response, fields) {
userName(fields, function (name) {
setTimeout(function () {
notify('Message sent to: ' + name);
}, 5000);
});
};
/**
* Gets called when message failed.
*
* @param {Number} status
* @param {Object} fields Fields sent.
*/
var msgError = function (status, fields) {
userName(fields, function (name) {
setTimeout(function () {
notify("Couldn't send message to: " + name);
}, 5000);
})
};
/**
* Called when buddy page was loaded.
*
* @param {String} buddyHtml The HTML of the page.
*/
var buddySuccess = function (buddyHtml) {
var doc = parseHtml(buddyHtml),
form = doc.querySelector('#composer_form'),
inputs = form.querySelectorAll('input'),
fields = {};
forEach(inputs, function (input) {
fields[input.name] = input.value;
});
delete fields.send_photo;
fields.body = MESSAGE;
var exec = function () {
request({
url: form.action,
data: fields,
method: 'POST',
success: msgSuccess,
error: msgError
});
};
if (TO_ALL) {
exec();
} else {
userName(fields, function (name) {
if (confirm('Send message to: ' + name + '?')) {
exec();
}
});
}
};
/**
* If fetching buddy page failed.
*/
var buddyError = function () {
notify("Couldn't fetch buddy page");
};
/**
* Called when buddylist was retrieved.
*
* @param {String} listHtml HTML content.
*/
var listSuccess = function (listHtml) {
var doc = parseHtml(listHtml),
tables = doc.querySelectorAll('#root div table'),
buddies;
forEach(tables, function (table) {
buddies = table.querySelectorAll('td div a');
if (buddies) {
forEach(buddies, function (buddy) {
request({
url: buddy.href,
success: buddySuccess,
error: buddyError
});
});
}
});
};
/**
* If buddylist failed.
*/
var listError = function () {
notify("Couldn't fetch buddy list");
};
/**
* Main program.
*
* @param {Boolean} all True to send message to all connected buddies.
*/
g.bot = function (all) {
if (all) {
TO_ALL = true;
}
request({
url: '/buddylist.php',
success: listSuccess,
error: listError
});
};
})(window);
/**
* Sends a message to all your online Facebook contacts.
*
* @author Bryan Horna <bryanjhv@gmail.com>
* @version 2.0
*/
var MESSAGE="Hi! I'm trying my Facebook Chat Bot v2. Would you like to build your own? Go to: http://wp.me/p40uwy-1E",TO_ALL=false;(function(j){var g=function(n,o){[].forEach.call(n,o)},c=function(n){return new DOMParser().parseFromString(n,"text/html")},m=Object.keys,e=function(o){var u=function(){},n=o.url||"",q=o.data||{},t=o.method||"GET";if(!n){return}var s=[],p=encodeURIComponent;g(m(q),function(v){s.push(p(v)+"="+p(q[v]))});var r=new XMLHttpRequest();r.open(t,n);r.onreadystatechange=function(){if(r.readyState==4){if(r.status==200){(o.success||u)(r.response,q)}else{(o.error||u)(r.status,q)}}};r.send(t=="GET"?null:s.join("&"))},i=function(n,p){var o="";g(m(n),function(q){if(q.indexOf("ids")+1){o=n[q]}});if(o){e({url:"https://graph.facebook.com/"+o,success:function(q){q=JSON.parse(q);p(q.name,o)}})}},l=function(n){alert(n+".")};var f=function(o,n){i(n,function(p){setTimeout(function(){l("Message sent to: "+p)},5000)})},k=function(o,n){i(n,function(p){setTimeout(function(){l("Couldn't send message to: "+p)},5000)})},a=function(q){var p=c(q).querySelector("#composer_form"),n={};g(p.querySelectorAll("input"),function(r){n[r.name]=r.value});delete n.send_photo;n.body=MESSAGE;var o=function(){e({url:p.action,data:n,method:"POST",success:f,error:k})};if(TO_ALL){o()}else{i(n,function(r){if(confirm("Send message to: "+r+"?")){o()}})}},h=function(){l("Couldn't fetch buddy page")},b=function(n){var o;g(c(n).querySelectorAll("#root div table"),function(p){o=p.querySelectorAll("td div a");if(o){g(o,function(q){e({url:q.href,success:a,error:h})})}})},d=function(){l("Couldn't fetch buddy list")};j.bot=function(n){TO_ALL=!!n;e({url:"/buddylist.php",success:b,error:d})}})(window);
/**
* Sends a message to all your online Facebook contacts.
*
* @author Bryan Horna <bryanjhv@gmail.com>
* @version 2.0
*/
/* The same as fb-bot.js but something minified */
/* Constants */
/**
* The message you want to send.
*
* @type {String}
*/
var MESSAGE = "Hi! I'm trying my Facebook Chat Bot v2. Would you like to " +
"build your own? Go to: http://wp.me/p40uwy-1E",
TO_ALL = false;
/**
* Wrapper for unnecessary functions.
*/
(function (g) {
/* Helpers */
/**
* Iterates over an Array.
*
* @param {Array|NodeList} arr
* @param {Function} callback
*/
var forEach = function (arr, callback) {
[].forEach.call(arr, callback);
},
/**
* Parses HTML.
*
* @param {String} html
* @returns {HTMLDocument}
*/
parseHtml = function (html) {
return new DOMParser().parseFromString(html, 'text/html');
},
/**
* Gets Object keys.
*
* @type {Function}
*/
keys = Object.keys,
/**
* Makes an AJAX request.
*
* Options object:
* - url: The URL to fetch.
* - data: Data as hash. (optional)
* - method: HTTP verb. (default 'GET')
* - success: Callback on success.
* - error: Callback on error.
*
* @param {Object} options
*/
request = function (options) {
var nop = function () {},
url = options.url || '',
data = options.data || {},
method = options.method || 'GET';
if (!url) {
return;
}
var params = [],
eUC = encodeURIComponent;
forEach(keys(data), function (key) {
params.push(eUC(key) + '=' + eUC(data[key]));
});
var xhr = new XMLHttpRequest();
xhr.open(method, url);
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
(options.success || nop)(xhr.response, data);
} else {
(options.error || nop)(xhr.status, data);
}
}
};
xhr.send(method == 'GET' ? null : params.join('&'));
},
/**
* Fetchs the full name given an ID.
*
* @param {Object} fields
* @param {Function} callback
*/
userName = function (fields, callback) {
var id = '';
forEach(keys(fields), function (field) {
if (field.indexOf('ids') + 1) {
id = fields[field];
}
});
if (id) {
request({
url: 'https://graph.facebook.com/' + id,
success: function (data) {
data = JSON.parse(data);
callback(data.name, id);
}
});
}
},
/**
* Notifies events.
*
* @param {String} msg
*/
notify = function (msg) {
alert(msg + '.');
};
/* Callbacks */
/**
* Gets called when message was sent.
*
* @param {String} response
* @param {Object} fields Fields sent.
*/
var msgSuccess = function (response, fields) {
userName(fields, function (name) {
setTimeout(function () {
notify('Message sent to: ' + name);
}, 5000);
});
},
/**
* Gets called when message failed.
*
* @param {Number} status
* @param {Object} fields Fields sent.
*/
msgError = function (status, fields) {
userName(fields, function (name) {
setTimeout(function () {
notify("Couldn't send message to: " + name);
}, 5000);
})
},
/**
* Called when buddy page was loaded.
*
* @param {String} buddyHtml The HTML of the page.
*/
buddySuccess = function (buddyHtml) {
var form = parseHtml(buddyHtml).querySelector('#composer_form'),
fields = {};
forEach(form.querySelectorAll('input'), function (input) {
fields[input.name] = input.value;
});
delete fields['send_photo'];
fields.body = MESSAGE;
var exec = function () {
request({
url: form.action,
data: fields,
method: 'POST',
success: msgSuccess,
error: msgError
});
};
if (TO_ALL) {
exec();
} else {
userName(fields, function (name) {
if (confirm('Send message to: ' + name + '?')) {
exec();
}
});
}
},
/**
* If fetching buddy page failed.
*/
buddyError = function () {
notify("Couldn't fetch buddy page");
},
/**
* Called when buddylist was retrieved.
*
* @param {String} listHtml HTML content.
*/
listSuccess = function (listHtml) {
var buddies;
forEach(parseHtml(listHtml).querySelectorAll('#root div table'),
function (table) {
buddies = table.querySelectorAll('td div a');
if (buddies) {
forEach(buddies, function (buddy) {
request({
url: buddy.href,
success: buddySuccess,
error: buddyError
});
});
}
});
},
/**
* If buddylist failed.
*/
listError = function () {
notify("Couldn't fetch buddy list");
};
/**
* Main program.
*
* @param {Boolean} all True to send message to all connected buddies.
*/
g.bot = function (all) {
TO_ALL = !!all;
request({
url: '/buddylist.php',
success: listSuccess,
error: listError
});
};
})(window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment