pushbullet_agent.rb 1.7 KB

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