From 69ec3bd1e64b706983cb15f49e30b893e291dc78 Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Mon, 6 May 2024 16:04:22 -0500 Subject: [PATCH] JSON rendering option for inbound calls --- lib/call_attempt.rb | 6 +++--- lib/customer_fwd.rb | 22 ++++++++++++++++++++++ lib/customer_ogm.rb | 22 ++++++++++++++++++++++ web.rb | 33 ++++++++++++++++++++++++++++----- 4 files changed, 75 insertions(+), 8 deletions(-) diff --git a/lib/call_attempt.rb b/lib/call_attempt.rb index 97cf7c36d74afd7ca4a3592808fb95a96252fb29..3676cdfd5d0c26599454d2a21e333e73e3e6174e 100644 --- a/lib/call_attempt.rb +++ b/lib/call_attempt.rb @@ -145,7 +145,7 @@ class CallAttempt def create_call(*); end def as_json(*) - { tts: tts } + tts.empty? ? {} : { tts: tts } end def to_json(*args) @@ -181,7 +181,7 @@ class CallAttempt def create_call(*); end def as_json(*) - { tts: tts } + tts.empty? ? {} : { tts: tts } end def to_json(*args) @@ -231,7 +231,7 @@ class CallAttempt def create_call(*); end def as_json(*) - { tts: tts } + tts.empty? ? {} : { tts: tts } end def to_json(*args) diff --git a/lib/customer_fwd.rb b/lib/customer_fwd.rb index 65e85a22560bacf2a6e1e969d8290876cf665b7d..d57b371ae205cbb57566dbfef6a4da7278d015a5 100644 --- a/lib/customer_fwd.rb +++ b/lib/customer_fwd.rb @@ -65,6 +65,14 @@ class CustomerFwd to_i.to_s end + def as_json(*) + to_i + end + + def to_json(*args) + as_json.to_json(*args) + end + class Infinite < Timeout def initialize; end @@ -77,6 +85,10 @@ class CustomerFwd end def to_i; end + + def as_json(*) + nil + end end end @@ -104,6 +116,14 @@ class CustomerFwd BANDWIDTH_VOICE.create_call(account, body: request).data.call_id end + def as_json(*) + [to_h] + end + + def to_json(*args) + as_json.to_json(*args) + end + class Tel < CustomerFwd def initialize(values) super @@ -132,6 +152,8 @@ class CustomerFwd def create_call(*); end def to; end + + def as_json(*); end end URIS = { diff --git a/lib/customer_ogm.rb b/lib/customer_ogm.rb index 991dfc46e89fd680cd5a66aeec6164ed64dc32cc..dbdd67e6c047900aa4c82c23f33f4132ad952a7b 100644 --- a/lib/customer_ogm.rb +++ b/lib/customer_ogm.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require_relative "tts_template" + module CustomerOGM def self.for(url, tel, fetch_vcard) return Media.new(url) if url @@ -15,6 +17,16 @@ module CustomerOGM def to_render [:voicemail_ogm_media, { locals: { url: @url } }] end + + def as_json(*) + { + media_url: @url + } + end + + def to_json(*args) + as_json.to_json(*args) + end end class TTS @@ -50,5 +62,15 @@ module CustomerOGM def to_render [:voicemail_ogm_tts, { locals: { fn: fn } }] end + + def as_json(*) + { + tts: TTSTemplate.new(to_render.first).tts(self) + } + end + + def to_json(*args) + as_json.to_json(*args) + end end end diff --git a/web.rb b/web.rb index 2dfdb68ade13e1b34f0da825662f69b638257419..a5252bf0e0eaf022a824798f4acb241b7bd8a6b8 100644 --- a/web.rb +++ b/web.rb @@ -206,19 +206,41 @@ class Web < Roda ]) end + def json_call(customer, fwd, call_attempt) + raise "Not allowed" unless params["secret"] == CONFIG[:component][:secret] + + customer.ogm(params["from"]).then do |ogm| + call_attempt.as_json.merge( + fwd: Array(fwd.as_json), + ogm: ogm + ).to_json + end + end + + def transfer_complete_url(customer_id, call_id, tries) + url( + inbound_calls_path(:transfer_complete, customer_id, call_id: call_id) + ) + (tries ? "&tries=#{tries}" : "") + end + def create_call(customer, from, call_id, application_id, tries: nil) call_inputs(customer, from, call_id).then do |(customer_id, fwd, ca)| + request.json { json_call(customer, fwd, ca) } ca.create_call(fwd, CONFIG[:creds][:account]) do |cc| cc.from = from cc.application_id = application_id cc.answer_url = url inbound_calls_path(nil, customer_id) - cc.disconnect_url = url( - inbound_calls_path(:transfer_complete, customer_id, call_id: call_id) - ) + (tries ? "&tries=#{tries}" : "") + cc.disconnect_url = transfer_complete_url(customer_id, call_id, tries) end end end + def hangup + request.json { {}.to_json } + + render :hangup + end + route do |r| r.get "healthcheck" do "OK" @@ -344,7 +366,7 @@ class Web < Roda ).then { |c| c.ogm(params["from"]) }.then { |ogm| - next render :hangup unless ogm + next hangup unless ogm render :voicemail, locals: { ogm: ogm } }.catch_only(CustomerRepo::NotFound) { @@ -372,7 +394,7 @@ class Web < Roda sgx_repo: Bwmsgsv2Repo.new ).find_by_tel(params["to"]).then { |customer| reachability_repo.find(customer, params["from"]).then do |reach| - reach.filter(if_yes: ->(_) { render :hangup }) do + reach.filter(if_yes: ->(_) { hangup }) do create_call( customer, params["from"], @@ -390,6 +412,7 @@ class Web < Roda render :forward, locals: { fwd: e.fwd, from: params["from"] } }.catch { |e| log_error(e) unless e == :voicemail + r.json { { error: e.to_s }.to_json } render :redirect, locals: { to: inbound_calls_path(:voicemail) } } end