Keine Beschreibung http://j1x-huginn.herokuapp.com

pushbullet_agent.rb 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. To register a new device run the following command:
  12. `curl -u <your api key here>: -X POST https://api.pushbullet.com/v2/devices -d nickname=huginn -d type=stream`
  13. Put one of the retured `iden` strings into the `device_id` field.
  14. You have to provide a message `type` which has to be `note`, `link`, or `address`. The message types `checklist`, and `file` are not supported at the moment.
  15. Depending on the message `type` you can use additional fields:
  16. * note: `title` and `body`
  17. * link: `title`, `body`, and `url`
  18. * address: `name`, and `address`
  19. 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).
  20. MD
  21. def default_options
  22. {
  23. 'api_key' => '',
  24. 'device_id' => '',
  25. 'title' => "{{title}}",
  26. 'body' => '{{body}}',
  27. 'type' => 'note',
  28. }
  29. end
  30. def validate_options
  31. errors.add(:base, "you need to specify a pushbullet api_key") unless options['api_key'].present?
  32. errors.add(:base, "you need to specify a device_id") if options['device_id'].blank?
  33. errors.add(:base, "you need to specify a valid message type") if options['type'].blank? or not ['note', 'link', 'address'].include?(options['type'])
  34. end
  35. def working?
  36. received_event_without_error?
  37. end
  38. def receive(incoming_events)
  39. incoming_events.each do |event|
  40. response = HTTParty.post "https://api.pushbullet.com/api/pushes", query_options(event)
  41. error(response.body) if response.body.include? 'error'
  42. end
  43. end
  44. private
  45. def query_options(event)
  46. mo = interpolated(event)
  47. body = {:device_iden => mo[:device_id], :type => mo[:type]}
  48. if mo[:type] == "note"
  49. body[:title] = mo[:title]
  50. body[:body] = mo[:body]
  51. elsif mo[:type] == "link"
  52. body[:title] = mo[:title]
  53. body[:body] = mo[:body]
  54. body[:url] = mo[:url]
  55. elsif mo[:type] == "address"
  56. body[:name] = mo[:name]
  57. body[:address] = mo[:address]
  58. end
  59. {
  60. :basic_auth => {:username => mo[:api_key], :password => ''},
  61. :body => body
  62. }
  63. end
  64. end
  65. end