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

@@ -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",

View File

@@ -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",

View 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