Ruby Call File Generator

There is a Ruby script which you are all free to use in the AGI directory: /var/lib/asterisk/agi-bin/ called gencallfile.rb.

This is a basic command line Ruby script that takes in a couple of arguments: The first argument is the number to call, the second the context to put the call in, the third is the extension in that context, the fourth is any variables you would like set and the fifth is the time you would like the call to occur in this this format: HH-MM-SS-MM-DD-YYYY. The second remaining arguments are optional and will default to our class context and the “s” extension without any variables and will immediately make the call. The first argument is required.

To execute this script from the command line, you do the following:

/var/lib/asterisk/agi-bin/gencallfile.rb 19178096659

or

/var/lib/asterisk/agi-bin/gencallfile.rb 19178096659 somecontext s somevar=somevalue 02-10-00-02-27-2012

To use this from Asterisk itself we can use our System command:

[sve204_callfile]
exten => s,1,System(/var/lib/asterisk/agi-bin/gencallfile.rb 19178096659);
exten => s,1,Goto(redial_sve204,s,1);

Here is the code (for your reference):

#!/usr/bin/ruby
require 'fileutils'
# Get Arguments
# First argument Number to Call
numbertocall = ""
if (ARGV.size > 0)
	numbertocall = ARGV[0]
else
	puts "USAGE: gencallfile.rb [phone-number] [context] [exten] [varname=value] [hour-minute-second-month-day-year]\n"
	exit(1)
end

# Context
context = ARGV[1] || "itp-redial"
# Extension
extension = ARGV[2] || "s"
#variables
vars = ARGV[3] || ""
#future time
touchtime = ARGV[4] || ""

time = (Time.now.to_f * 1000).to_i 	#current timestamp
temp_dir = "/tmp/"
callfile = "call_" + time.to_s + ".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/flowroute/" + numbertocall + "\n")
file.puts("MaxRetries: 1\n")
file.puts("RetryTime: 60\n")
file.puts("WaitTime: 30\n")
file.puts("Context: " + context + "\n")
file.puts("Extension: " + extension + "\n")
file.puts("Set: " + vars + "\n") unless vars == ""
file.close
#change file permission
File.chmod(0777, startcallfile)
FileUtils.chown(ENV['USER'],'asterisk',startcallfile)
#hour-minute-second-month-day-year (example: 02-10-00-09-27-2007)
if (touchtime != "")
	timesplit = touchtime.split('-')
	ctime = Time.local(timesplit[5],timesplit[3],timesplit[4],timesplit[0],timesplit[1],timesplit[2])
	File.utime(ctime,ctime,startcallfile) #change file time to future date
end 

#move file to /var/spool/asterisk/outgoing
FileUtils.mv(startcallfile,endcallfile)