pushbullet_agent.rb 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. module Agents
  2. class PushbulletAgent < Agent
  3. include LiquidInterpolatable
  4. cannot_be_scheduled!
  5. cannot_create_events!
  6. description <<-MD
  7. The Pushbullet agent sends pushes to a pushbullet device
  8. To authenticate you need to set the `api_key`, you can find yours at your account page:
  9. `https://www.pushbullet.com/account`
  10. Currently you need to get a the device identification manually:
  11. `curl -u <your api key here>: https://api.pushbullet.com/api/devices`
  12. Put one of the retured `iden` strings into the `device_id` field.
  13. You can provide a `title` and a `body`.
  14. In every value of the options hash you can use the liquid templating, learn more about it at the [Wiki](https://github.com/cantino/huginn/wiki/Formatting-Events-using-Liquid).
  15. MD
  16. def default_options
  17. {
  18. 'api_key' => '',
  19. 'device_id' => '',
  20. 'title' => "Hello from Huginn!",
  21. 'body' => '{{body}}',
  22. }
  23. end
  24. def validate_options
  25. errors.add(:base, "you need to specify a pushbullet api_key") unless options['api_key'].present?
  26. errors.add(:base, "you need to specify a device_id") if options['device_id'].blank?
  27. end
  28. def working?
  29. received_event_without_error?
  30. end
  31. def receive(incoming_events)
  32. incoming_events.each do |event|
  33. response = HTTParty.post "https://api.pushbullet.com/api/pushes", query_options(event)
  34. error(response.body) if response.body.include? 'error'
  35. end
  36. end
  37. private
  38. def query_options(event)
  39. mo = interpolate_options options, event.payload
  40. {
  41. :basic_auth => {:username =>mo[:api_key], :password=>''},
  42. :body => {:device_iden => mo[:device_id], :title => mo[:title], :body => mo[:body], :type => 'note'}
  43. }
  44. end
  45. end
  46. end