feat(dialogflow): make language_code configurable instead of hardcoded (#13221)
# Pull Request Template ## Description Please include a summary of the change and issue(s) fixed. Also, mention relevant motivation, context, and any dependencies that this change requires. - Add language_code setting to Dialogflow integration configuration - Support 'auto' mode to detect language from contact's additional_attributes - Fallback to 'en-US' when no language is configured or detected - Include comprehensive language options (22 languages) - Add tests for language code configuration scenarios Fixes #3071 ## Type of change Please delete options that are not relevant. - [ ] Bug fix (non-breaking change which fixes an issue) - [x] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality not to work as expected) - [ ] This change requires a documentation update ## How Has This Been Tested? Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration. <img width="815" height="506" alt="Screenshot 2026-01-10 220410" src="https://github.com/user-attachments/assets/26d2619c-ed42-4c9a-a41d-9fb07ef91a30" /> ## Checklist: - [x] My code follows the style guidelines of this project - [x] I have performed a self-review of my code - [ ] I have commented on my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [x] My changes generate no new warnings - [ ] I have added tests that prove my fix is effective or that my feature works - [x] New and existing unit tests pass locally with my changes - [ ] Any dependent changes have been merged and published in downstream modules --------- Co-authored-by: Sojan Jose <sojan@pepalo.com>
This commit is contained in:
@@ -1,6 +1,51 @@
|
||||
require 'google/cloud/dialogflow/v2'
|
||||
|
||||
class Integrations::Dialogflow::ProcessorService < Integrations::BotProcessorService
|
||||
SUPPORTED_LANGUAGE_CODES = %w[
|
||||
ar
|
||||
en-US
|
||||
en-GB
|
||||
es-ES
|
||||
es-419
|
||||
fr-FR
|
||||
de-DE
|
||||
pt-BR
|
||||
pt-PT
|
||||
it-IT
|
||||
ja-JP
|
||||
ko-KR
|
||||
zh-CN
|
||||
zh-TW
|
||||
hi-IN
|
||||
ru-RU
|
||||
nl-NL
|
||||
pl-PL
|
||||
tr-TR
|
||||
th-TH
|
||||
vi-VN
|
||||
id-ID
|
||||
].freeze
|
||||
AUTO_LANGUAGE_CODE_MAP = {
|
||||
'ar' => 'ar',
|
||||
'de' => 'de-DE',
|
||||
'en' => 'en-US',
|
||||
'es' => 'es-ES',
|
||||
'fr' => 'fr-FR',
|
||||
'hi' => 'hi-IN',
|
||||
'id' => 'id-ID',
|
||||
'it' => 'it-IT',
|
||||
'ja' => 'ja-JP',
|
||||
'ko' => 'ko-KR',
|
||||
'nl' => 'nl-NL',
|
||||
'pl' => 'pl-PL',
|
||||
'pt' => 'pt-BR',
|
||||
'ru' => 'ru-RU',
|
||||
'th' => 'th-TH',
|
||||
'tr' => 'tr-TR',
|
||||
'vi' => 'vi-VN',
|
||||
'zh' => 'zh-CN'
|
||||
}.freeze
|
||||
|
||||
pattr_initialize [:event_name!, :hook!, :event_data!]
|
||||
|
||||
private
|
||||
@@ -84,7 +129,7 @@ class Integrations::Dialogflow::ProcessorService < Integrations::BotProcessorSer
|
||||
def detect_intent(session_id, message)
|
||||
client = ::Google::Cloud::Dialogflow::V2::Sessions::Client.new
|
||||
session = build_session_path(session_id)
|
||||
query_input = { text: { text: message, language_code: 'en-US' } }
|
||||
query_input = { text: { text: message, language_code: dialogflow_language_code } }
|
||||
client.detect_intent session: session, query_input: query_input
|
||||
end
|
||||
|
||||
@@ -98,4 +143,30 @@ class Integrations::Dialogflow::ProcessorService < Integrations::BotProcessorSer
|
||||
"projects/#{project_id}/locations/#{region}/agent/sessions/#{session_id}"
|
||||
end
|
||||
end
|
||||
|
||||
def dialogflow_language_code
|
||||
configured_language = hook.settings['language_code'].to_s.strip
|
||||
return 'en-US' if configured_language.blank?
|
||||
return configured_language if configured_language != 'auto'
|
||||
|
||||
normalized_contact_language_code(conversation&.contact&.additional_attributes&.dig('language_code')) || 'en-US'
|
||||
end
|
||||
|
||||
def normalized_contact_language_code(language_code)
|
||||
canonicalized_language_code = canonical_language_code(language_code)
|
||||
return if canonicalized_language_code.blank?
|
||||
return canonicalized_language_code if SUPPORTED_LANGUAGE_CODES.include?(canonicalized_language_code)
|
||||
|
||||
AUTO_LANGUAGE_CODE_MAP[canonicalized_language_code] || AUTO_LANGUAGE_CODE_MAP[canonicalized_language_code.split('-', 2).first]
|
||||
end
|
||||
|
||||
def canonical_language_code(language_code)
|
||||
normalized_language_code = language_code.to_s.tr('_', '-').strip
|
||||
return if normalized_language_code.blank?
|
||||
|
||||
language, region = normalized_language_code.split('-', 2)
|
||||
return language.downcase if region.blank?
|
||||
|
||||
"#{language.downcase}-#{region.upcase}"
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user