-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1645 from tactilenews/360dialog_spike
- Loading branch information
Showing
36 changed files
with
2,859 additions
and
337 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
app/adapters/whats_app_adapter/outbound/three_sixty_dialog_file.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# frozen_string_literal: true | ||
|
||
module WhatsAppAdapter | ||
class Outbound | ||
class ThreeSixtyDialogFile < ApplicationJob | ||
queue_as :default | ||
|
||
def perform(message_id:, file_id:) | ||
message = Message.find(message_id) | ||
@recipient = message.recipient | ||
@file_id = file_id | ||
|
||
url = URI.parse("#{Setting.three_sixty_dialog_whats_app_rest_api_endpoint}/messages") | ||
headers = { 'D360-API-KEY' => Setting.three_sixty_dialog_client_api_key, 'Content-Type' => 'application/json' } | ||
request = Net::HTTP::Post.new(url.to_s, headers) | ||
request.body = payload.to_json | ||
response = Net::HTTP.start(url.host, url.port, use_ssl: true) do |http| | ||
http.request(request) | ||
end | ||
handle_response(response) | ||
end | ||
|
||
private | ||
|
||
attr_reader :recipient, :file_id | ||
|
||
def payload | ||
{ | ||
recipient_type: 'individual', | ||
to: recipient.whats_app_phone_number.split('+').last, | ||
type: 'image', | ||
image: { | ||
id: file_id | ||
} | ||
} | ||
end | ||
|
||
def handle_response(response) | ||
case response.code.to_i | ||
when 200 | ||
Rails.logger.debug 'Great!' | ||
when 400..599 | ||
exception = WhatsAppAdapter::ThreeSixtyDialogError.new(error_code: response.code, message: response.body) | ||
ErrorNotifier.report(exception) | ||
end | ||
end | ||
end | ||
end | ||
end |
33 changes: 33 additions & 0 deletions
33
app/adapters/whats_app_adapter/outbound/three_sixty_dialog_text.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# frozen_string_literal: true | ||
|
||
module WhatsAppAdapter | ||
class Outbound | ||
class ThreeSixtyDialogText < ApplicationJob | ||
queue_as :default | ||
|
||
def perform(payload:) | ||
url = URI.parse("#{Setting.three_sixty_dialog_whats_app_rest_api_endpoint}/messages") | ||
headers = { 'D360-API-KEY' => Setting.three_sixty_dialog_client_api_key, 'Content-Type' => 'application/json' } | ||
request = Net::HTTP::Post.new(url.to_s, headers) | ||
|
||
request.body = payload.to_json | ||
response = Net::HTTP.start(url.host, url.port, use_ssl: true) do |http| | ||
http.request(request) | ||
end | ||
handle_response(response) | ||
end | ||
|
||
private | ||
|
||
def handle_response(response) | ||
case response.code.to_i | ||
when 201 | ||
Rails.logger.debug 'Great!' | ||
when 400..599 | ||
exception = WhatsAppAdapter::ThreeSixtyDialogError.new(error_code: response.code, message: response.body) | ||
ErrorNotifier.report(exception) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# frozen_string_literal: true | ||
|
||
module WhatsAppAdapter | ||
class ThreeSixtyDialogError < StandardError | ||
def initialize(error_code:, message:) | ||
super("Error occurred for WhatsApp with error code: #{error_code} with message: #{message}") | ||
end | ||
end | ||
end |
Oops, something went wrong.