google_translation_agent.rb 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. module Agents
  2. class GoogleTranslationAgent < Agent
  3. cannot_be_scheduled!
  4. gem_dependency_check { defined?(Google) && defined?(Google::Cloud::Translate) }
  5. description <<-MD
  6. The Translation Agent will attempt to translate text between natural languages.
  7. #{'## Include `google-api-client` in your Gemfile to use this Agent!' if dependencies_missing?}
  8. 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.
  9. The service is **not free**.
  10. To use credentials for the `google_api_key` use the liquid `credential` tag like so `{% credential google-api-key %}`
  11. `to` must be filled with a [translator language code](https://cloud.google.com/translate/docs/languages).
  12. `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.
  13. Specify what you would like to translate in `content` field, you can use [Liquid](https://github.com/huginn/huginn/wiki/Formatting-Events-using-Liquid) specify which part of the payload you want to translate.
  14. `expected_receive_period_in_days` is the maximum number of days you would allow to pass between events.
  15. MD
  16. event_description "User defined"
  17. def default_options
  18. {
  19. 'to' => "sv",
  20. 'from' => 'en',
  21. 'google_api_key' => '',
  22. 'expected_receive_period_in_days' => 1,
  23. 'content' => {
  24. 'text' => "{{message}}",
  25. 'moretext' => "{{another message}}"
  26. }
  27. }
  28. end
  29. def working?
  30. last_receive_at && last_receive_at > interpolated['expected_receive_period_in_days'].to_i.days.ago && !recent_error_logs?
  31. end
  32. def validate_options
  33. unless options['google_api_key'].present? && options['to'].present? && options['content'].present? && options['expected_receive_period_in_days'].present?
  34. errors.add :base, "google_api_key, to, content and expected_receive_period_in_days are all required"
  35. end
  36. end
  37. def translate_from
  38. interpolated["from"].presence
  39. end
  40. def receive(incoming_events)
  41. incoming_events.each do |event|
  42. translated_event = {}
  43. opts = interpolated(event)
  44. opts['content'].each_pair do |key, value|
  45. result = translate(value)
  46. translated_event[key] = result.text
  47. end
  48. create_event payload: translated_event
  49. end
  50. end
  51. def google_client
  52. @google_client ||= Google::APIClient.new(
  53. {
  54. application_name: "Huginn",
  55. application_version: "0.0.1",
  56. key: options['google_api_key'],
  57. authorization: nil
  58. }
  59. )
  60. end
  61. def translate_service
  62. @translate_service ||= google_client.discovered_api('translate','v2')
  63. end
  64. def cloud_translate_service
  65. # https://github.com/GoogleCloudPlatform/google-cloud-ruby/blob/master/google-cloud-translate/lib/google-cloud-translate.rb#L130
  66. @google_client ||= Google::Cloud::Translate.new(key: interpolated['google_api_key'])
  67. end
  68. def translate(value)
  69. # google_client.execute(
  70. # api_method: translate_service.translations.list,
  71. # parameters: {
  72. # format: 'text',
  73. # source: translate_from,
  74. # target: options["to"],
  75. # q: value
  76. # }
  77. # )
  78. cloud_translate_service.translate(value, to: interpolated["to"], from: translate_from, format: "text")
  79. end
  80. end
  81. end