From 01987a42611fc9e56bb208131d4ee670848be6a8 Mon Sep 17 00:00:00 2001 From: Samnang Chhun Date: Tue, 17 Oct 2023 15:48:41 +0700 Subject: [PATCH] TTS Provider --- components/app/app/models/call_properties.rb | 1 + components/app/app/models/execute_twiml.rb | 15 +++++++++++++++ components/app/app/models/outbound_call.rb | 1 + components/app/lib/call_platform/client.rb | 8 ++++++++ components/app/spec/models/outbound_call_spec.rb | 3 +++ .../app/spec/support/call_controller_helpers.rb | 1 + components/app/spec/web/api_spec.rb | 1 + 7 files changed, 30 insertions(+) diff --git a/components/app/app/models/call_properties.rb b/components/app/app/models/call_properties.rb index e205c6480..fb01656f8 100644 --- a/components/app/app/models/call_properties.rb +++ b/components/app/app/models/call_properties.rb @@ -10,6 +10,7 @@ :to, :from, :sip_headers, + :default_tts_provider, keyword_init: true ) do def inbound? diff --git a/components/app/app/models/execute_twiml.rb b/components/app/app/models/execute_twiml.rb index d3556a3e4..0567cda8f 100644 --- a/components/app/app/models/execute_twiml.rb +++ b/components/app/app/models/execute_twiml.rb @@ -10,6 +10,7 @@ class ExecuteTwiML NESTED_GATHER_VERBS = %w[Say Play].freeze MAX_LOOP = 100 SLEEP_BETWEEN_REDIRECTS = 1 + DEFAULT_VOICE_PROVIDER = "polly".freeze DEFAULT_TWILIO_VOICE = "man".freeze DEFAULT_TWILIO_LANGUAGE = "en".freeze FINISH_ON_KEY_PATTERN = /\A(?:\d|\*|\#)\z/.freeze @@ -105,10 +106,17 @@ def execute_say(verb) answer unless answered? attributes = twiml_attributes(verb) + provider = resolve_voice_provider(attributes["voice"]) twiml_loop(attributes).each do say(say_options(verb.content, attributes)) end + + # call_platform_client.notify_tts_event( + # phone_call: call_properties.call_sid, + # provider:, + # num_chars: verb.content.length + # ) end def execute_redirect(verb) @@ -302,4 +310,11 @@ def sip_headers def normalize_recording_url(raw_recording_url) URL_PATTERN.match(raw_recording_url)[0] end + + def resolve_voice_provider(voice) + return DEFAULT_VOICE_PROVIDER if voice.in?(["man", "woman"]) + return DEFAULT_VOICE_PROVIDER if voice.start_with?("Polly.") + + raise Errors::TwiMLError, "Unsupported voice" + end end diff --git a/components/app/app/models/outbound_call.rb b/components/app/app/models/outbound_call.rb index 7c5310096..506cb9668 100644 --- a/components/app/app/models/outbound_call.rb +++ b/components/app/app/models/outbound_call.rb @@ -29,6 +29,7 @@ def initiate api_version: call_params.fetch("api_version"), from: call_params.fetch("from"), to: call_params.fetch("to"), + default_tts_provider: call_params.fetch("default_tts_provider"), sip_headers: ) }, diff --git a/components/app/lib/call_platform/client.rb b/components/app/lib/call_platform/client.rb index ecf4ac1f8..2f1349a1b 100644 --- a/components/app/lib/call_platform/client.rb +++ b/components/app/lib/call_platform/client.rb @@ -32,6 +32,14 @@ def notify_call_event(params) end end + def notify_tts_event(params) + response = http_client.post("/services/tts_events", params.to_json) + + unless response.success? + Sentry.capture_message("Invalid phone call event", extra: { response_body: response.body }) + end + end + def build_routing_parameters(params) make_request("/services/routing_parameters", params: params) end diff --git a/components/app/spec/models/outbound_call_spec.rb b/components/app/spec/models/outbound_call_spec.rb index 537ec2897..eef55ef69 100644 --- a/components/app/spec/models/outbound_call_spec.rb +++ b/components/app/spec/models/outbound_call_spec.rb @@ -15,6 +15,7 @@ "account_auth_token" => "sample-auth-token", "direction" => "outbound-api", "api_version" => "2010-04-01", + "default_tts_provider" => "basic", "routing_parameters" => { "destination" => "85516701721", "dial_string_prefix" => nil, @@ -46,6 +47,7 @@ call_sid: "sample-call-sid", direction: "outbound-api", api_version: "2010-04-01", + default_tts_provider: "basic", to: "+85516701721", from: "2442", sip_headers: SIPHeaders.new( @@ -139,6 +141,7 @@ def build_call_params(params) "account_auth_token" => "sample-auth-token", "direction" => "outbound-api", "api_version" => "2010-04-01", + "default_tts_provider" => "basic", "routing_parameters" => { "destination" => "85516701721", "dial_string_prefix" => nil, diff --git a/components/app/spec/support/call_controller_helpers.rb b/components/app/spec/support/call_controller_helpers.rb index c7ba20634..4d48f0f26 100644 --- a/components/app/spec/support/call_controller_helpers.rb +++ b/components/app/spec/support/call_controller_helpers.rb @@ -8,6 +8,7 @@ def build_controller(call: build_fake_call, call_properties: {}, **options) account_sid: SecureRandom.uuid, api_version: "2010-04-01", auth_token: SecureRandom.alphanumeric, + default_tts_provider: "basic", from: "1000", to: "85512456869" ) diff --git a/components/app/spec/web/api_spec.rb b/components/app/spec/web/api_spec.rb index 19a641607..3536ea824 100644 --- a/components/app/spec/web/api_spec.rb +++ b/components/app/spec/web/api_spec.rb @@ -33,6 +33,7 @@ module Web "account_auth_token" => "sample-auth-token", "direction" => "outbound-api", "api_version" => "2010-04-01", + "default_tts_provider" => "basic", "routing_parameters" => { "destination" => "85516701721", "dial_string_prefix" => nil,