Example: Pill Reminder

When my wife was pregnant she asked if I would help her remember to take her prenatal pills every evening.  Since I probably wouldn’t remember myself, I decided to create an Asterisk app that would call her every day at 9:00pm and ask if she had taken her pills.  She could press 1, 2, or 3, depending on whether she’d taken her pills.

  1. “Yes”.  Asterisk would schedule a call for the next day at 9:00pm
  2. “Remind me in 1 hour” Asterisk would schedule a call for 1 hour later.
  3. “Remind me in X hours”  When prompted, she could enter in a number for how many hours to wait.  For example, “2” would schedule the call for 2 hours later.

The Asterisk dialplan handled most of the interaction, and a Ruby script was run at the end of the call to generate the next call file, and timestamp it with the appropriate time to call.

It’s worth pointing out my use of the “h” extension, which will run when the caller hangs up.  By running the script after the call is over, I guarantee that the script will run.  If it’s included elsewhere in the dialplan, then there’s a chance that the user may hang up before reaching the script.

Asterisk extensions.conf dialplan:

[pill-call]
exten => s,1,Answer()
exten => s,n,Set(calltime=-1) ;-1 means call next day
exten => s,n,Wait(1);
exten => s,n,Background(/root/pillcall/audio/did-you-take-pills)
exten => s,n(press123),Background(/root/pillcall/audio/press123)
exten => s,n,WaitExten(5)
exten => 1,1,Playback(/root/pillcall/audio/thanks)
exten => 1,n,Hangup()
exten => 2,1,Set(calltime=1)
exten => 2,n,Playback(/root/pillcall/audio/call_1_hour)
exten => 2,n,Hangup()
exten => 3,1,Goto(pillcall-choose-hour,s,1)
exten => t,1,Goto(2,1)
exten => i,1,Goto(s,press123)
exten => h,1,System(/root/pillcall/script/gen_pill_call.rb 19175849999 ${calltime})

[pillcall-choose-hour]
exten => s,1,Set(calltime=1)
exten => s,n,Background(/root/pillcall/audio/pick-next-call)
exten => s,n,WaitExten(5)
exten => _X,1,Set(calltime=${EXTEN})
exten => _X,n,Hangup()
exten => t,1,Goto(s,1)
exten => i,1,Goto(s,1)
exten => h,1,System(/root/pillcall/script/gen_pill_call.rb 19175849999 ${calltime})

gen_pill_call.rb call file generator.

#!/usr/bin/ruby
require 'fileutils'
# Get Arguments
phone_number_to_call = ARGV[0]
hours = ARGV[1].to_i
hour_in_seconds = 60*60
day_in_seconds = hour_in_seconds*24
default_hour = 21 #9pm
#year [, month, day, hour, min, sec, usec]
time_to_call = Time.now
if hours > 0
	hour = Time.now.hour+hours
	if hour > 24
		#call the next day at default hour
		time_to_call = Time.local(Time.now.year,Time.now.month,Time.now.day,default_hour,0,0)
		time_to_call = time_to_call + day_in_seconds #roll forward 1 day
	else
		time_to_call = time_to_call + (hour_in_seconds *hours)
	end
else
	#call the next day at default hour
	time_to_call = Time.local(Time.now.year,Time.now.month,Time.now.day,default_hour,0,0)
	time_to_call = time_to_call + day_in_seconds #roll forward 1 day
end

temp_dir = "/tmp/"
callfile = "call_#{time_to_call.to_i}.call"
startcallfile = temp_dir + callfile
end_dir = "/var/spool/asterisk/outgoing/"
endcallfile = end_dir + callfile
#write file to disk
file = File.open(startcallfile,"w")
file.puts("Channel: SIP/#{phone_number_to_call}@flowroute\n")
file.puts("MaxRetries: 2\n")
file.puts("RetryTime: 30\n")
file.puts("WaitTime: 60\n")
file.puts("Context: pill-call\n")
file.puts("Extension: s\n")
file.puts("CallerID: Kairalla <16466429999>\n")
file.close
#change file permission
File.chmod(0777, startcallfile)
FileUtils.chown(ENV['USER'],'asterisk',startcallfile)
File.utime(time_to_call,time_to_call,startcallfile) #change file time to future date
#move file to /var/spool/asterisk/outgoing
FileUtils.mv(startcallfile,endcallfile)