~singpolyma/sgx-jmp

13a472e7a3c5a5d45fa09f085968803eb2ec0d3d — Stephen Paul Weber 2 years ago a8d0861
Testing has gone well, enable RevAI for everyone

Disabling the Bandwidth transcription in the process
M lib/backend_sgx.rb => lib/backend_sgx.rb +0 -1
@@ 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


M lib/bwmsgsv2_repo.rb => lib/bwmsgsv2_repo.rb +1 -4
@@ 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

M lib/customer.rb => lib/customer.rb +1 -1
@@ 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,

M lib/rev_ai.rb => lib/rev_ai.rb +1 -1
@@ 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

M lib/trivial_backend_sgx_repo.rb => lib/trivial_backend_sgx_repo.rb +0 -2
@@ 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

M test/test_web.rb => test/test_web.rb +33 -0
@@ 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


M views/voicemail.slim => views/voicemail.slim +0 -2
@@ 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"} /

M web.rb => web.rb +31 -52
@@ 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