Skip to content

Commit

Permalink
Log translated_reply if made available by the driver (#342)
Browse files Browse the repository at this point in the history
* Log translated_reply if made available by the driver

* Prepare v2.0.0.beta7

* Update gemfile
  • Loading branch information
mgomes authored Nov 6, 2022
1 parent d22e0ce commit 9e6097c
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 28 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
37 changes: 17 additions & 20 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -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)

Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -78,7 +76,6 @@ DEPENDENCIES
oj (~> 3.10)
rack-test (~> 2.0)
rspec (~> 3.9)
rspec_junit_formatter (~> 0.3)
stealth!

BUNDLED WITH
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.0.0.beta6
2.0.0.beta7
16 changes: 11 additions & 5 deletions lib/stealth/controller/replies.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down Expand Up @@ -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'
Expand All @@ -282,6 +286,8 @@ def log_reply(reply)
topic: current_service,
message: "User #{current_session_id} -> Sending: #{message}"
)

message
end

end # instance methods
Expand Down
48 changes: 48 additions & 0 deletions spec/controller/replies_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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...' },
Expand Down Expand Up @@ -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") }
Expand Down
2 changes: 2 additions & 0 deletions spec/replies/messages/say_simple_hello.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- reply_type: text
text: "Hello"
3 changes: 1 addition & 2 deletions stealth.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down

0 comments on commit 9e6097c

Please sign in to comment.