<?xml version="1.0" encoding="UTF-8"?>
<post>
  <author-id type="integer">2</author-id>
  <body>I thought I'd continue the Facebooker theme, from my "previous post":http://gamutworks.net/posts/8, by extending it to include SMS. There is a lack of documentation on the Facebook developer wiki and Facebooker Rdocs, so here's a short, simple, high level how-to.

To keep things consistent, I'm going to be sticking with the same theme as the previous post. That is, reminding students about assignment deadlines.

h3. Assumptions

1) You've got a Facebook application up and running using Facebooker
2) Your app requires access to a Facebook user's profile
3) Your running OSX 10.5 or higher

h3. Lets go already

Open up the model that's handling your notifications and add the following:

&lt;pre lang="ruby"&gt;&lt;code&gt;
#app/models/assignment.rb

class Assignment &lt; ActiveRecord::Base
  belongs_to :course 

  named_scope :next, lambda { { :conditions =&gt; ['due_date &gt; ?', Date.today], :limit =&gt; 1 } }



  def self.send_sms
    fbook_sms_session = Facebooker::Mobile.new(Facebooker::Session.create)
    for user in User.all
      self.next.each do |a|
        fbook_sms_session.send(user, "#{a.title} is due in #{a.course.title} tomorrow.")
      end
    end 
  end
end
&lt;/code&gt;&lt;/pre&gt;

All this is doing, is creating a Facebooker mobile session, which is required when dealing with SMS, using the public class method, create, given to us by the Facebooker::Session class.

We then proceed to loop through all of our users, invoke our session, and call send, a public instance method of the Facebooker::Mobile class, which takes two params: 1) uids 2) a message.

At this point, you'll need to sign up for Facebook Mobile. This can be setup in your Facebook account settings.

Next, you need to ask your users to opt in to receive SMS from your application. To do this, add the following wherever you'd like in your views:

&lt;pre lang="ruby"&gt;&lt;code&gt;
&lt;%= fb_prompt_permission ('sms', "Click here if you'd like to receive SMS notifications from this application") %&gt;
&lt;/code&gt;&lt;/pre&gt;

Alright, now that we have everything in place we can test drive sending an SMS to our users. Load up script/console and fire the method:

&lt;pre lang="text"&gt;&lt;code&gt;
$ script/console
$ Assignment.send_sms
&lt;/code&gt;&lt;/pre&gt;

Voila!



</body>
  <category>Ruby on Rails</category>
  <created-at type="datetime">2010-02-02T05:26:56Z</created-at>
  <excerpt>Adding SMS features to your Facebook application with Rails.</excerpt>
  <id type="integer">10</id>
  <private type="boolean">false</private>
  <published-at type="datetime">2010-02-01T00:00:00Z</published-at>
  <tags>Facebooker, SMS, Ruby on Rails</tags>
  <title>Part 2: Adding SMS to your Facebooker Application</title>
  <updated-at type="datetime">2010-02-02T06:32:34Z</updated-at>
</post>
