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

pushbullet_agent.rb 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. module Agents
  2. class PushbulletAgent < Agent
  3. cannot_be_scheduled!
  4. cannot_create_events!
  5. API_URL = 'https://api.pushbullet.com/v2/pushes'
  6. TYPE_TO_ATTRIBUTES = {
  7. 'note' => [:title, :body],
  8. 'link' => [:title, :body, :url],
  9. 'address' => [:name, :address]
  10. }
  11. description <<-MD
  12. The Pushbullet agent sends pushes to a pushbullet device
  13. To authenticate you need to set the `api_key`, you can find yours at your account page:
  14. `https://www.pushbullet.com/account`
  15. Currently you need to get a the device identification manually:
  16. `curl -u <your api key here>: https://api.pushbullet.com/v2/devices`
  17. To register a new device run the following command:
  18. `curl -u <your api key here>: -X POST https://api.pushbullet.com/v2/devices -d nickname=huginn -d type=stream`
  19. Put one of the retured `iden` strings into the `device_id` field.
  20. 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.
  21. Depending on the message `type` you can use additional fields:
  22. * note: `title` and `body`
  23. * link: `title`, `body`, and `url`
  24. * address: `name`, and `address`
  25. 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).
  26. MD
  27. def default_options
  28. {
  29. 'api_key' => '',
  30. 'device_id' => '',
  31. 'title' => "{{title}}",
  32. 'body' => '{{body}}',
  33. 'type' => 'note',
  34. }
  35. end
  36. def validate_options
  37. errors.add(:base, "you need to specify a pushbullet api_key") if options['api_key'].blank?
  38. errors.add(:base, "you need to specify a device_id") if options['device_id'].blank?
  39. errors.add(:base, "you need to specify a valid message type") if options['type'].blank? or not ['note', 'link', 'address'].include?(options['type'])
  40. end
  41. def working?
  42. received_event_without_error?
  43. end
  44. def receive(incoming_events)
  45. incoming_events.each do |event|
  46. response = HTTParty.post API_URL, query_options(event)
  47. error(response.body) if response.body.include? 'error'
  48. end
  49. end
  50. private
  51. def query_options(event)
  52. mo = interpolated(event)
  53. {
  54. :basic_auth => {username: mo[:api_key], password: ''},
  55. :body => {device_iden: mo[:device_id], type: mo[:type]}.merge(payload(mo))
  56. }
  57. end
  58. def payload(mo)
  59. Hash[TYPE_TO_ATTRIBUTES[mo[:type]].map { |k| [k, mo[k]] }]
  60. end
  61. end
  62. end