event_formatting_agent.rb 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. module Agents
  2. class EventFormattingAgent < Agent
  3. include LiquidInterpolatable
  4. cannot_be_scheduled!
  5. description <<-MD
  6. An Event Formatting Agent allows you to format incoming Events, adding new fields as needed.
  7. For example, here is a possible Event:
  8. {
  9. "high": {
  10. "celsius": "18",
  11. "fahreinheit": "64"
  12. },
  13. "date": {
  14. "epoch": "1357959600",
  15. "pretty": "10:00 PM EST on January 11, 2013"
  16. },
  17. "conditions": "Rain showers",
  18. "data": "This is some data"
  19. }
  20. You may want to send this event to another Agent, for example a Twilio Agent, which expects a `message` key.
  21. You can use an Event Formatting Agent's `instructions` setting to do this in the following way:
  22. "instructions": {
  23. "message": "Today's conditions look like {{conditions}} with a high temperature of {{high.celsius}} degrees Celsius.",
  24. "subject": "{{data}}"
  25. }
  26. Have a look at the [Wiki](https://github.com/cantino/huginn/wiki/Formatting-Events-using-Liquid) to learn more about liquid templating.
  27. Events generated by this possible Event Formatting Agent will look like:
  28. {
  29. "message": "Today's conditions look like Rain showers with a high temperature of 18 degrees Celsius.",
  30. "subject": "This is some data"
  31. }
  32. In `matchers` setting you can perform regular expression matching against contents of events and expand the match data for use in `instructions` setting. Here is an example:
  33. {
  34. "matchers": [
  35. {
  36. "path": "{{date.pretty}}",
  37. "regexp": "\\A(?<time>\\d\\d:\\d\\d [AP]M [A-Z]+)",
  38. "to": "pretty_date",
  39. }
  40. ]
  41. }
  42. This virtually merges the following hash into the original event hash:
  43. "pretty_date": {
  44. "time": "10:00 PM EST",
  45. "0": "10:00 PM EST on January 11, 2013"
  46. "1": "10:00 PM EST",
  47. }
  48. So you can use it in `instructions` like this:
  49. "instructions": {
  50. "message": "Today's conditions look like <$.conditions> with a high temperature of {{high.celsius}} degrees Celsius according to the forecast at {{pretty_date.time}}.",
  51. "subject": "{{data}}"
  52. }
  53. If you want to retain original contents of events and only add new keys, then set `mode` to `merge`, otherwise set it to `clean`.
  54. 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`.
  55. To CGI escape output (for example when creating a link), use the Liquid `uri_escape` filter, like so:
  56. {
  57. "message": "A peak was on Twitter in {{group_by}}. Search: https://twitter.com/search?q={{group_by | uri_escape}}"
  58. }
  59. MD
  60. event_description "User defined"
  61. after_save :clear_matchers
  62. def validate_options
  63. 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?
  64. validate_matchers
  65. end
  66. def default_options
  67. {
  68. 'instructions' => {
  69. 'message' => "You received a text {{text}} from {{fields.from}}",
  70. 'some_other_field' => "Looks like the weather is going to be {{fields.weather}}"
  71. },
  72. 'matchers' => [],
  73. 'mode' => "clean",
  74. 'skip_agent' => "false",
  75. 'skip_created_at' => "false"
  76. }
  77. end
  78. def working?
  79. !recent_error_logs?
  80. end
  81. def receive(incoming_events)
  82. incoming_events.each do |event|
  83. formatted_event = options['mode'].to_s == "merge" ? event.payload.dup : {}
  84. payload = perform_matching(event.payload)
  85. formatted_event.merge! interpolate_options(options['instructions'], payload)
  86. formatted_event['agent'] = Agent.find(event.agent_id).type.slice!(8..-1) unless options['skip_agent'].to_s == "true"
  87. formatted_event['created_at'] = event.created_at unless options['skip_created_at'].to_s == "true"
  88. create_event :payload => formatted_event
  89. end
  90. end
  91. private
  92. def validate_matchers
  93. matchers = options['matchers'] or return
  94. unless matchers.is_a?(Array)
  95. errors.add(:base, "matchers must be an array if present")
  96. return
  97. end
  98. matchers.each do |matcher|
  99. unless matcher.is_a?(Hash)
  100. errors.add(:base, "each matcher must be a hash")
  101. next
  102. end
  103. regexp, path, to = matcher.values_at(*%w[regexp path to])
  104. if regexp.present?
  105. begin
  106. Regexp.new(regexp)
  107. rescue
  108. errors.add(:base, "bad regexp found in matchers: #{regexp}")
  109. end
  110. else
  111. errors.add(:base, "regexp is mandatory for a matcher and must be a string")
  112. end
  113. errors.add(:base, "path is mandatory for a matcher and must be a string") if !path.present?
  114. errors.add(:base, "to must be a string if present in a matcher") if to.present? && !to.is_a?(String)
  115. end
  116. end
  117. def perform_matching(payload)
  118. matchers.inject(payload.dup) { |hash, matcher|
  119. matcher[hash]
  120. }
  121. end
  122. def matchers
  123. @matchers ||=
  124. if matchers = options['matchers']
  125. matchers.map { |matcher|
  126. regexp, path, to = matcher.values_at(*%w[regexp path to])
  127. re = Regexp.new(regexp)
  128. proc { |hash|
  129. mhash = {}
  130. value = interpolate_string(path, hash)
  131. if value.is_a?(String) && (m = re.match(value))
  132. m.to_a.each_with_index { |s, i|
  133. mhash[i.to_s] = s
  134. }
  135. m.names.each do |name|
  136. mhash[name] = m[name]
  137. end if m.respond_to?(:names)
  138. end
  139. if to
  140. case value = hash[to]
  141. when Hash
  142. value.update(mhash)
  143. else
  144. hash[to] = mhash
  145. end
  146. else
  147. hash.update(mhash)
  148. end
  149. hash
  150. }
  151. }
  152. else
  153. []
  154. end
  155. end
  156. def clear_matchers
  157. @matchers = nil
  158. end
  159. end
  160. end