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

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