pushbullet_agent.rb 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. module Agents
  2. class PushbulletAgent < Agent
  3. include FormConfigurable
  4. cannot_be_scheduled!
  5. cannot_create_events!
  6. before_validation :create_device, on: :create
  7. API_BASE = 'https://api.pushbullet.com/v2/'
  8. TYPE_TO_ATTRIBUTES = {
  9. 'note' => [:title, :body],
  10. 'link' => [:title, :body, :url],
  11. 'address' => [:name, :address]
  12. }
  13. class Unauthorized < StandardError; end
  14. description <<-MD
  15. The Pushbullet agent sends pushes to a pushbullet device
  16. To authenticate you need to either the `api_key` or create a `pushbullet_api_key` credential, you can find yours at your account page:
  17. `https://www.pushbullet.com/account`
  18. If you do not select an existing device, Huginn will create a new one with the name 'Huginn'.
  19. 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.
  20. Depending on the message `type` you can use additional fields:
  21. * note: `title` and `body`
  22. * link: `title`, `body`, and `url`
  23. * address: `name`, and `address`
  24. 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).
  25. MD
  26. def default_options
  27. {
  28. 'api_key' => '',
  29. 'device_id' => '',
  30. 'title' => "{{title}}",
  31. 'body' => '{{body}}',
  32. 'type' => 'note',
  33. }
  34. end
  35. form_configurable :api_key, roles: :validatable
  36. form_configurable :device_id, roles: :completable
  37. form_configurable :type, type: :array, values: ['note', 'link', 'address']
  38. form_configurable :title
  39. form_configurable :body, type: :text
  40. form_configurable :url
  41. form_configurable :name
  42. form_configurable :address
  43. def validate_options
  44. errors.add(:base, "you need to specify a pushbullet api_key") if options['api_key'].blank?
  45. errors.add(:base, "you need to specify a device_id") if options['device_id'].blank?
  46. errors.add(:base, "you need to specify a valid message type") if options['type'].blank? or not ['note', 'link', 'address'].include?(options['type'])
  47. TYPE_TO_ATTRIBUTES[options['type']].each do |attr|
  48. errors.add(:base, "you need to specify '#{attr.to_s}' for the type '#{options['type']}'") if options[attr].blank?
  49. end
  50. end
  51. def validate_api_key
  52. devices
  53. true
  54. rescue Unauthorized
  55. false
  56. end
  57. def complete_device_id
  58. devices.map { |d| {text: d['nickname'], id: d['iden']} }
  59. end
  60. def working?
  61. received_event_without_error?
  62. end
  63. def receive(incoming_events)
  64. incoming_events.each do |event|
  65. safely do
  66. response = request(:post, 'pushes', query_options(event))
  67. end
  68. end
  69. end
  70. private
  71. def safely
  72. yield
  73. rescue Unauthorized => e
  74. error(e.message)
  75. end
  76. def request(http_method, method, options)
  77. response = JSON.parse(HTTParty.send(http_method, API_BASE + method, options).body)
  78. raise Unauthorized, response['error']['message'] if response['error'].present?
  79. response
  80. end
  81. def devices
  82. response = request(:get, 'devices', basic_auth)
  83. response['devices'].select { |d| d['pushable'] == true }
  84. rescue Unauthorized
  85. []
  86. end
  87. def create_device
  88. return if options['device_id'].present?
  89. safely do
  90. response = request(:post, 'devices', basic_auth.merge(body: {nickname: 'Huginn', type: 'stream'}))
  91. self.options[:device_id] = response['iden']
  92. end
  93. end
  94. def basic_auth
  95. {basic_auth: {username: interpolated[:api_key].presence || credential('pushbullet_api_key'), password: ''}}
  96. end
  97. def query_options(event)
  98. mo = interpolated(event)
  99. basic_auth.merge(body: {device_iden: mo[:device_id], type: mo[:type]}.merge(payload(mo)))
  100. end
  101. def payload(mo)
  102. Hash[TYPE_TO_ATTRIBUTES[mo[:type]].map { |k| [k, mo[k]] }]
  103. end
  104. end
  105. end