pushbullet_agent.rb 4.2 KB

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