translation_agent.rb 3.2 KB

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