Wednesday, January 12, 2011

Integrate FaceBook Chat Using AS3 and FaceBook fqlQuery and sending messages using facebook post Stream URL.



First of all you need to read my post on how to create new facebook application using AS3 by follow below  link:

in this tutorial I will show you two Integration point with facebook API:
  • getting Friend Status .
  • Sending Messages to you friend chat box.
To get your friends status you need to make sure that friends_online_presence and user_online_presence Permissions have been applied to facebook uiserver.php for your application as below sample :
var PermURL = "http://www.facebook.com/connect/uiserver.php?app_id=181390918539668&next=http://amazingwork.com/fbexampletest/index.html&display=page&locale=en_US&perms=friends_online_presence,publish_stream,user_online_presence&return_session=0&fbconnect=0&canvas=1&legacy_return=1&method=permissions.request"
now in order to get your friends status, you need to get the online_presence field from facebook User table using fqlQuery as below :
Facebook.fqlQuery("SELECT uid, online_presence FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = --please fill here the uer Id who you need to get his friends status--) ",LoadOnlineUsers)
private function LoadOnlineUsers(response:Object, fail:Object)
{
trace("-->"+fail)
var OnlineString =""
var OnlineUsers:Array = new Array()
for (var key:Object in response)
{
var oUsersOnlineClass:UsersOnlineClass= new UsersOnlineClass();
oUsersOnlineClass.UserID=response[key]["uid"]
oUsersOnlineClass.Status=response[key]["online_presence"]
OnlineUsers.push(oUsersOnlineClass)
}
}
package 
{
public class UsersOnlineClass
{
private var sUserID:String = "";
private var sStatus:String = "";
//Item Constructor
public function UsersOnlineClass()
{
}
//-----------------------------
public function get UserID():String {
            return sUserID;
        }
        
        public function set UserID(x:String):void {
            sUserID = x;    
        }
//-----------------------------
public function get Status():String {
            return sStatus;
        }
        
        public function set Status(x:String):void {
            sStatus = x;    
        }
}
}
as you can in the above code that I have get my frineds online status ( active, offline, null)and store them along with their Ids in an array to use it anywhere in my application .
-----------------------------------------------------------------------------
sending a new chat message from our AS3 application :

this is that hard part where you need to follow precisely below steps :
  •  the first step is to use facebook sending messages URL :
    • var url:String = "http://www.facebook.com/ajax/chat/send.php?__a=1";
  • the second step is to create URL post variable to send them to the above URL :
    • var request:URLRequest = new URLRequest(url);
              var requestVars:URLVariables = new URLVariables();
    requestVars.msg_text = "Message Text";
    requestVars.to = "friend ID";
    requestVars.msg_id = "Random Message ID";
    requestVars.client_time = new Date().getTime().toString();
    requestVars.post_form_id = "you can find this field by viewing facebook source page";and for me i am trying now to get this field in adynamic way using javascript function and pass it to flash ( no success until now cuase i am using Iframe :( "
                   request.data = requestVars;
                   request.method = URLRequestMethod.POST;
  • now to send these variables to the facebook message center URL we need to use URL Loader :
    • var urlLoader:URLLoader = new URLLoader();
    urlLoader = new URLLoader();
    urlLoader.dataFormat = URLLoaderDataFormat.TEXT;
                    urlLoader.addEventListener(Event.COMPLETE, loaderCompleteHandler, false, 0, true);
                    urlLoader.load(request);
    • all u need now is to handle the result data and decoded the data using JSON serialization to an object
      • function loaderCompleteHandler(e:Event):void {
                 var sResult:String = e.target.data as String
                         sResult= sResult.replace("for (;;);","")
                         var Res = JSON.decode(d,false)
                         trace(Res)}
        I hope this tutorial will help you to enrich your applications,
        for any question regarding this tutorial, please visit http://www.facebook.com/pages/Flash-Community/176320862386436.

        Create facebook Iframe Application Using flash AS3 API ( Updated Version ).

        please like and post your questions :  http://www.facebook.com/pages/FlashCS5-Tutorials/176320862386436 After I have received a lot of requ...