email_agent.rb 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. You can specify the email's subject line by providing a `subject` option, which can contain Liquid formatting. E.g.,
  9. you could provide `"Huginn email"` to set a simple subject, or `{{subject}}` to use the `subject` key from the incoming Event.
  10. By default, the email body will contain an optional `headline`, followed by a listing of the Events' keys.
  11. You can customize the email body by including the optional `body` param. Like the `subject`, the `body` can be a simple message
  12. or a Liquid template. You could send only the Event's `some_text` field with a `body` set to `{{ some_text }}`.
  13. The body can contain simple HTML and will be sanitized.
  14. You can specify one or more `recipients` for the email, or skip the option in order to send the email to your
  15. account's default email address.
  16. 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.
  17. MD
  18. def default_options
  19. {
  20. 'subject' => "You have a notification!",
  21. 'headline' => "Your notification:",
  22. 'expected_receive_period_in_days' => "2"
  23. }
  24. end
  25. def receive(incoming_events)
  26. incoming_events.each do |event|
  27. log "Sending digest mail to #{user.email} with event #{event.id}"
  28. recipients(event.payload).each do |recipient|
  29. SystemMailer.send_message(:to => recipient, :subject => interpolated(event)['subject'], :headline => interpolated(event)['headline'], :body => interpolated(event)['body'], :groups => [present(event.payload)]).deliver_later
  30. end
  31. end
  32. end
  33. end
  34. end