This Post is a tutorial for facebook api lovers.This script invites the facebook friends from the facebook to the website.Most facebook scripts in the internet does not handle the batch request for this invite api.Thus i did a script which sends invite request to the facebook friends in a batch of 50(Only 50 is allowed by facebook in one batch).
Technology’s used in the tutorial are
- Jquery
- facebook api
window.fbAsyncInit = function() {
FB.init({
appId : 'app_id', // App ID, change your app id above also. Else it wont work.!
frictionlessRequests : true,
channelUrl : '//'+window.location.hostname+'/test/', // Path to your Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
// inituializations
/////////////////////////////////////////////////////////////////////////////////////////////
FB.Event.subscribe('auth.login', function(response) {
//$("#loader").fadeIn();
//alert("response change");
FB.api('/me', function(me){
if (me.email)
{
user_status="connected";
}
else
{
alert("You have restricted by giving email! sorry,So you are unable to login");
//$("#loader").fadeOut();
//window.location.href=window.location.href;
}
})
});
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
user_status="connected";
$("#cover").fadeOut();
$("#popup_login").fadeOut();
} else if (response.status === 'not_authorized') {
user_status="not_authorized";
} else {
//not loged in
user_status="not_log_in"
}
});
FB.Event.subscribe('auth.logout', function(response){
if(email!="")
{
email="";
profilelink="";
profilepic="";
username="";
}
});
};
/////////////////////////////////////////////////////////////////////////////////////////////
//ASYNCHROMOUS LOADINGS OF API
(function(d){
var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
d.getElementsByTagName('head')[0].appendChild(js);
}(document));
//ASYNCHROMOUS LOADINGS OF API
STEP4: Inviting friends with batch processing is done with a click event of a button.
$(document).ready(function()
{
$("#popup_login").hide();
$("#spread_it").hide();
$("#cover").hide();
ht=$(window).height();
$("#cover").css('height',ht);
});
var splitup,res,initial,final,max,response,backup_response;
function spread()
{
// $("ajax_loader").animate({"width": "400"});
flag=true;
if(user_status=="connected")
{
$("#spread_it").hide();
$("#popup_login").hide();
wt=$(window).width();
wt=wt/2;
wt=wt-330;
$("#spread_it").css('left',wt);
ht=$(document).height();
$("#cover").css('height',ht);
location.href="#topbg";
$("#cover").fadeIn();
FB.api('/me', function(me){
// alert("loged in");
profilepic="<img src=http://graph.facebook.com/" + me.id + "/picture />";
profilelink=me.link;
email=me.email;
username=me.name;
FB.api(
{
method: 'fql.query',
query: 'SELECT uid FROM friendlist_member WHERE flid IN (SELECT flid FROM friendlist WHERE owner=me() AND type="current_city" )'
},
function(response) {
backup_response=response;
flag_once=true;
flag_once_split=true;
max=response.length;
// alert("max"+max);
document.getElementById('total').innerHTML = max;
x=max/50
splitup=400/x;
increment=splitup;
//splitup=Math.round(splitup);
calculatingbatch(response,0,49);
}
);
})
}
else if(user_status=="not_authorized")
{
alert("sorry you have not authorized for it");
}
else
{
popup_login();
}
}
initial=0;
final=50;
/////////////////////////////////////////////////////////////////////////////////////////////
function calculatingbatch(response,initial,final)
{
// alert(response[100].uid)
res="";
// $('#ajax_loader').css("width",splitup);
if(flag_once_split)
{
flag_once_split=false;
}else{
splitup=splitup+increment;
}
for(i=initial;i<final;i++)
{
res=res+','+backup_response[i].uid;
}
sendrequest(res,initial,final,response);
}
/////////////////////////////////////////////////////////////////////////////////////////////
function moretext()
{
$("#ajax_loader").css("width",splitup);
}
function sendrequest(res,initial,final,response)
{
//alert("res-"+res);
FB.ui({method: 'apprequests',message: 'A awsome website for facebook API',to:res }, requestCallback);
}
function requestCallback(callbackresponse)
{
if(callbackresponse && callbackresponse.request)
{
document.getElementById('sent').innerHTML = final;
$("#spread_it").fadeIn();
var t=setTimeout('moretext()',300);
} else
{
alert("You havent invited your facebook friends");
$("#cover").fadeOut();
}
}
function popup_login()
{
ht=$(document).height();
$("#cover").css('height',ht);
location.href="#topbg";
wt=$(window).width();
wt=wt/2;
wt=wt-350;
$("#popup_login").css('left',wt);
$("#cover").fadeIn();
$("#popup_login").fadeIn();
}
function continue_spread(response)
{
$("#spread_it").fadeOut();
initial=initial+50;
final=final+50;
if(final<=max)
{
calculatingbatch(response,initial,final);
}
else
{
if(flag_once)
{
flag_once=false;
initial=final-50;
final=max-initial;
final=initial+final;
diff=final-initial;
percentage=(diff/50)*100;
tobeadded=(percentage/100)*increment;
splitup=splitup+tobeadded;
flag_once_split=true;
calculatingbatch(response,initial,final);
}
else
{
//$('#ajax_loader').css("width",splitup);
alert("Sent to all your friends");
$("#cover").fadeOut();
$("#spread_it").fadeOut();
}
}
}
function close_spread()
{
$("#cover").fadeOut();
$("#spread_it").fadeOut();
alert("Havent Sent to all your friends");
}
NOTE: You should update your app id in the following places in index.php in the download script
FB.init({
appId : 'app_id', // App ID, change your app id above also. Else it wont work.!
frictionlessRequests : true,
channelUrl : '//'+window.location.hostname+'/test/', // Path to your Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/all.js#xfbml=1&appId=app_id";// CHANGE THE APP ID HERE ALSO
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
Your batch friends inviter is ready.For any queries comment below.



