Skip to content

Commit

Permalink
Setup a ServiceMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
emorissettegregoire committed Aug 8, 2024
1 parent bd72f4f commit 646c2ae
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
16 changes: 10 additions & 6 deletions lib/stealth/services/bandwidth/event_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@ module Bandwidth

class EventHandler
def self.determine_event_type(request)
# WIP will handle calls and other events here
case request.params.dig(:_json)&.first&.dig(:type)
# Create a new instance of ServiceMessage with the request params and headers
service_message = Stealth::Services::Bandwidth::ServiceMessage.new(
params: request.params.dig(:_json)&.first,
headers: request.headers
).process

# Determine the event type and include the service_message in the response
case service_message.event_type
when 'message-received'
:text_message_receive
# when 'unsubscribe'
# :unsubscribe
{ type: :text_message_receive, service_message: service_message }
else
:unknown_event
{ type: :unknown_event }
end
end
end
Expand Down
34 changes: 34 additions & 0 deletions lib/stealth/services/bandwidth/service_message.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module Stealth
module Services
module Bandwidth

class ServiceMessage < Stealth::ServiceMessage
attr_reader :params, :headers

def initialize(params:, headers:)
super(service: 'bandwidth')
@params = params
@headers = headers
end

def process
self.sender_id = params.dig('message', 'from')
self.target_id = params.dig('to')
self.message = params.dig('message', 'text')
self.event_type = params.dig('type')
self.timestamp = params.dig('time')

# Handle attachments if any
params.dig('message', 'media')&.each do |attachment_url|
service_message.attachments << {
url: attachment_url
}
end

self
end
end

end
end
end

0 comments on commit 646c2ae

Please sign in to comment.