Facebook: Inviting people to your application from within actionscript

Facebook: Inviting people to your application from within actionscript

invite_popup1I tried hard to figure this out and I found very little information on this topic on the Internet so this is why i wrote this article. Did you ever try to send someone an invitation to use your app? Like Farmville does with sending gifts? Then you probably stumbled upon the multi-friends-selector fbml tag. In my case i wanted to only invite one specific person via Actionscript. After much research on google i gave it up. There seems to be no way to do this from the API. But i found a good alternative which works as follows: You call a JavaScript function on your Canvas page from your Flash application. This JavaScript function pops up a dialog which includes a request from fbml tag. This dialog looks like the one in the picture on the left.

<fb:request-form action='index.php' method='POST' invite='true' type='Yor Application Name' content='Some comment here... <?php echo htmlentities("<fb:req-choice url=\"http://apps.facebook.com/musicplaylists/\" label=\"Button Caption for Accept\"") ?>' >

<fb:request-form-submit uid='xxxxx' label='%n is not using YOUR APP NAME. Klick here to invite him/her'/>

</fb:request-form>

invite_popup2The code above  generates a Button. When the user clicks on it it opens a popup where one can enter a optional message and send the invitation. But how is this supposed to work in Actionscript? Actually it isn’t. But we can do a workaround. We can open a popup including this button. Its not the user-friendliest solution but it is a good one if you need to invite specific persons. (This person is specified by the uid attribute of the request-form-submit tag)

So we could write a JavaScript (FBJS) function to open such a dialog with the request-form in it. Unfortunately there is a problem: Because Facebook has hard restrictions for Javascript we are not able to change the value of the uid attribute of the request-form-submit tag :( But there is a workaround: We can load the fbml tags per Ajax and fill them in our dialog:

<fb:js-string var="dialogInvite">
	<div id="dialog_content">
		<div class="dialog_loading">Loading...</div>
	</div>
</fb:js-string>

<script type='text/javascript'>
<!--
	function onRequestComplete(result) {
            document.getElementById('dialog_content').setInnerFBML(result);
        }

        function do_adduser(uid) {
            var ajax = new Ajax();

            ajax.responseType = Ajax.FBML;
            ajax.ondone = onRequestComplete;

            ajax.requireLogin = true;
	    new Dialog(Dialog.DIALOG_POP).showMessage('Invite friends', dialogInvite,button_cancel='Cancel');
            ajax.post('http://URL TO YOUR SERVER.de/invite.php?uid='+uid);
        }
-->
</script>

The code of invite.php looks like this:

<fb:request-form action='index.php' method='POST' invite='true' type='Yor Application Name' content='Some comment here... <?php echo htmlentities("<fb:req-choice url=\"http://apps.facebook.com/musicplaylists/\" label=\"Button Caption for Accept\"") ?>' >

<fb:request-form-submit uid='<?php echo $_GET["uid"]?>' label='%n is not using YOUR APP NAME. Klick here to invite him/her'/>

</fb:request-form>

When you call do_adduser(13234); it would open a popup asking you to invite the user with id 13234. In Order to call this JavaScript function from within your ActionScript program you have to insert a fbjs-bridge tag before your swf tag and an empty script tag with an empty comment in it:

<fb:fbjs-bridge/>
<fb:swf swfsrc="http://URL TO YOUR SERVER.de/flashapp.swf"/>
<script><!-- --></script>

Now you can call do_adduser(123) from actionscript:

var lconn:LocalConnection = new LocalConnection;

lconn.send(LoaderInfo(root.loaderInfo).parameters.fb_local_connection, "callFBJS", "do_adduser", [123]);

LoaderInfo(root.loaderInfo).parameters.fb_local_connection will need the fbjs-bridge working correctly.

I hope this post helped you :)

<fb:request-form action='index.php' method='POST' invite='true' type='Yor Application Name' content='Some comment here... <?php echo htmlentities("<fb:req-choice url="http://apps.facebook.com/musicplaylists/" label="Button Caption for Accept"") ?>' >

<fb:request-form-submit uid='xxxxx' label='%n is not using YOUR APP NAME. Klick here to invite him/her'/>

</fb:request-form>

About the Author

Studying MultimediaTechnology in Salzburg, Austria