Part 2: Adding SMS to your Facebooker Application
February 01, 2010
I thought I’d continue the Facebooker theme, from my previous post, 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.
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
Lets go already
Open up the model that’s handling your notifications and add the following:
#app/models/assignment.rb
class Assignment < ActiveRecord::Base
belongs_to :course
named_scope :next, lambda { { :conditions => ['due_date > ?', Date.today], :limit => 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
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:
<%= fb_prompt_permission ('sms', "Click here if you'd like to receive SMS notifications from this application") %>
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:
$ script/console
$ Assignment.send_sms
Voila!