beeper_agent.rb 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. module Agents
  2. class BeeperAgent < Agent
  3. cannot_be_scheduled!
  4. cannot_create_events!
  5. no_bulk_receive!
  6. description <<-MD
  7. Beeper agent sends messages to Beeper app on your mobile device via Push notifications.
  8. You need a Beeper Application ID (`app_id`), Beeper REST API Key (`api_key`) and Beeper Sender ID (`sender_id`) [https://beeper.io](https://beeper.io)
  9. You have to provide phone number (`phone`) of the recipient which have a mobile device with Beeper installed, or a `group_id` – Beeper Group ID
  10. Also you have to provide a message `type` which has to be `message`, `image`, `event`, `location` or `task`.
  11. Depending on message type you have to provide additional fields:
  12. ##### Message
  13. * `text` – **required**
  14. ##### Image
  15. * `image` – **required** (Image URL or Base64-encoded image)
  16. * `text` – optional
  17. ##### Event
  18. * `text` – **required**
  19. * `start_time` – **required** (Corresponding to ISO 8601)
  20. * `end_time` – optional (Corresponding to ISO 8601)
  21. ##### Location
  22. * `latitude` – **required**
  23. * `longitude` – **required**
  24. * `text` – optional
  25. ##### Task
  26. * `text` – **required**
  27. You can see additional documentation at [Beeper website](https://beeper.io/docs)
  28. MD
  29. BASE_URL = 'https://api.beeper.io/api'
  30. TYPE_ATTRIBUTES = {
  31. 'message' => %w(text),
  32. 'image' => %w(text image),
  33. 'event' => %w(text start_time end_time),
  34. 'location' => %w(text latitude longitude),
  35. 'task' => %w(text)
  36. }
  37. MESSAGE_TYPES = TYPE_ATTRIBUTES.keys
  38. TYPE_REQUIRED_ATTRIBUTES = {
  39. 'message' => %w(text),
  40. 'image' => %w(image),
  41. 'event' => %w(text start_time),
  42. 'location' => %w(latitude longitude),
  43. 'task' => %w(text)
  44. }
  45. def default_options
  46. {
  47. 'type' => 'message',
  48. 'app_id' => '',
  49. 'api_key' => '',
  50. 'sender_id' => '',
  51. 'phone' => '',
  52. 'text' => '{{title}}'
  53. }
  54. end
  55. def validate_options
  56. %w(app_id api_key sender_id type).each do |attr|
  57. errors.add(:base, "you need to specify a #{attr}") if options[attr].blank?
  58. end
  59. if options['type'].in?(MESSAGE_TYPES)
  60. required_attributes = TYPE_REQUIRED_ATTRIBUTES[options['type']]
  61. if required_attributes.any? { |attr| options[attr].blank? }
  62. errors.add(:base, "you need to specify a #{required_attributes.join(', ')}")
  63. end
  64. else
  65. errors.add(:base, 'you need to specify a valid message type')
  66. end
  67. unless options['group_id'].blank? ^ options['phone'].blank?
  68. errors.add(:base, 'you need to specify a phone or group_id')
  69. end
  70. end
  71. def working?
  72. received_event_without_error? && !recent_error_logs?
  73. end
  74. def receive(incoming_events)
  75. incoming_events.each do |event|
  76. send_message(event)
  77. end
  78. end
  79. def send_message(event)
  80. mo = interpolated(event)
  81. begin
  82. response = HTTParty.post(endpoint_for(mo['type']), body: payload_for(mo), headers: headers)
  83. error(response.body) if response.code != 201
  84. rescue HTTParty::Error => e
  85. error(e.message)
  86. end
  87. end
  88. private
  89. def headers
  90. {
  91. 'X-Beeper-Application-Id' => options['app_id'],
  92. 'X-Beeper-REST-API-Key' => options['api_key'],
  93. 'Content-Type' => 'application/json'
  94. }
  95. end
  96. def payload_for(mo)
  97. mo.slice(*TYPE_ATTRIBUTES[mo['type']], 'sender_id', 'phone', 'group_id').to_json
  98. end
  99. def endpoint_for(type)
  100. "#{BASE_URL}/#{type}s.json"
  101. end
  102. end
  103. end