beeper_agent.rb 3.6KB

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