How to Build a Reminder Service Using Tropo and Ruby on Rails
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 installrake db:createrake db:migrateSetup 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.
message($remindermessage, { :to => $phonenumber, :channel => "VOICE" })Here is the source code to send an SMS message on Tropo.
message($remindermessage, { :to => $phonenumber, :network => "SMS" })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:
class ApiController < ApplicationController def check require 'time' @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 ) @reminders.each do |reminder| if reminder.appointment-1.hour < Time.now.utc && reminder.flag3.nil? # Send SMS RestClient.get 'https://api.tropo.com/1.0/sessions', {:params => { :action => 'create', :token => '848b6b17c6229844827847b381...58eaa12683b6ea0a8aa5e166ee7bfcc8', :phonenumber => formatphone(reminder.phonenumber), :remindermessage => 'dont forget ' + reminder.message.to_s + ' at ' + reminder.appointment.to_s}} # Write Flag3 @reminder = Reminder.find(reminder.id) @reminder.flag3 = true @reminder.save elsif reminder.appointment-1.day < Time.now.utc && reminder.flag2.nil? # Place outbound reminder call RestClient.get 'https://api.tropo.com/1.0/sessions', {:params => { :action => 'create', :token => '3d5eed33429706408efcc0e92307b...d04af908e583112195de9ec7b05b9e', :phonenumber => formatphone(reminder.phonenumber), :remindermessage => 'dont forget ' + reminder.message.to_s + ' at ' + reminder.appointment.to_s}} # Write Flag2 @reminder = Reminder.find(reminder.id) @reminder.flag2 = true @reminder.save elsif reminder.appointment-1.week < Time.now.utc && reminder.flag1.nil? # Place outbound reminder call RestClient.get 'https://api.tropo.com/1.0/sessions', {:params => { :action => 'create', :token => '3d5eed33429706408efcc0e92307b04...908e583112195de9ec7b05b9e', :phonenumber => formatphone(reminder.phonenumber), :remindermessage => 'dont forget ' + reminder.message.to_s + ' at ' + reminder.appointment.to_s}} # Write Flag1 @reminder = Reminder.find(reminder.id) @reminder.flag1 = true @reminder.save end end if @reminders render :text => "sent " + @reminders.length.to_s + " reminders" else render :text => "no reminders" end end def formatphone(phone) @phone = phone.gsub("(", "").gsub(")", "").gsub("-", "").gsub(".", "").gsub(" ", "") if @phone[0..0] != '1' @phone = '1' + @phone end return @phone end endNow 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 :)
Originally from Voxeo Blogs




Leave a Reply