diff --git a/lib/integrations/dialogflow/processor_service.rb b/lib/integrations/dialogflow/processor_service.rb index 384c41c11..11fdc8f88 100644 --- a/lib/integrations/dialogflow/processor_service.rb +++ b/lib/integrations/dialogflow/processor_service.rb @@ -13,6 +13,10 @@ class Integrations::Dialogflow::ProcessorService < Integrations::BotProcessorSer end def get_response(session_id, message) + if hook.settings['credentials'].blank? + Rails.logger.warn "Account: #{hook.try(:account_id)} Hook: #{hook.id} credentials are not present." && return + end + Google::Cloud::Dialogflow.configure { |config| config.credentials = hook.settings['credentials'] } session_client = Google::Cloud::Dialogflow.sessions session = session_client.session_path project: hook.settings['project_id'], session: session_id diff --git a/spec/lib/integrations/dialogflow/processor_service_spec.rb b/spec/lib/integrations/dialogflow/processor_service_spec.rb index 835dbc71b..e212caf57 100644 --- a/spec/lib/integrations/dialogflow/processor_service_spec.rb +++ b/spec/lib/integrations/dialogflow/processor_service_spec.rb @@ -44,6 +44,18 @@ describe Integrations::Dialogflow::ProcessorService do end end + context 'when dilogflow settings are not present' do + it 'will get empty response' do + last_count = conversation.reload.messages.count + allow(processor).to receive(:get_response).and_return({}) + hook.settings = { 'project_id' => 'something_invalid', 'credentials' => {} } + hook.save! + processor.perform + + expect(conversation.reload.messages.count).to eql(last_count) + end + end + context 'when dialogflow returns fullfillment text to be empty' do let(:dialogflow_response) do ActiveSupport::HashWithIndifferentAccess.new( @@ -126,4 +138,25 @@ describe Integrations::Dialogflow::ProcessorService do end end end + + describe '#get_response' do + let(:google_dialogflow) { Google::Cloud::Dialogflow } + let(:session_client) { double } + let(:session) { double } + let(:query_input) { { text: { text: message, language_code: 'en-US' } } } + let(:processor) { described_class.new(event_name: event_name, hook: hook, event_data: event_data) } + + before do + hook.update(settings: { 'project_id' => 'test', 'credentials' => 'creds' }) + allow(google_dialogflow).to receive(:sessions).and_return(session_client) + allow(session_client).to receive(:session_path).and_return(session) + allow(session_client).to receive(:detect_intent).and_return({ session: session, query_input: query_input }) + end + + it 'returns indented response' do + response = processor.send(:get_response, conversation.contact_inbox.source_id, message.content) + expect(response[:query_input][:text][:text]).to eq(message) + expect(response[:query_input][:text][:language_code]).to eq('en-US') + end + end end