feat: Support messages along with the actions in Dialogflow response (#2579)

This commit is contained in:
Pranav Raj S
2021-07-07 10:04:48 +05:30
committed by GitHub
parent bbc47971d7
commit 0d02142925
2 changed files with 39 additions and 11 deletions

View File

@@ -39,14 +39,22 @@ class Integrations::Dialogflow::ProcessorService
end end
def process_response(message, response) def process_response(message, response)
text_response = response.query_result['fulfillment_text'] fulfillment_messages = response.query_result['fulfillment_messages']
fulfillment_messages.each do |fulfillment_message|
content_params = generate_content_params(fulfillment_message)
if content_params['action'].present?
process_action(message, content_params['action'])
else
create_conversation(message, content_params)
end
end
end
content_params = { content: text_response } if text_response.present? def generate_content_params(fulfillment_message)
content_params ||= response.query_result['fulfillment_messages'].first['payload'].to_h text_response = fulfillment_message['text'].to_h
content_params = { content: text_response[:text].first } if text_response[:text].present?
process_action(message, content_params['action']) and return if content_params['action'].present? content_params ||= fulfillment_message['payload'].to_h
content_params
create_conversation(message, content_params)
end end
def create_conversation(message, content_params) def create_conversation(message, content_params)

View File

@@ -7,12 +7,15 @@ describe Integrations::Dialogflow::ProcessorService do
let(:message) { create(:message, account: account, conversation: conversation) } let(:message) { create(:message, account: account, conversation: conversation) }
let(:event_name) { 'message.created' } let(:event_name) { 'message.created' }
let(:event_data) { { message: message } } let(:event_data) { { message: message } }
let(:dialogflow_text_double) { double }
describe '#perform' do describe '#perform' do
let(:dialogflow_service) { double } let(:dialogflow_service) { double }
let(:dialogflow_response) do let(:dialogflow_response) do
ActiveSupport::HashWithIndifferentAccess.new( ActiveSupport::HashWithIndifferentAccess.new(
fulfillment_text: 'hello' fulfillment_messages: [
{ text: dialogflow_text_double }
]
) )
end end
@@ -21,23 +24,26 @@ describe Integrations::Dialogflow::ProcessorService do
before do before do
allow(dialogflow_service).to receive(:query_result).and_return(dialogflow_response) allow(dialogflow_service).to receive(:query_result).and_return(dialogflow_response)
allow(processor).to receive(:get_dialogflow_response).and_return(dialogflow_service) allow(processor).to receive(:get_dialogflow_response).and_return(dialogflow_service)
allow(dialogflow_text_double).to receive(:to_h).and_return({ text: ['hello payload'] })
end end
context 'when valid message and dialogflow returns fullfillment text' do context 'when valid message and dialogflow returns fullfillment text' do
it 'creates the response message' do it 'creates the response message' do
expect(processor.perform.content).to eql('hello') processor.perform
expect(conversation.reload.messages.last.content).to eql('hello payload')
end end
end end
context 'when dialogflow returns fullfillment text to be empty' do context 'when dialogflow returns fullfillment text to be empty' do
let(:dialogflow_response) do let(:dialogflow_response) do
ActiveSupport::HashWithIndifferentAccess.new( ActiveSupport::HashWithIndifferentAccess.new(
fulfillment_messages: [{ payload: { content: 'hello payload' } }] fulfillment_messages: [{ payload: { content: 'hello payload random' } }]
) )
end end
it 'creates the response message based on fulfillment messages' do it 'creates the response message based on fulfillment messages' do
expect(processor.perform.content).to eql('hello payload') processor.perform
expect(conversation.reload.messages.last.content).to eql('hello payload random')
end end
end end
@@ -54,6 +60,20 @@ describe Integrations::Dialogflow::ProcessorService do
end end
end end
context 'when dialogflow returns action and messages if available' do
let(:dialogflow_response) do
ActiveSupport::HashWithIndifferentAccess.new(
fulfillment_messages: [{ payload: { action: 'handoff' } }, { text: dialogflow_text_double }]
)
end
it 'handsoff the conversation to agent' do
processor.perform
expect(conversation.reload.status).to eql('open')
expect(conversation.messages.last.content).to eql('hello payload')
end
end
context 'when conversation is not bot' do context 'when conversation is not bot' do
let(:conversation) { create(:conversation, account: account, status: :open) } let(:conversation) { create(:conversation, account: account, status: :open) }