google_calendar_publish_agent.rb 3.5 KB

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