Feat: detect language of the message content (#6660)
This commit is contained in:
@@ -63,6 +63,13 @@
|
||||
"filter_operators": [ "equal_to", "not_equal_to", "contains", "does_not_contain" ],
|
||||
"attribute_type": "additional_attributes"
|
||||
},
|
||||
"conversation_language": {
|
||||
"attribute_name": "Conversation Language",
|
||||
"input_type": "textbox",
|
||||
"data_type": "text",
|
||||
"filter_operators": [ "equal_to", "not_equal_to" ],
|
||||
"attribute_type": "additional_attributes"
|
||||
},
|
||||
"mail_subject": {
|
||||
"attribute_name": "Email Subject",
|
||||
"input_type": "textbox",
|
||||
|
||||
@@ -63,6 +63,13 @@
|
||||
"filter_operators": [ "equal_to", "not_equal_to", "contains", "does_not_contain" ],
|
||||
"attribute_type": "additional_attributes"
|
||||
},
|
||||
"conversation_language": {
|
||||
"attribute_name": "Conversation Language",
|
||||
"input_type": "textbox",
|
||||
"data_type": "text",
|
||||
"filter_operators": [ "equal_to", "not_equal_to" ],
|
||||
"attribute_type": "additional_attributes"
|
||||
},
|
||||
"country_code": {
|
||||
"attribute_name": "Country Name",
|
||||
"input_type": "textbox",
|
||||
|
||||
40
lib/integrations/google_translate/detect_language_service.rb
Normal file
40
lib/integrations/google_translate/detect_language_service.rb
Normal file
@@ -0,0 +1,40 @@
|
||||
class Integrations::GoogleTranslate::DetectLanguageService
|
||||
pattr_initialize [:hook!, :message!]
|
||||
|
||||
def perform
|
||||
return unless valid_message?
|
||||
return if conversation.additional_attributes['conversation_language'].present?
|
||||
|
||||
text = message.content[0...1500]
|
||||
response = client.detect_language(
|
||||
content: text,
|
||||
parent: "projects/#{hook.settings['project_id']}"
|
||||
)
|
||||
|
||||
update_conversation(response)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def valid_message?
|
||||
message.incoming? && message.content.present?
|
||||
end
|
||||
|
||||
def conversation
|
||||
@conversation ||= message.conversation
|
||||
end
|
||||
|
||||
def update_conversation(response)
|
||||
return if response&.languages.blank?
|
||||
|
||||
conversation_language = response.languages.first.language_code
|
||||
additional_attributes = conversation.additional_attributes.merge({ conversation_language: conversation_language })
|
||||
conversation.update!(additional_attributes: additional_attributes)
|
||||
end
|
||||
|
||||
def client
|
||||
@client ||= Google::Cloud::Translate.translation_service do |config|
|
||||
config.credentials = hook.settings['credentials']
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user