diff --git a/CHANGELOG.md b/CHANGELOG.md index 19cdd2f2..359cd177 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -56,6 +56,7 @@ * [Replies] Voice services (determined by having "voice" in the name) now automatically skip auto-delays. * [Controllers] `current_message` now has a `confidence` attribute containing a float with the confidence value of the transcription (from 0 to 1). * [Controllers] Added a `halt!` method that can be used with the controller error handlers to stop code execution. +* [Logger] If the driver makes the `translated_reply` instance variable available, it will now be logged. ## Bug Fixes diff --git a/Gemfile.lock b/Gemfile.lock index 7147d288..b653548c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,11 +1,11 @@ PATH remote: . specs: - stealth (2.0.0.beta6) + stealth (2.0.0.beta7) activesupport (~> 6.0) multi_json (~> 1.12) puma (>= 4.2, < 6.0) - sidekiq (> 6.5) + sidekiq (< 7) sinatra (~> 2.0) thor (~> 1.0) @@ -30,7 +30,7 @@ GEM mustermann (2.0.2) ruby2_keywords (~> 0.0.1) nio4r (2.5.8) - oj (3.10.6) + oj (3.13.22) puma (5.6.5) nio4r (~> 2.0) rack (2.2.4) @@ -39,24 +39,22 @@ GEM rack-test (2.0.2) rack (>= 1.3) redis (4.8.0) - rspec (3.11.0) - rspec-core (~> 3.11.0) - rspec-expectations (~> 3.11.0) - rspec-mocks (~> 3.11.0) - rspec-core (3.11.0) - rspec-support (~> 3.11.0) - rspec-expectations (3.11.0) + rspec (3.12.0) + rspec-core (~> 3.12.0) + rspec-expectations (~> 3.12.0) + rspec-mocks (~> 3.12.0) + rspec-core (3.12.0) + rspec-support (~> 3.12.0) + rspec-expectations (3.12.0) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.11.0) - rspec-mocks (3.11.1) + rspec-support (~> 3.12.0) + rspec-mocks (3.12.0) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.11.0) - rspec-support (3.11.0) - rspec_junit_formatter (0.4.1) - rspec-core (>= 2, < 4, != 2.12.0) + rspec-support (~> 3.12.0) + rspec-support (3.12.0) ruby2_keywords (0.0.5) - sidekiq (6.5.7) - connection_pool (>= 2.2.5) + sidekiq (6.5.8) + connection_pool (>= 2.2.5, < 3) rack (~> 2.0) redis (>= 4.5.0, < 5) sinatra (2.2.2) @@ -68,7 +66,7 @@ GEM tilt (2.0.11) tzinfo (2.0.5) concurrent-ruby (~> 1.0) - zeitwerk (2.6.0) + zeitwerk (2.6.4) PLATFORMS ruby @@ -78,7 +76,6 @@ DEPENDENCIES oj (~> 3.10) rack-test (~> 2.0) rspec (~> 3.9) - rspec_junit_formatter (~> 0.3) stealth! BUNDLED WITH diff --git a/VERSION b/VERSION index 721ae0aa..d3dd3989 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -2.0.0.beta6 +2.0.0.beta7 diff --git a/lib/stealth/controller/replies.rb b/lib/stealth/controller/replies.rb index 8312d4c1..93910f83 100644 --- a/lib/stealth/controller/replies.rb +++ b/lib/stealth/controller/replies.rb @@ -96,11 +96,11 @@ def send_reply(reply:) reply: reply ) - translated_reply = handler.send(reply.reply_type) - client = service_client.new(reply: translated_reply) + formatted_reply = handler.send(reply.reply_type) + client = service_client.new(reply: formatted_reply) client.transmit - log_reply(reply) if Stealth.config.transcript_logging + log_reply(reply, handler) if Stealth.config.transcript_logging # If this was a 'delay' type of reply, we insert the delay if reply.delay? @@ -264,10 +264,14 @@ def calculate_reply_range end end - def log_reply(reply) + def log_reply(reply, reply_handler) message = case reply.reply_type when 'text' - reply['text'] + if reply_handler.respond_to?(:translated_reply) + reply_handler.translated_reply + else + reply['text'] + end when 'speech' reply['speech'] when 'ssml' @@ -282,6 +286,8 @@ def log_reply(reply) topic: current_service, message: "User #{current_session_id} -> Sending: #{message}" ) + + message end end # instance methods diff --git a/spec/controller/replies_spec.rb b/spec/controller/replies_spec.rb index cdfed12c..2c478b77 100644 --- a/spec/controller/replies_spec.rb +++ b/spec/controller/replies_spec.rb @@ -66,6 +66,10 @@ def say_nested_custom_reply send_replies custom_reply: 'messages/sub1/sub2/say_nested' end + def say_simple_hello + send_replies + end + def say_inline_reply reply = [ { 'reply_type' => 'text', 'text' => 'Hi, Morty. Welcome to Stealth bot...' }, @@ -609,6 +613,50 @@ def say_inline_reply end end + describe "Logging replies" do + let(:stubbed_handler) { double("handler") } + let(:stubbed_client) { double("client") } + + before(:each) do + allow(Stealth::Services::Facebook::ReplyHandler).to receive(:new).and_return(stubbed_handler) + allow(Stealth::Services::Facebook::Client).to receive(:new).and_return(stubbed_client) + allow(controller.current_session).to receive(:flow_string).and_return("message") + allow(controller.current_session).to receive(:state_string).and_return("say_simple_hello") + Stealth.config.auto_insert_delays = false + Stealth.config.transcript_logging = true + end + + after(:each) do + Stealth.config.auto_insert_delays = true + Stealth.config.transcript_logging = false + end + + it "should log replies if transcript_logging is enabled" do + allow(stubbed_client).to receive(:transmit).and_return(true) + allow(controller).to receive(:sleep).and_return(true).with(2.0) + + allow(stubbed_handler).to receive(:text).exactly(1).times + expect(Stealth::Logger).to receive(:l).with( + topic: 'facebook', + message: "User #{controller.current_session_id} -> Sending: Hello" + ) + controller.say_simple_hello + end + + it "should log translated replies if transcript_logging is enabled and the driver supports it" do + allow(stubbed_client).to receive(:transmit).and_return(true) + allow(controller).to receive(:sleep).and_return(true).with(2.0) + + allow(stubbed_handler).to receive(:text).exactly(1).times + allow(stubbed_handler).to receive(:translated_reply).and_return("Bonjour") + expect(Stealth::Logger).to receive(:l).with( + topic: 'facebook', + message: "User #{controller.current_session_id} -> Sending: Bonjour" + ) + controller.say_simple_hello + end + end + describe "client errors" do let(:stubbed_handler) { double("handler") } let(:stubbed_client) { double("client") } diff --git a/spec/replies/messages/say_simple_hello.yml b/spec/replies/messages/say_simple_hello.yml new file mode 100644 index 00000000..33ef46d0 --- /dev/null +++ b/spec/replies/messages/say_simple_hello.yml @@ -0,0 +1,2 @@ +- reply_type: text + text: "Hello" diff --git a/stealth.gemspec b/stealth.gemspec index ab98007e..76d8d881 100644 --- a/stealth.gemspec +++ b/stealth.gemspec @@ -16,11 +16,10 @@ Gem::Specification.new do |s| s.add_dependency 'puma', '>= 4.2', '< 6.0' s.add_dependency 'thor', '~> 1.0' s.add_dependency 'multi_json', '~> 1.12' - s.add_dependency 'sidekiq', '> 6.5' + s.add_dependency 'sidekiq', '< 7' s.add_dependency 'activesupport', '~> 6.0' s.add_development_dependency 'rspec', '~> 3.9' - s.add_development_dependency 'rspec_junit_formatter', '~> 0.3' s.add_development_dependency 'rack-test', '~> 2.0' s.add_development_dependency 'mock_redis', '~> 0.22'