email_agent.rb 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. module Agents
  2. class EmailAgent < Agent
  3. include EmailConcern
  4. cannot_be_scheduled!
  5. cannot_create_events!
  6. no_bulk_receive!
  7. description <<-MD
  8. The Email Agent sends any events it receives via email immediately.
  9. You can specify the email's subject line by providing a `subject` option, which can contain Liquid formatting. E.g.,
  10. you could provide `"Huginn email"` to set a simple subject, or `{{subject}}` to use the `subject` key from the incoming Event.
  11. By default, the email body will contain an optional `headline`, followed by a listing of the Events' keys.
  12. You can customize the email body by including the optional `body` param. Like the `subject`, the `body` can be a simple message
  13. or a Liquid template. You could send only the Event's `some_text` field with a `body` set to `{{ some_text }}`.
  14. The body can contain simple HTML and will be sanitized.
  15. You can specify one or more `recipients` for the email, or skip the option in order to send the email to your
  16. account's default email address.
  17. Set `expected_receive_period_in_days` to the maximum amount of time that you'd expect to pass between Events being received by this Agent.
  18. MD
  19. def default_options
  20. {
  21. 'subject' => "You have a notification!",
  22. 'headline' => "Your notification:",
  23. 'expected_receive_period_in_days' => "2"
  24. }
  25. end
  26. def receive(incoming_events)
  27. incoming_events.each do |event|
  28. recipients(event.payload).each do |recipient|
  29. log "Sending digest mail to #{recipient} with event #{event.id}"
  30. SystemMailer.send_message(:to => recipient, :subject => interpolated(event)['subject'], :headline => interpolated(event)['headline'], :body => interpolated(event)['body'], :groups => [present(event.payload)]).deliver_later
  31. end
  32. end
  33. end
  34. end
  35. end