google_calendar_publish_agent.rb 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. require 'json'
  2. module Agents
  3. class GoogleCalendarPublishAgent < Agent
  4. cannot_be_scheduled!
  5. description <<-MD
  6. The GoogleCalendarPublishAgent creates events on your google calendar.
  7. This agent relies on service accounts, rather than oauth.
  8. Setup:
  9. 1. Visit [the google api console](https://code.google.com/apis/console/b/0/)
  10. 2. New project -> Huginn
  11. 3. APIs & Auth -> Enable google calendar
  12. 4. Credentials -> Create new Client ID -> Service Account
  13. 5. Persist the generated private key to a path, ie: `/home/hugin/a822ccdefac89fac6330f95039c492dfa3ce6843.p12`
  14. 6. Grant access via google calendar UI to the service account email address for each calendar you wish to manage. For a whole google apps domain, you can [delegate authority](https://developers.google.com/+/domains/authentication/delegation)
  15. Agent Configuration:
  16. `calendar_id` - The id the calendar you want to publish to. Typically your google account email address.
  17. `google` A hash of configuration options for the agent.
  18. `google` `service_account_email` - The authorised service account.
  19. `google` `key_file` - The path to the key file.
  20. `google` `key_secret` - The secret for the key, typically 'notasecret'
  21. Set `expected_update_period_in_days` to the maximum amount of time that you'd expect to pass between Events being created by this Agent.
  22. Use it with a trigger agent to shape your payload!
  23. A hash of event details. See the [Google Calendar API docs](https://developers.google.com/google-apps/calendar/v3/reference/events/insert)
  24. Example payload for trigger agent:
  25. <pre><code>{
  26. "message": {
  27. "visibility": "default",
  28. "summary": "Awesome event",
  29. "description": "An example event with text. Pro tip: DateTimes are in RFC3339",
  30. "start": {
  31. "dateTime": "2014-10-02T10:00:00-05:00"
  32. },
  33. "end": {
  34. "dateTime": "2014-10-02T11:00:00-05:00"
  35. }
  36. }
  37. }</code></pre>
  38. MD
  39. event_description <<-MD
  40. {
  41. 'success' => true,
  42. 'published_calendar_event' => {
  43. ....
  44. },
  45. 'agent_id' => 1234,
  46. 'event_id' => 3432
  47. }
  48. MD
  49. def validate_options
  50. errors.add(:base, "expected_update_period_in_days is required") unless options['expected_update_period_in_days'].present?
  51. end
  52. def working?
  53. event_created_within?(options['expected_update_period_in_days']) && most_recent_event && most_recent_event.payload['success'] == true && !recent_error_logs?
  54. end
  55. def default_options
  56. {
  57. 'expected_update_period_in_days' => "10",
  58. 'calendar_id' => 'you@email.com',
  59. 'google' => {
  60. 'key_file' => '/path/to/private.key',
  61. 'key_secret' => 'notasecret',
  62. 'service_account_email' => ''
  63. }
  64. }
  65. end
  66. def receive(incoming_events)
  67. incoming_events.each do |event|
  68. calendar = GoogleCalendar.new(options, Rails.logger)
  69. calendar_event = JSON.parse(calendar.publish_as(options['calendar_id'], event.payload["message"]).response.body)
  70. create_event :payload => {
  71. 'success' => true,
  72. 'published_calendar_event' => calendar_event,
  73. 'agent_id' => event.agent_id,
  74. 'event_id' => event.id
  75. }
  76. end
  77. end
  78. end
  79. end