Saturday, November 29, 2008

Making sense out of Facebook feeds - Part one: Template Bundles

I'm trying to make posting content to a Facebook feed work and have run into quite a few problems finding a good tutorial that actually works. So I'm posting these notes for myself and anyone else who might find them useful:

In order to publish a item from a Facebook application to a users feed you need to follow a couple of steps:

1. Create a "Template Bundle": This is a template that contains up to three types of feed templates, a title only (one line), a title and short content (short story) or a longer amount of content (full story). The template contain "tokens" which are essentially placemarks where dynamic content will be inserted once a user actually publishes content to their feed using this template. Tokens have this format {*name-of-token*}. {*actor*} and {*target*} are two reserved tokens and if used will be replaced with the name of the facebook user {*actor*} and/or the target of the action {*target*} such as : "Joe tagged Joanie". There are additional reserved words that can't be used for token names such as "flash", "images", "mp3" and "video".

Here is an example of a php page that creates and registers a template bundle. In this example we will use the {*actor*} token and also create a another token called {*author*}:


<?php

// create the different feed templates

// one line feed template

$one_line_story = array('{*actor*} saw a great video from {*author*}!');

//short story template

$short_story = array(

'template_title' => '{*actor*} saw a great short video from {*author*}!',

'template_body' => 'Check this video out >'

);



//full story template

$full_story = array(

'template_title' => '{*actor*} saw a great full video from {*author*}!',

'template_body' => 'Check it out! I found on <a href="http://www.youtube.com">YouTube</a>: <br/>'

);



// now register the bundle

$res = $facebook->api_client->feed_registerTemplateBundle($one_line_story, $short_story, $full_story);

// this is the template bundle id

echo $res

?>



2. Once you've registered this bundle you can save the id you got in the last line:
 echo $res 
or go to the Facebook Developer "Registered Templates Console" (http://developers.facebook.com/tools.php?templates) and get the ID for the particular template you want to use later.

3. Now you are ready to set the feed.

4. Lets say that you have a canvas page where you post a number of videos from YouTube and CNN. On the page you want to allow the user to let people know which one they looked at--such as: "Bill watched a video from CNN". To do this you need to create a form on your page with a required tag fbtype="feedStory" and the action param that points to a php page that will process the feed content and post it for you:

<form fbtype="feedStory" action="http://apps.facebook.com/myapp/set-feed-item.php"> 

<select size=2 name="author">

<option value="" selected="true">Select an author</option>

<option value="CNN" > CNN </option>

<option value="YouTube"> YouTube</option>

</select>

<input type="submit" label="Submit" /> </form>


5. Now here is the php processing page:


<?php

$feedReturn = array(

'content'=> array(

'feed'=> array(

'template_id'=>100566785645,

'template_data'=> array (

'author'=>$_POST['author']

// add additional tokens here

)

)

),

'method'=>'feedStory'

);
echo json_encode($feedReturn);

 
?>


Once the user has submitted the form from the canvas page, this page will process the information and post the feed. However, this sample will only post text. While I have successfully implemented adding an image to a short story feed post, I have been unable to add flash, videos and mp3s - despite following the Facebook documentation closely. Hopefully I will be able to figure this out shortly and add to this post later...

5 comments:

Unknown said...

Wow, thanks for posting this man. I've been trying to figure out this whole feed publishing thing for the past 3 hours now, and your post really put some pieces together for me.

Even with the new Feed Template Console, the documentation is lacking. This helps a lot!

Jonesy said...

Argh!!! Why is this so hard? I don't get it.

I'm to the point where everything seems to be in place and I don't get any error messages... but nothing is posting to my wall either. I'm not even getting the dialog to ask me if I want to post.

I have my feed template registered, my processing page set up with the form pointing to it... Am I missing something?

Unknown said...

put your call in a try catch block. chances are there's an exception, and the error messages in the exception are more useful than the documentation.

Developer said...

can you please tell me how can I add an image to a short story feed post,

Anonymous said...

Thank you thank you thank you! I agree, the documentation is seriously lacking in this area. If I find anything else out while trudging ahead, I'll be sure to post :)