Keine Beschreibung http://j1x-huginn.herokuapp.com

event_formatting_agent.rb 3.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. module Agents
  2. class EventFormattingAgent < Agent
  3. cannot_be_scheduled!
  4. description <<-MD
  5. An Event Formatting Agent allows you to format incoming Events, adding new fields as needed.
  6. For example, here is a possible Event:
  7. {
  8. "high": {
  9. "celsius": "18",
  10. "fahreinheit": "64"
  11. },
  12. "conditions": "Rain showers",
  13. "data": "This is some data"
  14. }
  15. You may want to send this event to another Agent, for example a Twilio Agent, which expects a `message` key.
  16. You can use an Event Formatting Agent's `instructions` setting to do this in the following way:
  17. "instructions": {
  18. "message": "Today's conditions look like <$.conditions> with a high temperature of <$.high.celsius> degrees Celsius.",
  19. "subject": "$.data"
  20. }
  21. JSONPaths must be between < and > . Make sure that you don't use these symbols anywhere else.
  22. Events generated by this possible Event Formatting Agent will look like:
  23. {
  24. "message": "Today's conditions look like Rain showers with a high temperature of 18 degrees Celsius.",
  25. "subject": "This is some data"
  26. }
  27. If you want to retain original contents of events and only add new keys, then set `mode` to `merge`, otherwise set it to `clean`.
  28. By default, the output event will have `agent` and `created_at` fields added as well, reflecting the original Agent type and Event creation time. You can skip these outputs by setting `skip_agent` and `skip_created_at` to `true`.
  29. To CGI escape output (for example when creating a link), prefix with `escape`, like so:
  30. {
  31. "message": "A peak was on Twitter in <$.group_by>. Search: https://twitter.com/search?q=<escape $.group_by>"
  32. }
  33. MD
  34. event_description "User defined"
  35. def validate_options
  36. errors.add(:base, "instructions, mode, skip_agent, and skip_created_at all need to be present.") unless options['instructions'].present? and options['mode'].present? and options['skip_agent'].present? and options['skip_created_at'].present?
  37. end
  38. def default_options
  39. {
  40. 'instructions' => {
  41. 'message' => "You received a text <$.text> from <$.fields.from>",
  42. 'some_other_field' => "Looks like the weather is going to be <$.fields.weather>"
  43. },
  44. 'mode' => "clean",
  45. 'skip_agent' => "false",
  46. 'skip_created_at' => "false"
  47. }
  48. end
  49. def working?
  50. !recent_error_logs?
  51. end
  52. def receive(incoming_events)
  53. incoming_events.each do |event|
  54. formatted_event = options['mode'].to_s == "merge" ? event.payload : {}
  55. options['instructions'].each_pair {|key, value| formatted_event[key] = Utils.interpolate_jsonpaths(value, event.payload) }
  56. formatted_event['agent'] = Agent.find(event.agent_id).type.slice!(8..-1) unless options['skip_agent'].to_s == "true"
  57. formatted_event['created_at'] = event.created_at unless options['skip_created_at'].to_s == "true"
  58. create_event :payload => formatted_event
  59. end
  60. end
  61. end
  62. end