Skip to content

Commit

Permalink
Save external id of outbound message to record
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwr18 committed Oct 7, 2024
1 parent 39ed9e5 commit 8e1e3cd
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ def send_message(recipient, message)
WhatsAppAdapter::ThreeSixtyDialogOutbound::Text.perform_later(organization_id: message.organization.id,
payload: text_payload(
recipient, message.text
))
),
message_id: message.id)
else
files.each do |_file|
WhatsAppAdapter::ThreeSixtyDialog::UploadFileJob.perform_later(message_id: message.id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class File < ApplicationJob
queue_as :default

def perform(message_id:, file_id:)
message = Message.find(message_id)
@message = Message.find(message_id)
organization = Organization.find(message.organization.id)

@recipient = message.recipient
Expand All @@ -26,7 +26,7 @@ def perform(message_id:, file_id:)

private

attr_reader :recipient, :file_id
attr_reader :recipient, :file_id, :message

def payload
{
Expand All @@ -41,10 +41,11 @@ def payload
end

def handle_response(response)
case response.code.to_i
when 200
Rails.logger.debug 'Great!'
when 400..599
case response
when Net::HTTPSuccess
external_id = JSON.parse(response.body)['messages'].first['id']
message.update!(external_id: external_id)
when Net::HTTPClientError, Net::HTTPServerError
exception = WhatsAppAdapter::ThreeSixtyDialogError.new(error_code: response.code, message: response.body)
ErrorNotifier.report(exception)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ class ThreeSixtyDialogOutbound
class Text < ApplicationJob
queue_as :default

def perform(organization_id:, payload:)
def perform(organization_id:, payload:, message_id: nil)
organization = Organization.find(organization_id)
@message = Message.find(message_id) if message_id

url = URI.parse("#{ENV.fetch('THREE_SIXTY_DIALOG_WHATS_APP_REST_API_ENDPOINT', 'https://stoplight.io/mocks/360dialog/360dialog-partner-api/24588693')}/messages")
headers = { 'D360-API-KEY' => organization.three_sixty_dialog_client_api_key, 'Content-Type' => 'application/json' }
Expand All @@ -21,13 +22,16 @@ def perform(organization_id:, payload:)
ErrorNotifier.report(e)
end

attr_reader :message

private

def handle_response(response)
case response.code.to_i
when 201
Rails.logger.debug 'Great!'
when 400..599
case response
when Net::HTTPSuccess
external_id = JSON.parse(response.body)['messages'].first['id']
message&.update!(external_id: external_id)
when Net::HTTPClientError, Net::HTTPServerError
exception = WhatsAppAdapter::ThreeSixtyDialogError.new(error_code: response.code, message: response.body)
ErrorNotifier.report(exception)
end
Expand Down

0 comments on commit 8e1e3cd

Please sign in to comment.