Permalink
Browse files

update the API

  • Loading branch information...
1 parent ec6ec87 commit 9191c428e44522ff7d7618bcc8f5e18c954ac939 @chrismatthieu chrismatthieu committed Feb 26, 2011
Showing with 68 additions and 37 deletions.
  1. +16 −10 README
  2. +43 −16 app/controllers/api_controller.rb
  3. +2 −2 app/views/reminders/_form.html.erb
  4. +1 −1 app/views/reminders/index.html.erb
  5. +4 −1 db/schema.rb
  6. +2 −7 tropo_scripting_api.rb
View
26 README
@@ -1,25 +1,31 @@
== Welcome to the Tropo Reminder Demo
-This is a Tropo application designed to demonstrate building a reminder application using both Voice and SMS alerts.
+This is a Tropo application designed to demonstrate building a reminder 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.
-== Create application steps or your could simply clone it from github :)
+== Installing this application on your localhost
-rails new Reminders -d mysql
-rails g scaffold Reminder name:string message:text phonenumber:string appointment:datetime
+Clone app from GitHub
+
+bundle install
rake db:create
rake db:migrate
-delete public/index.html
-change root :to => "welcome#index" in config/routes.rb to root :to => "reminders#index"
+== Create Tropo app using the Scripting API
+
+Setup an account at Tropo (http://tropo.com)
-Add gem 'rest-client' to Gemfile and run bundle install
+Create a new application
+
+Copy and paste 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.
-rails g controller api
+Add a phone number to your application. SMS messages will not work without one.
+== Running your application
Open your web browser to http://localhost:3000
-== Create Tropo App using the Scripting API
+Setup Cron job to call http://localhost:3000/api/check every 5 minutes
+
+
-Copy and paste 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.
@@ -1,30 +1,49 @@
class ApiController < ApplicationController
def check
- @reminders = Reminder.where("appointment > ?", Time.now)
+ @reminders = Reminder.where("(appointment > ?) && (appointment < ?) && ( ISNULL(flag1) || ISNULL(flag2) || ISNULL(flag3))", Time.now.utc, Time.now.utc+1.week)
@reminders.each do |reminder|
-
- if reminder.appointment-1.day > Time.now
-
- # Place outbound reminder call
- RestClient.get 'http://api.tropo.com/1.0/sessions', {:params => {
- :action => 'create',
- :token => '3d5eed33429706408efcc0e92307b044ba2ecb3c2e641f69f9179b54e9d04af908e583112195de9ec7b05b9e',
- :phonenumber => reminder.phonenumber,
- :remindermessage => reminder.message}}
-
+
+ if reminder.appointment-1.hour < Time.now.utc
+
+ # Send SMS
+ RestClient.get 'http://api.tropo.com/1.0/sessions', {:params => {
+ :action => 'create',
+ :token => '848b6b17c6229844827847b381a4a7e25c72d0750ec751bc0d10072e58eaa12683b6ea0a8aa5e166ee7bfcc8',
+ :phonenumber => formatphone(reminder.phonenumber),
+ :remindermessage => reminder.message + ' at ' + reminder.appointment}}
+
else
- # Send SMS
+ # Place outbound reminder call
RestClient.get 'http://api.tropo.com/1.0/sessions', {:params => {
:action => 'create',
- :token => '848b6b17c6229844827847b381a4a7e25c72d0750ec751bc0d10072e58eaa12683b6ea0a8aa5e166ee7bfcc8',
- :phonenumber => reminder.phonenumber,
- :remindermessage => reminder.message}}
+ :token => '3d5eed33429706408efcc0e92307b044ba2ecb3c2e641f69f9179b54e9d04af908e583112195de9ec7b05b9e',
+ :phonenumber => formatphone(reminder.phonenumber),
+ :remindermessage => reminder.message + ' at ' + reminder.appointment}}
end
-
+
+ # Write flag
+ if reminder.appointment-1.hour < Time.now.utc
+ # Write Flag3
+ @reminder = Reminder.find(reminder.id)
+ @reminder.flag3 = true
+ @reminder.save
+
+ elsif reminder.appointment-1.day < Time.now.utc
+ # Write Flag2
+ @reminder = Reminder.find(reminder.id)
+ @reminder.flag2 = true
+ @reminder.save
+
+ elsif reminder.appointment-1.week < Time.now.utc
+ # Write Flag1
+ @reminder = Reminder.find(reminder.id)
+ @reminder.flag1 = true
+ @reminder.save
+ end
end
if @reminders
@@ -33,4 +52,12 @@ def check
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
end
@@ -20,12 +20,12 @@
<%= f.text_area :message %>
</div>
<div class="field">
- <%= f.label :phonenumber %> (include country code i.e. 16025551212)<br />
+ <%= f.label :phonenumber %> <br />
<%= f.text_field :phonenumber %>
</div>
<div class="field">
<%= f.label :appointment %><br />
- <%= f.datetime_select :appointment %>
+ <%= f.datetime_select :appointment %> UTC
</div>
<div class="actions">
<%= f.submit %>
@@ -16,7 +16,7 @@
<td><%= reminder.name %></td>
<td><%= reminder.message %></td>
<td><%= reminder.phonenumber %></td>
- <td><%= reminder.appointment %></td>
+ <td><%= reminder.appointment %> UTC</td>
<td><%= link_to 'Show', reminder %></td>
<td><%= link_to 'Edit', edit_reminder_path(reminder) %></td>
<td><%= link_to 'Destroy', reminder, :confirm => 'Are you sure?', :method => :delete %></td>
View
@@ -10,7 +10,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20110224140331) do
+ActiveRecord::Schema.define(:version => 20110226001423) do
create_table "reminders", :force => true do |t|
t.string "name"
@@ -19,6 +19,9 @@
t.datetime "created_at"
t.datetime "updated_at"
t.string "phonenumber"
+ t.boolean "flag1"
+ t.boolean "flag2"
+ t.boolean "flag3"
end
end
View
@@ -1,23 +1,18 @@
# Create a new Tropo Scripting API application and attached these two scripts to it for voice and sms messaging
# Attach this script to the voice token
-#http://api.tropo.com/1.0/sessions?action=create&token=3d5eed33429706408efcc0e92307b044ba2ecb3c2e641f69f9179b54e9d04af908e583112195de9ec7b05b9e&phonenumber=14803194368&remindermessage=hello+world
+#http://api.tropo.com/1.0/sessions?action=create&token=3d5eed33429706408efcc0e92307b044ba2ecb3c2e641f69f9179b54e9d04af908e583112195de9ec7b05b9e&phonenumber=14805551212&remindermessage=hello+world
message($remindermessage, {
:to => $phonenumber,
:channel => "VOICE"
})
-# call('tel:+' + $phonenumber, {
-# :onAnswer => lambda { |event|
-# say $remindermessage
-# }}
-# )
# Attach this script to the SMS token
-#http://api.tropo.com/1.0/sessions?action=create&token=848b6b17c6229844827847b381a4a7e25c72d0750ec751bc0d10072e58eaa12683b6ea0a8aa5e166ee7bfcc8&phonenumber=14803194368&remindermessage=hello+world
+#http://api.tropo.com/1.0/sessions?action=create&token=848b6b17c6229844827847b381a4a7e25c72d0750ec751bc0d10072e58eaa12683b6ea0a8aa5e166ee7bfcc8&phonenumber=14805551212&remindermessage=hello+world
message($remindermessage, {
:to => $phonenumber,

0 comments on commit 9191c42

Please sign in to comment.