diff --git a/lib/backend_sgx.rb b/lib/backend_sgx.rb index 7f58711d83c81e3b71695d6d72339e7a2f82481e..9ae4d40ed093a7e3ed27465f7cfb0eeb3a43b4a5 100644 --- a/lib/backend_sgx.rb +++ b/lib/backend_sgx.rb @@ -14,7 +14,6 @@ class BackendSgx ogm_url Either(String, nil, NotLoaded) fwd Either(CustomerFwd, nil, NotLoaded) transcription_enabled Either(Bool(), NotLoaded) - alternate_transcription_enabled Either(Bool(), NotLoaded) registered? Either(Blather::Stanza::Iq::IBR, FalseClass, NotLoaded) end diff --git a/lib/bwmsgsv2_repo.rb b/lib/bwmsgsv2_repo.rb index 713ba8609a027f058383262f48339c3310e20f59..d238abb28a54cc44aeb7761ced7d433cac9831d4 100644 --- a/lib/bwmsgsv2_repo.rb +++ b/lib/bwmsgsv2_repo.rb @@ -8,7 +8,6 @@ require_relative "trivial_backend_sgx_repo" class Bwmsgsv2Repo VOICEMAIL_TRANSCRIPTION_DISABLED = 0 - VOICEMAIL_ALT_TRANSCRIPTION = 1 def initialize( jid: CONFIG[:sgx], @@ -29,7 +28,6 @@ class Bwmsgsv2Repo ogm_url: ogm_url, fwd: CustomerFwd.for(uri: fwd, timeout: fwd_time), transcription_enabled: !flags[VOICEMAIL_TRANSCRIPTION_DISABLED], - alternate_transcription_enabled: flags[VOICEMAIL_ALT_TRANSCRIPTION], registered?: reg ) end @@ -89,8 +87,7 @@ protected def unpack_flags(from_jid) @redis.bitfield( "catapult_settings_flags-#{from_jid}", - "GET", "u1", VOICEMAIL_TRANSCRIPTION_DISABLED, - "GET", "u1", VOICEMAIL_ALT_TRANSCRIPTION + "GET", "u1", VOICEMAIL_TRANSCRIPTION_DISABLED ).then { |arr| arr.map { |x| x.to_i == 1 } } end end diff --git a/lib/customer.rb b/lib/customer.rb index 6b6550b111b7519ebacb7da9ea070b32605fca17..0a378805a670438bb16b534f26139eb77c74e421 100644 --- a/lib/customer.rb +++ b/lib/customer.rb @@ -26,7 +26,7 @@ class Customer :message_limit, :auto_top_up_amount, :monthly_overage_limit, :monthly_price, :save_plan! def_delegators :@sgx, :deregister!, :register!, :registered?, :set_ogm_url, - :fwd, :transcription_enabled, :alternate_transcription_enabled + :fwd, :transcription_enabled def_delegators :@usage, :usage_report, :message_usage, :incr_message_usage def_delegators :@financials, :payment_methods, :btc_addresses, :add_btc_address, :declines, :mark_decline, diff --git a/lib/rev_ai.rb b/lib/rev_ai.rb index 5964c032ef7a4832df79dd9864ceba7ebdb014fc..5994a9bf79aa2532b277ef24a2435910f32f17a4 100644 --- a/lib/rev_ai.rb +++ b/lib/rev_ai.rb @@ -29,7 +29,7 @@ class RevAi "https://api.rev.ai/speechtotext/v1/jobs/#{job['id']}/transcript", accept: "text/plain" ).then do |res| - text = res.response.split(" ", 3)[2].strip + text = res.response.split(" ", 3)[2]&.strip.to_s job.merge("text" => text, "metadata" => JSON.parse(job["metadata"])) end end diff --git a/lib/trivial_backend_sgx_repo.rb b/lib/trivial_backend_sgx_repo.rb index e540f9723517a3263328831091570b2c440ace1e..dae803d37c46a2b654235f713083207b28ac5f06 100644 --- a/lib/trivial_backend_sgx_repo.rb +++ b/lib/trivial_backend_sgx_repo.rb @@ -21,8 +21,6 @@ class TrivialBackendSgxRepo ogm_url: NotLoaded.new(:ogm_url), fwd: NotLoaded.new(:fwd_timeout), transcription_enabled: NotLoaded.new(:transcription_enabled), - alternate_transcription_enabled: - NotLoaded.new(:alternate_transcription_enabled), registered?: NotLoaded.new(:registered?) ) end diff --git a/test/test_web.rb b/test/test_web.rb index a675d96e6db26da632e5185f182834f50864835b..a850c77e97d183bd56e036e31c674cf374e2545e 100644 --- a/test/test_web.rb +++ b/test/test_web.rb @@ -436,6 +436,21 @@ class WebTest < Minitest::Test em :test_inbound_limit_hangup def test_voicemail + language_id = stub_request(:post, "https://api.rev.ai/languageid/v1/jobs") + .with(body: { + metadata: { + media_url: "https://jmp.chat/media", + from_jid: "+15557654321@component", + customer_id: "customerid" + }.to_json, + source_config: { + url: "https://jmp.chat/media" + }, + notification_config: { + url: "http://example.org/inbound/calls/CALLID/voicemail/language_id" + } + }.to_json) + Customer::BLATHER.expect( :<<, nil, @@ -460,10 +475,27 @@ class WebTest < Minitest::Test assert last_response.ok? assert_mock Customer::BLATHER + assert_requested language_id end em :test_voicemail def test_anonymous_voicemail + language_id = stub_request(:post, "https://api.rev.ai/languageid/v1/jobs") + .with(body: { + metadata: { + media_url: "https://jmp.chat/media", + from_jid: + "16;phone-context=anonymous.phone-context.soprani.ca@component", + customer_id: "customerid" + }.to_json, + source_config: { + url: "https://jmp.chat/media" + }, + notification_config: { + url: "http://example.org/inbound/calls/CALLID/voicemail/language_id" + } + }.to_json) + Customer::BLATHER.expect( :<<, nil, @@ -491,6 +523,7 @@ class WebTest < Minitest::Test assert last_response.ok? assert_mock Customer::BLATHER + assert_requested language_id end em :test_anonymous_voicemail diff --git a/views/voicemail.slim b/views/voicemail.slim index fc9fe7086cb7c5b3f560b2bd6c4d80c11237266b..2b10583edf693072b8dc7aed67c8f578708e97c0 100644 --- a/views/voicemail.slim +++ b/views/voicemail.slim @@ -5,8 +5,6 @@ Response == render(*ogm.to_render) PlayAudio= "/beep.mp3" Record{ - transcribe=transcription_enabled.to_s maxDuration="600" recordingAvailableUrl="/inbound/calls/#{params['callId']}/voicemail/audio" - transcriptionAvailableUrl="/inbound/calls/#{params['callId']}/voicemail/transcription" fileFormat="mp3"} / diff --git a/web.rb b/web.rb index 3281e477780006da6dbf55c4e556f982d38fc4e2..885761a18d2395694e37072eb58da9cb52f2ee46 100644 --- a/web.rb +++ b/web.rb @@ -166,14 +166,12 @@ class Web < Roda raise $! unless $!.response_code.to_s == "404" end - def do_alternate_transcription(customer, call_id) - return unless customer.alternate_transcription_enabled + def start_transcription(customer, call_id, media_url) + return unless customer.transcription_enabled rev_ai.language_id( - jmp_media_url, - url(inbound_calls_path( - "voicemail/rev_ai/language_id", call_id: call_id - )), + media_url, + url(inbound_calls_path("voicemail/language_id", call_id: call_id)), from_jid: from_jid, customer_id: customer.customer_id ) @@ -228,7 +226,7 @@ class Web < Roda customer_repo( sgx_repo: Bwmsgsv2Repo.new ).find_by_tel(params["to"]).then do |customer| - do_alternate_transcription(customer, call_id) + start_transcription(customer, call_id, jmp_media_url) m = Blather::Stanza::Message.new m.chat_state = nil @@ -242,54 +240,35 @@ class Web < Roda end end - r.post "transcription" do - duration = Time.parse(params["endTime"]) - - Time.parse(params["startTime"]) - next "OK<5" unless duration > 5 - - customer_repo.find_by_tel(params["to"]).then do |customer| - m = Blather::Stanza::Message.new - m.chat_state = nil - m.from = from_jid - m.subject = "Voicemail Transcription" - m.body = BANDWIDTH_VOICE.get_recording_transcription( - params["accountId"], params["callId"], params["recordingId"] - ).data.transcripts[0].text - customer.stanza_to(m) - - "OK" + r.post "language_id" do + rev_ai.language_id_result(params).then do |result| + rev_ai.stt( + result["top_language"], + result.dig("metadata", "media_url"), + url(inbound_calls_path( + "voicemail/transcription", + call_id: call_id + )), + **result["metadata"].transform_keys(&:to_sym) + ).then { "OK" } end end - r.on "rev_ai" do - r.post "language_id" do - rev_ai.language_id_result(params).then do |result| - rev_ai.stt( - result["top_language"], - result.dig("metadata", "media_url"), - url(inbound_calls_path( - "voicemail/rev_ai/transcription", - call_id: call_id - )), - **result["metadata"].transform_keys(&:to_sym) - ).then { "OK" } - end - end - - r.post "transcription" do - rev_ai.stt_result(params).then do |result| - customer_repo.find( - result.dig("metadata", "customer_id") - ).then do |customer| - m = Blather::Stanza::Message.new - m.chat_state = nil - m.from = result.dig("metadata", "from_jid") - m.subject = "Voicemail Transcription" - m.body = "Alternate Transcription: #{result['text']}" - customer.stanza_to(m) - - "OK" - end + r.post "transcription" do + rev_ai.stt_result(params).then do |result| + next "OK" if result["text"].empty? + + customer_repo.find( + result.dig("metadata", "customer_id") + ).then do |customer| + m = Blather::Stanza::Message.new + m.chat_state = nil + m.from = result.dig("metadata", "from_jid") + m.subject = "Voicemail Transcription" + m.body = result["text"] + customer.stanza_to(m) + + "OK" end end end