translation_agent.rb 3.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. module Agents
  2. class TranslationAgent < Agent
  3. include LiquidInterpolatable
  4. cannot_be_scheduled!
  5. description <<-MD
  6. You can use Translation Agent to translate text between natural languages.
  7. Services are provided using Microsoft Translator. You can [sign up](https://datamarket.azure.com/dataset/bing/microsofttranslator) and [register your application](https://datamarket.azure.com/developer/applications/register) to get `client_id` and `client_secret` which are required to use this agent.
  8. `to` must be filled with a [translator language code](http://msdn.microsoft.com/en-us/library/hh456380.aspx).
  9. 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.
  10. `expected_receive_period_in_days` is the maximum number of days you would allow to pass between events.
  11. MD
  12. event_description "User defined"
  13. def default_options
  14. {
  15. 'client_id' => "xxxxxx",
  16. 'client_secret' => "xxxxxx",
  17. 'to' => "fi",
  18. 'expected_receive_period_in_days' => 1,
  19. 'content' => {
  20. 'text' => "{{message.text}}",
  21. 'content' => "{{xyz}}"
  22. }
  23. }
  24. end
  25. def working?
  26. last_receive_at && last_receive_at > options['expected_receive_period_in_days'].to_i.days.ago && !recent_error_logs?
  27. end
  28. def translate(text, to, access_token)
  29. translate_uri = URI 'http://api.microsofttranslator.com/v2/Ajax.svc/Translate'
  30. params = {
  31. 'text' => text,
  32. 'to' => to
  33. }
  34. translate_uri.query = URI.encode_www_form params
  35. request = Net::HTTP::Get.new translate_uri.request_uri
  36. request['Authorization'] = "Bearer" + " " + access_token
  37. http = Net::HTTP.new translate_uri.hostname, translate_uri.port
  38. response = http.request request
  39. YAML.load response.body
  40. end
  41. def validate_options
  42. unless options['client_id'].present? && options['client_secret'].present? && options['to'].present? && options['content'].present? && options['expected_receive_period_in_days'].present?
  43. errors.add :base, "client_id,client_secret,to,expected_receive_period_in_days and content are all required"
  44. end
  45. end
  46. def postform(uri, params)
  47. req = Net::HTTP::Post.new(uri.request_uri)
  48. req.form_data = params
  49. Net::HTTP.start(uri.hostname, uri.port, :use_ssl => true) { |http| http.request(req) }
  50. end
  51. def receive(incoming_events)
  52. auth_uri = URI "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13"
  53. response = postform auth_uri, :client_id => options['client_id'],
  54. :client_secret => options['client_secret'],
  55. :scope => "http://api.microsofttranslator.com",
  56. :grant_type => "client_credentials"
  57. access_token = JSON.parse(response.body)["access_token"]
  58. incoming_events.each do |event|
  59. translated_event = {}
  60. options['content'].each_pair do |key, value|
  61. to_be_translated = interpolate_string(value, event.payload)
  62. translated_event[key] = translate(to_be_translated.first, options['to'], access_token)
  63. end
  64. create_event :payload => translated_event
  65. end
  66. end
  67. end
  68. end