diff --git a/app/adapters/whats_app_adapter/three_sixty_dialog_outbound.rb b/app/adapters/whats_app_adapter/three_sixty_dialog_outbound.rb index e906e2312..bb490958f 100644 --- a/app/adapters/whats_app_adapter/three_sixty_dialog_outbound.rb +++ b/app/adapters/whats_app_adapter/three_sixty_dialog_outbound.rb @@ -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) diff --git a/app/adapters/whats_app_adapter/three_sixty_dialog_outbound/file.rb b/app/adapters/whats_app_adapter/three_sixty_dialog_outbound/file.rb index 67a239b62..d85885066 100644 --- a/app/adapters/whats_app_adapter/three_sixty_dialog_outbound/file.rb +++ b/app/adapters/whats_app_adapter/three_sixty_dialog_outbound/file.rb @@ -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 @@ -26,7 +26,7 @@ def perform(message_id:, file_id:) private - attr_reader :recipient, :file_id + attr_reader :recipient, :file_id, :message def payload { @@ -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 diff --git a/app/adapters/whats_app_adapter/three_sixty_dialog_outbound/text.rb b/app/adapters/whats_app_adapter/three_sixty_dialog_outbound/text.rb index 536ad7b5a..58e6f641e 100644 --- a/app/adapters/whats_app_adapter/three_sixty_dialog_outbound/text.rb +++ b/app/adapters/whats_app_adapter/three_sixty_dialog_outbound/text.rb @@ -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' } @@ -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