Guest User

Untitled

a guest
Aug 9th, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. require 'rubygems'
  2. require 'sinatra' # Get with "gem install sinatra"
  3. require 'restful_adhearsion'
  4. require 'pp'
  5.  
  6. #Create our Adhearsion object connected to the RESTful API of Adhearsion
  7. Adhearsion = RESTfulAdhearsion.new(:host => "localhost",
  8. :port => 5000,
  9. :user => "jicksta",
  10. :password => "roflcopterz")
  11.  
  12. # You'll need to change this for your own format.
  13. # Note: this will soon be handled by the Call Routing DSL in Adhearsion.
  14. def format_number(number)
  15. if number.length == 10
  16. return "SIP/tsgglobal/1#{number}"
  17. elsif number.length == 11
  18. return "SIP/tsgglobal/#{number}"
  19. else
  20. return "SIP/elastix-trunk/#{number}"
  21. end
  22. end
  23.  
  24. def call_with_source(source)
  25. #ahn_log.click_to_call "Finding call with source #{source.inspect} in #{Adhearsion.active_calls.size} active calls"
  26. retVal = Hash.new
  27. Adhearsion.active_calls.to_a.find do |call|
  28. if call.variables[:sourcedid].include? source
  29. if (defined? call.variables[:msg_filename] && call.variables[:msg_filename] != nil)
  30. retVal[:msg_filename] = call.variables[:msg_filename]
  31. retVal[:result] = "alive"
  32. end
  33. else
  34. retVal[:msg_filename] = nil
  35. retVal[:result] = "dead"
  36. end
  37.  
  38. return retVal
  39.  
  40. end
  41. end
  42.  
  43. post "/call" do
  44. options = { :channel => format_number(params[:source]),
  45. :context => "adhearsion",
  46. :priority => "1",
  47. :callerid => "8664680900",
  48. :exten => "1000",
  49. :async => 'true',
  50. :variable => "destination=#{format_number(params[:destination])},sourcedid=#{params[:source]}",
  51. }
  52.  
  53. #Originates a call over the AMI interface
  54. Adhearsion.originate(options)
  55. #puts options.to_json
  56. "ok".to_json
  57. end
  58.  
  59. post "/hangup" do
  60. #Get the channel of the active call, then hang it up
  61. channel_of_active_call = Adhearsion.hangup_channel_with_destination params[:call_to_hangup]
  62. "ok".to_json
  63. end
  64.  
  65. get '/status' do
  66. # The line below will return either {result:"alive"} or {result:"dead"} to the browser
  67. Adhearsion.call_with_source(params[:source]).to_json
  68. #{:result => Adhearsion.call_with_source(params[:source])}.to_json
  69. end
  70.  
  71. get '/' do
  72. #Build our web form that has the JQuery sprinkled in
  73. <<-HTML
  74. <html><head>
  75.  
  76. <title>Record a greeting</title>
  77.  
  78. <script src="jquery.js" type="text/javascript"></script>
  79. <script src="call.js" type="text/javascript"></script>
  80. <link href="style.css" media="screen" rel="stylesheet" type="text/css" />
  81.  
  82. </head><body>
  83.  
  84. <div id="content">
  85. <h1>Record a greeting</h1>
  86.  
  87.  
  88. <div id="call-form">
  89. <p><label for="source">Your number: </label><input type="text" id="source" name="source"/></p>
  90. <p><button onclick="new AhnCall($('#call'), $('#source').val(), $('#destination').val())">Start call</button></p>
  91. <input type="hidden" name="destination" value="1" />
  92. </div>
  93. </div>
  94.  
  95. <div id="call" class="hidden">
  96. <p>Starting...</p>
  97. </div>
  98.  
  99. </body></html>
  100. HTML
  101. end
Add Comment
Please, Sign In to add comment