~singpolyma/sgx-jmp

a318c83f769e5497e07879ea6ffc07d658293c81 — Stephen Paul Weber 9 months ago 3260c8a sip-outbound
WIP
3 files changed, 68 insertions(+), 0 deletions(-)

M Gemfile
A views/bridge.slim
M web.rb
M Gemfile => Gemfile +1 -0
@@ 3,6 3,7 @@
source "https://rubygems.org"

gem "amazing_print"
gem "bandwidth-sdk"
gem "blather", git: "https://github.com/singpolyma/blather.git", branch: "ergonomics"
gem "braintree"
gem "dhall"

A views/bridge.slim => views/bridge.slim +3 -0
@@ 0,0 1,3 @@
doctype xml
Response
	Bridge= call_id

M web.rb => web.rb +64 -0
@@ 3,11 3,19 @@
require "roda"
require "thin"
require "sentry-ruby"
require "bandwidth"

Faraday.default_adapter = :em_synchrony

require_relative "lib/cdr"
require_relative "lib/roda_em_promise"
require_relative "lib/rack_fiber"

BANDWIDTH_VOICE = Bandwidth::Client.new(
	voice_basic_auth_user_name: CONFIG[:creds][:username],
	voice_basic_auth_password: CONFIG[:creds][:password],
).voice_client.client

class Web < Roda
	use Rack::Fiber # Must go first!
	use Sentry::Rack::CaptureExceptions


@@ 44,7 52,63 @@ class Web < Roda
		)
	end

BOOP = {}
BOOP2 = {}

	route do |r|
		r.on "inbound" do

				r.post "fallback" do
"<Response><Redirect redirectUrl=\"https://62f3-2607-fea8-1c80-2d-f22f-74ff-fed2-3e37.ngrok.io/inbound/calls/#{params['callId']}/poll\" /></Response>"
				end
			r.on "calls" do
				r.post "status" do
					log.info(params)
"OK"
				end


				r.on :call_id do |call_id|
					r.post "poll" do
						EM.promise_timer(4).then do
"<Response><Redirect redirectUrl=\"https://62f3-2607-fea8-1c80-2d-f22f-74ff-fed2-3e37.ngrok.io/inbound/calls/#{params['callId']}/poll\" /></Response>"
						end
					end

					r.post do
true_call_id = BOOP2.fetch(call_id, call_id)
						render :bridge, locals: { call_id: true_call_id }
					end
				end

				r.post do
begin
if BOOP["#{params['from']},#{params['to']}"]
	BOOP2[BOOP["#{params['from']},#{params['to']}"]] = params["callId"]
	return "<Response><Pause duration=\"300\" /></Response>"
end
					body = Bandwidth::ApiCreateCallRequest.new.tap do |cc|
						#cc.to = "sip:singpolyma%5C40singpolyma.net%40cheogram.com@sip.cheogram.com"
						cc.to = "+12263393063"
						cc.from = params["from"]
						cc.application_id = params["applicationId"]
						cc.call_timeout = 300
						cc.answer_url = "https://62f3-2607-fea8-1c80-2d-f22f-74ff-fed2-3e37.ngrok.io/inbound/calls/#{params['callId']}"
					end
					outbound_leg = BANDWIDTH_VOICE.create_call(CONFIG[:creds][:account], body: body).data.call_id
BOOP["#{params['from']},#{params['to']}"] = params['callId']
"<Response><Ring duration=\"300\" /></Response>"
"<Response><Redirect redirectUrl=\"https://62f3-2607-fea8-1c80-2d-f22f-74ff-fed2-3e37.ngrok.io/inbound/calls/#{params['callId']}/poll\" /></Response>"
"<Response><Hangup/></Response>"
"<Response><Pause duration=\"300\" /></Response>"
rescue
	log.error($!)
exit!
end
				end
			end
		end

		r.on "outbound" do
			r.on "calls" do
				r.post "status" do