Facebook Chat Invite API to Increase The Application Users.

Over the last few days, we have seen some major changes on Facebook developer platform which will increase the application developers to engage more users with their applications. I will try to write on them one by one in near future whenever I get time. Today, i’m going to discuss “Chat Invite API” with an example and I will also focus on area(s) how we can use this API to get more users attention.

Chat invite API Enables your users to initiate Facebook Chat with their friends from within your applications. Great! Let’s give this responsibility to users to bring their friends in our application.

Three main attributes of this API are..

  • msg (required)
  • condensed
  • exclude_ids

an example could be like the following..

<fb:chat-invite msg="let's play a game!" condensed="false" exclude_ids="1,2,3"/>

2 things are very important here, “msg” that comes in the chat input box when you select an online friend from the list, so be sure that you put something related to your application. Setting up the application URL will be right idea along with some text.

I.e :

 msg ="Come join me! <?=$base_url;?>"


another attribute is “exclude_id”, let’s exclude those who haven’t added this application and idle. So that we will show those users who have not added our application and who are idle. Let’s look at the complete example that does this job for us.

<?php 
include_once("config.php");
$users = $facebook->api_client->fql_query("SELECT uid FROM user WHERE is_app_user = 1 and uid IN (SELECT uid2 FROM friend WHERE uid1 = {$fbuser})");
/*
 * Now, let's make these ids comma seperated (1,2,3)
 */
$arFriends = "";
    // Build an delimited list of users...
    if ($users){
        $arFriends .= $users[0]["uid"];
        for ( $i = 1; $i &lt; count($users); $i++ )    {
            if ( $arFriends != "" )        $arFriends .= ",";
            $arFriends .= $users[$i]["uid"];
        }
    }
?>
//now place this code where you want to show your chat invite box.
<fb:js-string var="chatInvite">  <fb:chat-invite msg="Come join me! <?=$base_url;?>" condensed="true" exclude_ids="<?=$arFriends;?>"/> </fb:js-string>
    <div id="chat_invite_container"></div>
<script> document.getElementById('chat_invite_container').setInnerFBML(chatInvite); </script>
Chat with friends from Canvase Page

Chat with friends from Canvase Page

Thats it! Now, we can allow users to talk to their friends directly from your application.

Reference : http://wiki.developers.facebook.com/index.php/Fb:chat-invite

This entry was posted in API, Facebook, PHP. Bookmark the permalink.
  • arkofdan

    nice
    Can you please put the entire page code for this example please? I really need it

  • http://junalontherun.com/ junal

    All codes are here, what else you need? just config.php file is missing right. Thats your file that you create with secret key and api key. a config file may look like the follow …
    <?php
    include '../client/facebook.php';

    $api_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
    $secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
    $base_url = 'http://apps.facebook.com/app_name/&#39;;
    $callback_url = 'http://clickspan.com/f8apps/directory_name/&#39;;

    $facebook = new Facebook($api_key, $secret);
    $fbuser = $facebook->require_login();
    ?>

  • arkofdan

    Thanks man, I will use it ;)

  • http://www.facebooklicious.com/facebook-developers.php Facebook Developers

    I was really searching for this code. It really helpful for me and other application users. Thanks for sharring

  • Rahul

    Your “for” statement appears to have 4 conditions instead of 3. Perhaps the formatting went wrong??

  • Rahul

    Also it looks like facebook updated something because when i tried it this is what i got

    Fatal error: Uncaught exception 'FacebookRestClientException' with message 'Parser error: unexpected ')' at position 92.' in /home/<site>/facebookapi_php5_restlib.php:1648 Stack trace: #0 /home/<site>/includes/facebookapi_php5_restlib.php(267): FacebookRestClient->call_method('facebook.fql.qu…', Array) #1 /home/n<site>/index.php(14): FacebookRestClient->fql_query('SELECT uid FROM…') #2 {main} thrown in /home/<site>/includes/facebookapi_php5_restlib.php on line 1648

  • Rahul

    Your “for” statement appears to have 4 conditions instead of 3. Perhaps the formatting went wrong??

  • Rahul

    Also it looks like facebook updated something because when i tried it this is what i got

    Fatal error: Uncaught exception 'FacebookRestClientException' with message 'Parser error: unexpected ')' at position 92.' in /home/<site>/facebookapi_php5_restlib.php:1648 Stack trace: #0 /home/<site>/includes/facebookapi_php5_restlib.php(267): FacebookRestClient->call_method('facebook.fql.qu…', Array) #1 /home/n<site>/index.php(14): FacebookRestClient->fql_query('SELECT uid FROM…') #2 {main} thrown in /home/<site>/includes/facebookapi_php5_restlib.php on line 1648

  • Sudipta

    1) Fatal error: Uncaught exception 'FacebookRestClientException' with message 'Parser error: unexpected ')' at position 92.' in /home/<site>/facebookapi_php5_restlib.php:1648 Stack trace: #0 /home/<site>/includes/facebookapi_php5_restlib.php(267): FacebookRestClient->call_method('facebook.fql.qu…', Array) #1 /home/n<site>/index.php(14): FacebookRestClient->fql_query('SELECT uid FROM…') #2 {main} thrown in /home/<site>/includes/facebookapi_php5_restlib.php on line 1648

    2)chatInvite is not defined javascript error

  • helena01

    don't works; have some problem in this line

    for ( $i = 1; $i < count($users); $i++ ) {

    Parse error: syntax error, unexpected ';', expecting ')'

  • http://www.mbt-outlet-store.com mbt outlet

    Well , the view of the passage is totally correct ,your details is really reasonable and you guy give us valuable informative post, I totally agree the standpoint of upstairs. I often surfing on this forum when I m free and I find there are so much good information we can learn in this forum!
    http://www.sin-game.com

  • Ashok Gupta1984

    I want to add chat-invite from flash and my flash is calling by a iframe.php file what i will do for this

  • http://twitter.com/LondonHotelRese London H.Reservation

    ATTENTION: Want travel as 75 % less? Must Following ➜ http://www.facebook.com/?sk=lf#!Or ➜http://www.twitter.com/#!/LondonHotelRese@LondonHotelRese and Tweet us we have 1000.000 offers around the world…

  • http://twitter.com/LondonHotelRese London H.Reservation

    ATTENTION: Want travel as 75 % less? Must Following ➜ http://www.facebook.com/?sk=lf#!Or ➜http://www.twitter.com/#!/LondonHotelRese@LondonHotelRese and Tweet us we have 1000.000 offers around the world…