Sunday 27 February 2011

How to Build a Reminder Service Using Tropo and Ruby on Rails | The Tropo Blog

Have you ever thought about starting a Reminder Service company and getting millions of dollars in funding all from a weekend project?  Tropo is here to help you with your communications needs in this endeavor!

The Reminder Service source code that I am about to share with you was started on the Nerd Bird (good to have in-flight WiFi) from Orlando to Phoenix, on my way back from the HIMSS conference.  It’s written in Ruby on Rails 3.0.4 and the Tropo Scripting API.

This is Tropo application is designed to demonstrate building a Reminder Service application using both Voice and SMS alerts.  The application will place an outbound reminder call to you 1 week in advance and then 1 day in advance and an SMS message 1 hour in advance of your appointment time.

You can demo the Reminder Service running on Heroku now.  It’s a basic Ruby on Rails scaffolding interface with the ability to create, update, and delete reminders.  All reminders are tracked in UTC time to simplify the demo and there is no authentication because IT’S A DEMO.

The source code is on GitHub.  Simply clone this application from GitHub and run the following statements from your command line:

bundle install
rake db:create
rake db:migrate

Setup an account at Tropo and create a new Scripting API application.  Copy and paste the source code from tropo_scripting_api.rb into separate scripts running under the same application. One for placing outbound voice call reminders and one for sending SMS message reminders.

Here is the source code to place the phone call on Tropo.

1message($remindermessage, {
2  :to => $phonenumber,
3  :channel => "VOICE"
4})

Here is the source code to send an SMS message on Tropo.

1message($remindermessage, {
2  :to => $phonenumber,
3  :network => "SMS"
4})

Add a phone number to your Tropo application.  SMS messages will not work without one.

The rest of the magic happens in the API controller as shown below:

01class ApiController < ApplicationController
02  def check
03    require 'time'
04 
05    @reminders = Reminder.where("(appointment > ?) and (appointment < ?) and ( flag1 IS NULL or flag2 IS NULL or flag3 IS NULL)", Time.now.utc, Time.now.utc+1.week )
06 
07    @reminders.each do |reminder|
08 
09      if reminder.appointment-1.hour < Time.now.utc && reminder.flag3.nil?
10 
11        # Send SMS
12        RestClient.get 'https://api.tropo.com/1.0/sessions', {:params => {
13          :action => 'create',
14          :token => '848b6b17c6229844827847b381...58eaa12683b6ea0a8aa5e166ee7bfcc8',
15          :phonenumber => formatphone(reminder.phonenumber),
16          :remindermessage => 'dont forget ' + reminder.message.to_s + ' at ' + reminder.appointment.to_s}}
17 
18        # Write Flag3
19        @reminder = Reminder.find(reminder.id)
20        @reminder.flag3 = true
21        @reminder.save
22 
23      elsif reminder.appointment-1.day < Time.now.utc  && reminder.flag2.nil?
24 
25        # Place outbound reminder call
26        RestClient.get 'https://api.tropo.com/1.0/sessions', {:params => {
27          :action => 'create',
28          :token => '3d5eed33429706408efcc0e92307b...d04af908e583112195de9ec7b05b9e',
29          :phonenumber => formatphone(reminder.phonenumber),
30          :remindermessage => 'dont forget ' + reminder.message.to_s + ' at ' + reminder.appointment.to_s}}
31 
32        # Write Flag2
33        @reminder = Reminder.find(reminder.id)
34        @reminder.flag2 = true
35        @reminder.save
36 
37      elsif reminder.appointment-1.week < Time.now.utc && reminder.flag1.nil?
38 
39        # Place outbound reminder call
40        RestClient.get 'https://api.tropo.com/1.0/sessions', {:params => {
41          :action => 'create',
42          :token => '3d5eed33429706408efcc0e92307b04...908e583112195de9ec7b05b9e',
43          :phonenumber => formatphone(reminder.phonenumber),
44          :remindermessage => 'dont forget ' + reminder.message.to_s + ' at ' + reminder.appointment.to_s}}
45 
46        # Write Flag1
47        @reminder = Reminder.find(reminder.id)
48        @reminder.flag1 = true
49        @reminder.save
50      end
51    end
52 
53    if @reminders
54      render :text => "sent " + @reminders.length.to_s + " reminders"
55    else
56      render :text => "no reminders"
57    end
58  end
59 
60  def formatphone(phone)
61    @phone = phone.gsub("(", "").gsub(")", "").gsub("-", "").gsub(".", "").gsub(" ", "")
62    if @phone[0..0] != '1'
63      @phone = '1' + @phone
64    end
65    return @phone
66  end
67end

Now for the drum roll! Setup a cron job to call http://localhost:3000/api/check every 5 minutes, run rails server, and open your web browser to http://localhost:3000!  Oh, one last thing…Go ask one of those “super angels” for some cash-money :)

ShareThis

Related posts:

  1. Create a Group SMS Service Using Tropo in 5 Lines of Code!
  2. Command Line SMS
  3. How to Build a VoIP-Based Baby Monitor
  4. Customer Service Hotline
  5. Scotty, We Need More Power!

Tags: , , , , , , ,

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Additional comments powered by BackType