Skip to content

Commit

Permalink
TTS Provider
Browse files Browse the repository at this point in the history
  • Loading branch information
samnang committed Oct 17, 2023
1 parent a871953 commit 01987a4
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions components/app/app/models/call_properties.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
:to,
:from,
:sip_headers,
:default_tts_provider,
keyword_init: true
) do
def inbound?
Expand Down
15 changes: 15 additions & 0 deletions components/app/app/models/execute_twiml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
1 change: 1 addition & 0 deletions components/app/app/models/outbound_call.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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:
)
},
Expand Down
8 changes: 8 additions & 0 deletions components/app/lib/call_platform/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions components/app/spec/models/outbound_call_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions components/app/spec/support/call_controller_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down
1 change: 1 addition & 0 deletions components/app/spec/web/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 01987a4

Please sign in to comment.