Feat: detect language of the message content (#6660)

This commit is contained in:
Tejaswini Chile
2023-04-04 08:57:27 +05:30
committed by GitHub
parent 268eababa3
commit 6a0ca35de4
14 changed files with 178 additions and 7 deletions

View File

@@ -16,5 +16,10 @@ FactoryBot.define do
app_id { 'dyte' }
settings { { api_key: 'api_key', organization_id: 'org_id' } }
end
trait :google_translate do
app_id { 'google_translate' }
settings { { project_id: 'test', credentials: {} } }
end
end
end

View File

@@ -7,7 +7,7 @@ RSpec.describe HookJob, type: :job do
let(:hook) { create(:integrations_hook, account: account) }
let(:inbox) { create(:inbox, account: account) }
let(:event_name) { 'message.created' }
let(:event_data) { { message: create(:message, account: account) } }
let(:event_data) { { message: create(:message, account: account, content: 'muchas muchas gracias', message_type: :incoming) } }
it 'enqueues the job' do
expect { job }.to have_enqueued_job(described_class)
@@ -25,7 +25,7 @@ RSpec.describe HookJob, type: :job do
it 'calls Integrations::Slack::SendOnSlackService when its a slack hook' do
hook = create(:integrations_hook, app_id: 'slack', account: account)
allow(Integrations::Slack::SendOnSlackService).to receive(:new).and_return(process_service)
expect(Integrations::Slack::SendOnSlackService).to receive(:new)
expect(Integrations::Slack::SendOnSlackService).to receive(:new).with(message: event_data[:message], hook: hook)
described_class.perform_now(hook, event_name, event_data)
end
@@ -40,7 +40,14 @@ RSpec.describe HookJob, type: :job do
it 'calls Integrations::Dialogflow::ProcessorService when its a dialogflow intergation' do
hook = create(:integrations_hook, :dialogflow, inbox: inbox, account: account)
allow(Integrations::Dialogflow::ProcessorService).to receive(:new).and_return(process_service)
expect(Integrations::Dialogflow::ProcessorService).to receive(:new)
expect(Integrations::Dialogflow::ProcessorService).to receive(:new).with(event_name: event_name, hook: hook, event_data: event_data)
described_class.perform_now(hook, event_name, event_data)
end
it 'calls Conversations::DetectLanguageJob when its a google_translate intergation' do
hook = create(:integrations_hook, :google_translate, account: account)
allow(Integrations::GoogleTranslate::DetectLanguageService).to receive(:new).and_return(process_service)
expect(Integrations::GoogleTranslate::DetectLanguageService).to receive(:new).with(hook: hook, message: event_data[:message])
described_class.perform_now(hook, event_name, event_data)
end
end

View File

@@ -0,0 +1,44 @@
require 'rails_helper'
require 'google/cloud/translate/v3'
describe Integrations::GoogleTranslate::DetectLanguageService do
let(:account) { create(:account) }
let(:message) { create(:message, account: account, content: 'muchas muchas gracias') }
let(:hook) { create(:integrations_hook, :google_translate, account: account) }
let(:translate_client) { double }
before do
allow(::Google::Cloud::Translate).to receive(:translation_service).and_return(translate_client)
allow(translate_client).to receive(:detect_language).and_return(::Google::Cloud::Translate::V3::DetectLanguageResponse
.new({ languages: [{ language_code: 'es', confidence: 0.71875 }] }))
end
describe '#perform' do
it 'detects and updates the conversation language' do
described_class.new(hook: hook, message: message).perform
expect(translate_client).to have_received(:detect_language)
expect(message.conversation.reload.additional_attributes['conversation_language']).to eq('es')
end
it 'will not update the conversation language if it is already present' do
message.conversation.update!(additional_attributes: { conversation_language: 'en' })
described_class.new(hook: hook, message: message).perform
expect(translate_client).not_to have_received(:detect_language)
expect(message.conversation.reload.additional_attributes['conversation_language']).to eq('en')
end
it 'will not update the conversation language if the message is not incoming' do
message.update!(message_type: :outgoing)
described_class.new(hook: hook, message: message).perform
expect(translate_client).not_to have_received(:detect_language)
expect(message.conversation.reload.additional_attributes['conversation_language']).to be_nil
end
it 'will not execute if the message content is blank' do
message.update!(content: nil)
described_class.new(hook: hook, message: message).perform
expect(translate_client).not_to have_received(:detect_language)
expect(message.conversation.reload.additional_attributes['conversation_language']).to be_nil
end
end
end

View File

@@ -1,4 +1,5 @@
require 'rails_helper'
describe AutomationRuleListener do
let(:listener) { described_class.instance }
let!(:account) { create(:account) }

View File

@@ -160,6 +160,22 @@ RSpec.describe Conversation, type: :model do
.with(described_class::CONVERSATION_UPDATED, kind_of(Time), conversation: conversation, notifiable_assignee_change: true)
end
it 'will run conversation_updated event for conversation_language in additional_attributes' do
conversation.additional_attributes[:conversation_language] = 'es'
conversation.save!
changed_attributes = conversation.previous_changes
expect(Rails.configuration.dispatcher).to have_received(:dispatch)
.with(described_class::CONVERSATION_UPDATED, kind_of(Time), conversation: conversation, notifiable_assignee_change: false,
changed_attributes: changed_attributes, performed_by: nil)
end
it 'will not run conversation_updated event for bowser_language in additional_attributes' do
conversation.additional_attributes[:browser_language] = 'es'
conversation.save!
expect(Rails.configuration.dispatcher).not_to have_received(:dispatch)
.with(described_class::CONVERSATION_UPDATED, kind_of(Time), conversation: conversation, notifiable_assignee_change: true)
end
it 'creates conversation activities' do
conversation.update(
status: :resolved,