Нет описания http://j1x-huginn.herokuapp.com

google_calendar_publish_agent.rb 3.8KB

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