Scott Rutherford

Life on and off the Rails

 

Facebook Friend Invites

November 27th, 2007

I have been messing around with Facebook apps recently and I found a couple of articles to be very helpful:

Tutorial On Developing A Facebook Platform Application With Ruby on Rails

Continuing Facebook Applications With Ruby on Rails

Fist in your Facebook

I especially liked the Mime type for fbml that is from the last post, simply add:

Mime::Type.register 'text/html', :fbml
ActionController::MimeResponds::Responder::DEFAULT_BLOCKS[:fbml] = %(lambda { render :action => "\#{action_name}_fbml" })

to environment.rb and off you go.

The first two were very helpful in getting things going but seemed to make some really simple things very complicated. One was invites. Facebook supply a very nice fbml helper for creating invite pages the fb:multi-friend-selector.

This can be used like so:

<fb:request-form action="http://apps.facebook.com/myapp" method="POST" type="MyApp" content="<%= render :partial => 'invite_fbml' %>">
    <fb:multi-friend-selector actiontext="Select the friends you want to install MyApp" exclude_ids="<%= @friends_with_app %>"/>
</fb:request-form>

Where the invite partial looks like:

<fb:name uid='<%= @current_fb_user.facebook_uid %>' firstnameonly='true' shownetwork='false'/> wants you to install MyApp
<fb:req-choice url='http://www.facebook.com/add.php?api_key=APIKEY' label='Install MyApp'/>

and the controller method goes a bit like:

def index
  fql =  "SELECT uid, name FROM user WHERE uid IN" <<
  "(SELECT uid2 FROM friend WHERE uid1 = #{@current_fb_user.facebook_uid}) AND has_added_app = 1" 
  friends = fbsession.fql_query :query => fql
  ids = []
  (friends/:user).each do|u| 
    ids << (u/:uid).inner_html
  end
  @friends_with_app = ids.join(',')

  respond_to do |format|
    format.html # index.rhtml
    format.fbml # index_fbml.rhtml - facebook
  end
end  

Put it all together and you get something like:

Ta da!! You can also provide a post invite hook which will be the action provided to the facebook form element.

Now go and install Nothing to see it in action.

Leave a Reply

This blog used the Shay theme as a base and is powered by Mephisto