event_formatting_agent.rb 6.7 KB

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