-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Google translate #1978
Google translate #1978
Changes from 11 commits
772662a
09aae9c
584afdc
25c73b8
3514ede
dddd9fc
3042a38
424d04b
5de4f1f
6645376
61f14d1
c38f602
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
require 'google/api_client' | ||
module Agents | ||
class GoogleTranslationAgent < Agent | ||
cannot_be_scheduled! | ||
|
||
description <<-MD | ||
The Translation Agent will attempt to translate text between natural languages. | ||
|
||
Services are provided using Google Translate. You can [sign up](https://cloud.google.com/translate/) to get `google_api_key` which is required to use this agent. | ||
The service is **not free**. | ||
|
||
`to` must be filled with a [translator language code](https://cloud.google.com/translate/docs/languages). | ||
|
||
`from` is the language translated from. If it's not specified, the API will attempt to detect the source language automatically and return it within the response. | ||
|
||
Specify what you would like to translate in `content` field, you can use [Liquid](https://github.com/cantino/huginn/wiki/Formatting-Events-using-Liquid) specify which part of the payload you want to translate. | ||
|
||
`expected_receive_period_in_days` is the maximum number of days you would allow to pass between events. | ||
MD | ||
|
||
event_description "User defined" | ||
|
||
def default_options | ||
{ | ||
'to' => "sv", | ||
'from' => 'en', | ||
'google_api_key' => '', | ||
'expected_receive_period_in_days' => 1, | ||
'content' => { | ||
'text' => "{{message}}", | ||
'moretext' => "{{another message}}" | ||
} | ||
} | ||
end | ||
|
||
def working? | ||
last_receive_at && last_receive_at > interpolated['expected_receive_period_in_days'].to_i.days.ago && !recent_error_logs? | ||
end | ||
|
||
def validate_options | ||
unless options['google_api_key'].present? && options['to'].present? && options['content'].present? && options['expected_receive_period_in_days'].present? | ||
errors.add :base, "google_api_key, to, content and expected_receive_period_in_days are all required" | ||
end | ||
end | ||
|
||
def translate_from | ||
interpolated["from"].presence || 'en' | ||
end | ||
|
||
def receive(incoming_events) | ||
incoming_events.each do |event| | ||
translated_event = {} | ||
opts = interpolated(event) | ||
opts['content'].each_pair do |key, value| | ||
result = translate(value) | ||
translated_event[key] = result.data.translations.last.translated_text | ||
end | ||
create_event payload: translated_event | ||
end | ||
end | ||
|
||
def google_client | ||
@google_client ||= Google::APIClient.new( | ||
{ | ||
application_name: "Huginn", | ||
application_version: "0.0.1", | ||
key: options['google_api_key'], | ||
authorization: nil | ||
} | ||
) | ||
end | ||
|
||
def translate_service | ||
@translate_service ||= google_client.discovered_api('translate','v2') | ||
end | ||
|
||
def translate(value) | ||
google_client.execute( | ||
api_method: translate_service.translations.list, | ||
parameters: { | ||
format: 'text', | ||
source: translate_from, | ||
target: options["to"], | ||
q: value | ||
} | ||
) | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I noticed that you memoize the method translate_service I agree it's better for performance but in our case the method has options['google_api_key'] as parameter. What do you think about using a hash with the value of options['google_api_key'] as keys in case a user changes the API key. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah it's only initialized once at startup? Then it's probably a good idea yeah. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it should be fine, since the API key is not interpolated it can be safely memoized. The instance variable will only be set for one runtime of the Agent. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dsander you are right, it will be memoized for an instance scope :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍🏻 |
||
end | ||
end |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add back in the
require: 'google/api_client'
so that the GoogleCalendarPublishAgent will also work ok? I think you should also copy thegem_dependency_check
and#{'## Include "google-api-client" in your Gemfile to use this Agent!' if dependencies_missing?}
lines from GoogleCalendarPublishAgent into your Agent so that it correctly detects ifgoogle-api-client
has been removed from the Gemfile, and remove therequire
line that you added at the top.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done and done!