From a318c83f769e5497e07879ea6ffc07d658293c81 Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Wed, 8 Sep 2021 15:40:03 -0500 Subject: [PATCH] WIP --- Gemfile | 1 + views/bridge.slim | 3 +++ web.rb | 64 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+) create mode 100644 views/bridge.slim diff --git a/Gemfile b/Gemfile index aa2984b..e5e933f 100644 --- a/Gemfile +++ b/Gemfile @@ -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" diff --git a/views/bridge.slim b/views/bridge.slim new file mode 100644 index 0000000..a97957c --- /dev/null +++ b/views/bridge.slim @@ -0,0 +1,3 @@ +doctype xml +Response + Bridge= call_id diff --git a/web.rb b/web.rb index 1e97e46..e5e5c02 100644 --- a/web.rb +++ b/web.rb @@ -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 +"" + 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 +"" + 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 "" +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'] +"" +"" +"" +"" +rescue + log.error($!) +exit! +end + end + end + end + r.on "outbound" do r.on "calls" do r.post "status" do -- 2.34.2