email_agent.rb 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. module Agents
  2. class EmailAgent < Agent
  3. include EmailConcern
  4. cannot_be_scheduled!
  5. cannot_create_events!
  6. description <<-MD
  7. The EmailAgent sends any events it receives via email immediately.
  8. The email will have a `subject` and an optional `headline` before listing the Events. If the Events' payloads
  9. contain a `:message`, that will be highlighted, otherwise everything in their payloads will be shown.
  10. You can specify one or more `recipients` for the email, or skip the option in order to send the email to your
  11. account's default email address.
  12. 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.
  13. MD
  14. def default_options
  15. {
  16. 'subject' => "You have a notification!",
  17. 'headline' => "Your notification:",
  18. 'expected_receive_period_in_days' => "2"
  19. }
  20. end
  21. def receive(incoming_events)
  22. incoming_events.each do |event|
  23. log "Sending digest mail to #{user.email} with event #{event.id}"
  24. recipients(event.payload).each do |recipient|
  25. SystemMailer.delay.send_message(:to => recipient, :subject => interpolated(event)['subject'], :headline => interpolated(event)['headline'], :groups => [present(event.payload)])
  26. end
  27. end
  28. end
  29. end
  30. end