Web Full Example (ruby)

The example requires the sinatra web framework. I generated the project on Redial’s server by running the following command from my home directory:

ruby new_sinatra_app.rb main_agi

This file uses “erb” html templates. Erb allows you to mix Ruby code with HTML.
Introduction to ERB

The script also uses the following context to create a “click-to-call” option.

;---- Bridge Call
[ck987_bridge]
exten => _1NXXNXXXXXX,1,Set(CALLERID(num)=16466429290)
exten => _1NXXNXXXXXX,n,Dial(SIP/flowroute/${EXTEN},30,r);

app.rb

require 'sinatra'
require 'rubygems'
require 'data_mapper'
require 'fileutils'

#------- set up database
DataMapper.setup(:default, {
 :adapter => 'mysql',
 :host => 'localhost',
 :username => 'ck987' ,
 :password => 'xxxxxxxxx',
 :database => 'ck987'})
#caller model for database interaction
class Caller
  include DataMapper::Resource

  property :id, Serial
  property :caller_id, String, :required => true
  property :name_audio, String, :required => true, :length => 64
  property :last_call_time, DateTime
  property :created_at, DateTime
end
# Finish setup
DataMapper.finalize

# ------- main route
get '/' do
  @calls = Caller.all
  @params_call=params[:call]
  erb :index #render the index erb template (see end of this file)
end

# ------- Playback audio
get '/names' do
  @audio_file="http://asterisk.itp-redial.com/~ck987/redial/names/#{params[:file]}.WAV"
  erb :names #render the names erb template (see end of this file)
end

#generates a call file that will first call my phone, then call the other person's phone
def gencallfile(numbertocall)
  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/16466429290\n")
  file.puts("MaxRetries: 1\n")
  file.puts("RetryTime: 60\n")
  file.puts("WaitTime: 30\n")
  file.puts("Context: ck987_bridge\n")
  file.puts("Extension: #{numbertocall}\n")
  file.close
  #change file permission
  File.chmod(0777, startcallfile)
  FileUtils.chown(ENV['USER'],'asterisk',startcallfile)
  #move file to /var/spool/outgoing
  FileUtils.mv(startcallfile,endcallfile)
end

#Now define html templates. Normally these would be separate files
__END__
@@ layout
<html>
  <body>
    <%= yield %>
  </body>
</html>

@@index
<% if (@params_call) %>
  <% gencallfile(@params_call) #make the call %>
  Calling <%= @params_call %>
<% end %>

<br><br>
<b>Callers To Chris's System</b><br><br>
<table>
<% @calls.each do |call| %>
<% spl=call.name_audio.split '/' %>
<% filename=spl[spl.length-1] %>
  <tr>
    <td> <%= call.last_call_time.to_s %> </td>
    <td> <a href="names?file=<%= filename %>" target="_blank"> <%= call.caller_id %> </a> </td>
    <td> <a href="?call=<%= call.caller_id %>">Call Them</a> </td>
  </tr>
<% end %>
</table>

@@names
<embed src="<%= @audio_file %>" width="200" height="40" autoplay="true" />