feat: handle Channel errors (#11015)
This PR adds missing error handlers for the following channels and cases 1. WhatsApp - Generic Handlers for both Cloud and 360Dialog (Deprecated) 2. Instagram - Handler for a case where there is an HTTP error instead of an `:error` in the 200 response 3. Facebook - Errors from the two sentry issues ([Net::OpenTimeout](https://chatwoot-p3.sentry.io/issues/6164805227) & [JSON::ParserError](https://chatwoot-p3.sentry.io/issues/5903200786)) 4. SMS: Generic handlers for Bandwidth SMS #### Checklist - [x] Bandwidth SMS - [x] Whatsapp Cloud + 360 Dialog - [x] Twilio SMS - [x] Line - [x] Telegram - [x] Instagram - [x] Facebook - [x] GMail - [x] 365 Mail - [x] SMTP Mail --------- Co-authored-by: Muhsin Keloth <muhsinkeramam@gmail.com>
This commit is contained in:
@@ -26,7 +26,7 @@ const { t } = useI18n();
|
||||
/>
|
||||
</div>
|
||||
<div
|
||||
class="absolute bg-n-alpha-3 px-4 py-3 border rounded-xl border-n-strong text-n-slate-12 bottom-6 w-52 text-xs backdrop-blur-[100px] shadow-[0px_0px_24px_0px_rgba(0,0,0,0.12)] opacity-0 invisible group-hover:opacity-100 group-hover:visible transition-all"
|
||||
class="absolute bg-n-alpha-3 px-4 py-3 border rounded-xl border-n-strong text-n-slate-12 bottom-6 w-52 text-xs backdrop-blur-[100px] shadow-[0px_0px_24px_0px_rgba(0,0,0,0.12)] opacity-0 invisible group-hover:opacity-100 group-hover:visible transition-all break-all"
|
||||
:class="{
|
||||
'ltr:left-0 rtl:right-0': orientation === ORIENTATION.LEFT,
|
||||
'ltr:right-0 rtl:left-0': orientation === ORIENTATION.RIGHT,
|
||||
|
||||
@@ -102,6 +102,7 @@ const statusToShow = computed(() => {
|
||||
if (isRead.value) return MESSAGE_STATUS.READ;
|
||||
if (isDelivered.value) return MESSAGE_STATUS.DELIVERED;
|
||||
if (isSent.value) return MESSAGE_STATUS.SENT;
|
||||
if (status.value === MESSAGE_STATUS.FAILED) return MESSAGE_STATUS.FAILED;
|
||||
|
||||
return MESSAGE_STATUS.PROGRESS;
|
||||
});
|
||||
|
||||
@@ -37,7 +37,7 @@ class Channel::Sms < ApplicationRecord
|
||||
body = message_body(contact_number, message.content)
|
||||
body['media'] = message.attachments.map(&:download_url) if message.attachments.present?
|
||||
|
||||
send_to_bandwidth(body)
|
||||
send_to_bandwidth(body, message)
|
||||
end
|
||||
|
||||
def send_text_message(contact_number, message_content)
|
||||
@@ -56,7 +56,7 @@ class Channel::Sms < ApplicationRecord
|
||||
}
|
||||
end
|
||||
|
||||
def send_to_bandwidth(body)
|
||||
def send_to_bandwidth(body, message = nil)
|
||||
response = HTTParty.post(
|
||||
"#{api_base_path}/users/#{provider_config['account_id']}/messages",
|
||||
basic_auth: bandwidth_auth,
|
||||
@@ -64,7 +64,22 @@ class Channel::Sms < ApplicationRecord
|
||||
body: body.to_json
|
||||
)
|
||||
|
||||
response.success? ? response.parsed_response['id'] : nil
|
||||
if response.success?
|
||||
response.parsed_response['id']
|
||||
else
|
||||
handle_error(response, message)
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
def handle_error(response, message)
|
||||
Rails.logger.error("[#{account_id}] Error sending SMS: #{response.parsed_response['description']}")
|
||||
return if message.blank?
|
||||
|
||||
# https://dev.bandwidth.com/apis/messaging-apis/messaging/#tag/Messages/operation/createMessage
|
||||
message.external_error = response.parsed_response['description']
|
||||
message.status = :failed
|
||||
message.save!
|
||||
end
|
||||
|
||||
def bandwidth_auth
|
||||
|
||||
@@ -20,15 +20,30 @@ class Facebook::SendOnFacebookService < Base::SendOnChannelService
|
||||
end
|
||||
|
||||
def send_message_to_facebook(delivery_params)
|
||||
result = Facebook::Messenger::Bot.deliver(delivery_params, page_id: channel.page_id)
|
||||
parsed_result = JSON.parse(result)
|
||||
parsed_result = deliver_message(delivery_params)
|
||||
return if parsed_result.nil?
|
||||
|
||||
if parsed_result['error'].present?
|
||||
message.update!(status: :failed, external_error: external_error(parsed_result))
|
||||
Rails.logger.info "Facebook::SendOnFacebookService: Error sending message to Facebook : Page - #{channel.page_id} : #{result}"
|
||||
Rails.logger.info "Facebook::SendOnFacebookService: Error sending message to Facebook : Page - #{channel.page_id} : #{parsed_result}"
|
||||
end
|
||||
|
||||
message.update!(source_id: parsed_result['message_id']) if parsed_result['message_id'].present?
|
||||
end
|
||||
|
||||
def deliver_message(delivery_params)
|
||||
result = Facebook::Messenger::Bot.deliver(delivery_params, page_id: channel.page_id)
|
||||
JSON.parse(result)
|
||||
rescue JSON::ParserError
|
||||
message.update!(status: :failed, external_error: 'Facebook was unable to process this request')
|
||||
Rails.logger.error "Facebook::SendOnFacebookService: Error parsing JSON response from Facebook : Page - #{channel.page_id} : #{result}"
|
||||
nil
|
||||
rescue Net::OpenTimeout
|
||||
message.update!(status: :failed, external_error: 'Request timed out, please try again later')
|
||||
Rails.logger.error "Facebook::SendOnFacebookService: Timeout error sending message to Facebook : Page - #{channel.page_id}"
|
||||
nil
|
||||
end
|
||||
|
||||
def fb_text_message_params
|
||||
{
|
||||
recipient: { id: contact.get_source_id(inbox.id) },
|
||||
|
||||
@@ -70,22 +70,28 @@ class Instagram::SendOnInstagramService < Base::SendOnChannelService
|
||||
query: query
|
||||
)
|
||||
|
||||
if response[:error].present?
|
||||
Rails.logger.error("Instagram response: #{response['error']} : #{message_content}")
|
||||
message.status = :failed
|
||||
message.external_error = external_error(response)
|
||||
handle_response(response, message_content)
|
||||
end
|
||||
|
||||
def handle_response(response, message_content)
|
||||
parsed_response = response.parsed_response
|
||||
if response.success? && parsed_response['error'].blank?
|
||||
message.update!(source_id: parsed_response['message_id'])
|
||||
|
||||
parsed_response
|
||||
else
|
||||
external_error = external_error(parsed_response)
|
||||
Rails.logger.error("Instagram response: #{external_error} : #{message_content}")
|
||||
message.update!(status: :failed, external_error: external_error)
|
||||
|
||||
nil
|
||||
end
|
||||
|
||||
message.source_id = response['message_id'] if response['message_id'].present?
|
||||
message.save!
|
||||
|
||||
response
|
||||
end
|
||||
|
||||
def external_error(response)
|
||||
# https://developers.facebook.com/docs/instagram-api/reference/error-codes/
|
||||
error_message = response[:error][:message]
|
||||
error_code = response[:error][:code]
|
||||
error_message = response.dig('error', 'message')
|
||||
error_code = response.dig('error', 'code')
|
||||
|
||||
"#{error_code} - #{error_message}"
|
||||
end
|
||||
|
||||
@@ -27,6 +27,33 @@ class Whatsapp::Providers::BaseService
|
||||
raise 'Overwrite this method in child class'
|
||||
end
|
||||
|
||||
def error_message
|
||||
raise 'Overwrite this method in child class'
|
||||
end
|
||||
|
||||
def process_response(response)
|
||||
parsed_response = response.parsed_response
|
||||
if response.success? && parsed_response['error'].blank?
|
||||
parsed_response['messages'].first['id']
|
||||
else
|
||||
handle_error(response)
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
def handle_error(response)
|
||||
Rails.logger.error response.body
|
||||
return if @message.blank?
|
||||
|
||||
# https://developers.facebook.com/docs/whatsapp/cloud-api/support/error-codes/#sample-response
|
||||
error_message = error_message(response)
|
||||
return if error_message.blank?
|
||||
|
||||
@message.external_error = error_message
|
||||
@message.status = :failed
|
||||
@message.save!
|
||||
end
|
||||
|
||||
def create_buttons(items)
|
||||
buttons = []
|
||||
items.each do |item|
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
class Whatsapp::Providers::Whatsapp360DialogService < Whatsapp::Providers::BaseService
|
||||
def send_message(phone_number, message)
|
||||
@message = message
|
||||
if message.attachments.present?
|
||||
send_attachment_message(phone_number, message)
|
||||
elsif message.content_type == 'input_select'
|
||||
@@ -78,6 +79,7 @@ class Whatsapp::Providers::Whatsapp360DialogService < Whatsapp::Providers::BaseS
|
||||
}
|
||||
type_content['caption'] = message.content unless %w[audio sticker].include?(type)
|
||||
type_content['filename'] = attachment.file.filename if type == 'document'
|
||||
|
||||
response = HTTParty.post(
|
||||
"#{api_base_path}/messages",
|
||||
headers: api_headers,
|
||||
@@ -91,13 +93,9 @@ class Whatsapp::Providers::Whatsapp360DialogService < Whatsapp::Providers::BaseS
|
||||
process_response(response)
|
||||
end
|
||||
|
||||
def process_response(response)
|
||||
if response.success?
|
||||
response['messages'].first['id']
|
||||
else
|
||||
Rails.logger.error response.body
|
||||
nil
|
||||
end
|
||||
def error_message(response)
|
||||
# {"meta": {"success": false, "http_code": 400, "developer_message": "errro-message", "360dialog_trace_id": "someid"}}
|
||||
response.parsed_response.dig('meta', 'developer_message')
|
||||
end
|
||||
|
||||
def template_body_parameters(template_info)
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
class Whatsapp::Providers::WhatsappCloudService < Whatsapp::Providers::BaseService
|
||||
def send_message(phone_number, message)
|
||||
@message = message
|
||||
|
||||
if message.attachments.present?
|
||||
send_attachment_message(phone_number, message)
|
||||
elsif message.content_type == 'input_select'
|
||||
@@ -111,13 +113,9 @@ class Whatsapp::Providers::WhatsappCloudService < Whatsapp::Providers::BaseServi
|
||||
process_response(response)
|
||||
end
|
||||
|
||||
def process_response(response)
|
||||
if response.success?
|
||||
response['messages'].first['id']
|
||||
else
|
||||
Rails.logger.error response.body
|
||||
nil
|
||||
end
|
||||
def error_message(response)
|
||||
# https://developers.facebook.com/docs/whatsapp/cloud-api/support/error-codes/#sample-response
|
||||
response.parsed_response&.dig('error', 'message')
|
||||
end
|
||||
|
||||
def template_body_parameters(template_info)
|
||||
|
||||
@@ -117,5 +117,54 @@ describe Facebook::SendOnFacebookService do
|
||||
expect(message.reload.status).to eq('failed')
|
||||
end
|
||||
end
|
||||
|
||||
context 'when deliver_message fails' do
|
||||
let(:message) { create(:message, message_type: 'outgoing', inbox: facebook_inbox, account: account, conversation: conversation) }
|
||||
|
||||
it 'handles JSON parse errors' do
|
||||
allow(bot).to receive(:deliver).and_return('invalid_json')
|
||||
described_class.new(message: message).perform
|
||||
|
||||
expect(message.reload.status).to eq('failed')
|
||||
expect(message.external_error).to eq('Facebook was unable to process this request')
|
||||
end
|
||||
|
||||
it 'handles timeout errors' do
|
||||
allow(bot).to receive(:deliver).and_raise(Net::OpenTimeout)
|
||||
described_class.new(message: message).perform
|
||||
|
||||
expect(message.reload.status).to eq('failed')
|
||||
expect(message.external_error).to eq('Request timed out, please try again later')
|
||||
end
|
||||
|
||||
it 'handles facebook error with code' do
|
||||
error_response = {
|
||||
error: {
|
||||
message: 'Invalid OAuth access token.',
|
||||
type: 'OAuthException',
|
||||
code: 190,
|
||||
fbtrace_id: 'BLBz/WZt8dN'
|
||||
}
|
||||
}.to_json
|
||||
allow(bot).to receive(:deliver).and_return(error_response)
|
||||
|
||||
described_class.new(message: message).perform
|
||||
|
||||
expect(message.reload.status).to eq('failed')
|
||||
expect(message.external_error).to eq('190 - Invalid OAuth access token.')
|
||||
end
|
||||
|
||||
it 'handles successful delivery with message_id' do
|
||||
success_response = {
|
||||
message_id: 'mid.1456970487936:c34767dfe57ee6e339'
|
||||
}.to_json
|
||||
allow(bot).to receive(:deliver).and_return(success_response)
|
||||
|
||||
described_class.new(message: message).perform
|
||||
|
||||
expect(message.reload.source_id).to eq('mid.1456970487936:c34767dfe57ee6e339')
|
||||
expect(message.status).not_to eq('failed')
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -4,7 +4,7 @@ describe Instagram::SendOnInstagramService do
|
||||
subject(:send_reply_service) { described_class.new(message: message) }
|
||||
|
||||
before do
|
||||
stub_request(:post, /graph.facebook.com/)
|
||||
stub_request(:post, /graph\.facebook\.com/)
|
||||
create(:message, message_type: :incoming, inbox: instagram_inbox, account: account, conversation: conversation)
|
||||
end
|
||||
|
||||
@@ -15,16 +15,49 @@ describe Instagram::SendOnInstagramService do
|
||||
let(:contact_inbox) { create(:contact_inbox, contact: contact, inbox: instagram_inbox) }
|
||||
let(:conversation) { create(:conversation, contact: contact, inbox: instagram_inbox, contact_inbox: contact_inbox) }
|
||||
let(:response) { double }
|
||||
let(:mock_response) do
|
||||
instance_double(
|
||||
HTTParty::Response,
|
||||
:success? => true,
|
||||
:body => { message_id: 'anyrandommessageid1234567890' }.to_json,
|
||||
:parsed_response => { 'message_id' => 'anyrandommessageid1234567890' }
|
||||
)
|
||||
end
|
||||
|
||||
let(:error_body) do
|
||||
{
|
||||
'error' => {
|
||||
'message' => 'The Instagram account is restricted.',
|
||||
'type' => 'OAuthException',
|
||||
'code' => 400,
|
||||
'fbtrace_id' => 'anyrandomfbtraceid1234567890'
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
let(:error_response) do
|
||||
instance_double(
|
||||
HTTParty::Response,
|
||||
:success? => false,
|
||||
:body => error_body.to_json,
|
||||
:parsed_response => error_body
|
||||
)
|
||||
end
|
||||
|
||||
let(:response_with_error) do
|
||||
instance_double(
|
||||
HTTParty::Response,
|
||||
:success? => true,
|
||||
:body => error_body.to_json,
|
||||
:parsed_response => error_body
|
||||
)
|
||||
end
|
||||
|
||||
describe '#perform' do
|
||||
context 'with reply' do
|
||||
before do
|
||||
allow(Facebook::Messenger::Configuration::AppSecretProofCalculator).to receive(:call).and_return('app_secret_key', 'access_token')
|
||||
allow(HTTParty).to receive(:post).and_return(
|
||||
{
|
||||
'message_id': 'anyrandommessageid1234567890'
|
||||
}
|
||||
)
|
||||
allow(HTTParty).to receive(:post).and_return(mock_response)
|
||||
end
|
||||
|
||||
context 'without message_tag HUMAN_AGENT' do
|
||||
@@ -35,22 +68,8 @@ describe Instagram::SendOnInstagramService do
|
||||
it 'if message is sent from chatwoot and is outgoing' do
|
||||
message = create(:message, message_type: 'outgoing', inbox: instagram_inbox, account: account, conversation: conversation)
|
||||
|
||||
allow(HTTParty).to receive(:post).with(
|
||||
{
|
||||
recipient: { id: contact.get_source_id(instagram_inbox.id) },
|
||||
message: {
|
||||
text: message.content
|
||||
}
|
||||
}
|
||||
).and_return(
|
||||
{
|
||||
'message_id': 'anyrandommessageid1234567890'
|
||||
}
|
||||
)
|
||||
|
||||
response = described_class.new(message: message).perform
|
||||
|
||||
expect(response).to eq({ message_id: 'anyrandommessageid1234567890' })
|
||||
expect(response['message_id']).to eq('anyrandommessageid1234567890')
|
||||
end
|
||||
|
||||
it 'if message is sent from chatwoot and is outgoing with multiple attachments' do
|
||||
@@ -78,22 +97,13 @@ describe Instagram::SendOnInstagramService do
|
||||
message.save!
|
||||
response = described_class.new(message: message).perform
|
||||
|
||||
expect(response).to eq({ message_id: 'anyrandommessageid1234567890' })
|
||||
expect(response['message_id']).to eq('anyrandommessageid1234567890')
|
||||
end
|
||||
|
||||
it 'if message sent from chatwoot is failed' do
|
||||
message = create(:message, message_type: 'outgoing', inbox: instagram_inbox, account: account, conversation: conversation)
|
||||
|
||||
allow(HTTParty).to receive(:post).and_return(
|
||||
{
|
||||
'error': {
|
||||
'message': 'The Instagram account is restricted.',
|
||||
'type': 'OAuthException',
|
||||
'code': 400,
|
||||
'fbtrace_id': 'anyrandomfbtraceid1234567890'
|
||||
}
|
||||
}
|
||||
)
|
||||
allow(HTTParty).to receive(:post).and_return(response_with_error)
|
||||
described_class.new(message: message).perform
|
||||
expect(HTTParty).to have_received(:post)
|
||||
expect(message.reload.status).to eq('failed')
|
||||
@@ -129,5 +139,39 @@ describe Instagram::SendOnInstagramService do
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'when handling errors' do
|
||||
before do
|
||||
allow(Facebook::Messenger::Configuration::AppSecretProofCalculator).to receive(:call).and_return('app_secret_key', 'access_token')
|
||||
end
|
||||
|
||||
it 'handles HTTP errors' do
|
||||
message = create(:message, message_type: 'outgoing', inbox: instagram_inbox, account: account, conversation: conversation)
|
||||
allow(HTTParty).to receive(:post).and_return(error_response)
|
||||
|
||||
described_class.new(message: message).perform
|
||||
|
||||
expect(message.reload.status).to eq('failed')
|
||||
expect(message.reload.external_error).to eq('400 - The Instagram account is restricted.')
|
||||
end
|
||||
|
||||
it 'handles response errors' do
|
||||
message = create(:message, message_type: 'outgoing', inbox: instagram_inbox, account: account, conversation: conversation)
|
||||
|
||||
error_response = instance_double(
|
||||
HTTParty::Response,
|
||||
success?: true,
|
||||
body: { 'error' => { 'message' => 'Invalid message format', 'code' => 100 } }.to_json,
|
||||
parsed_response: { 'error' => { 'message' => 'Invalid message format', 'code' => 100 } }
|
||||
)
|
||||
|
||||
allow(HTTParty).to receive(:post).and_return(error_response)
|
||||
|
||||
described_class.new(message: message).perform
|
||||
|
||||
expect(message.reload.status).to eq('failed')
|
||||
expect(message.reload.external_error).to eq('100 - Invalid message format')
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -263,4 +263,56 @@ describe Whatsapp::Providers::WhatsappCloudService do
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#handle_error' do
|
||||
let(:error_message) { 'Invalid message format' }
|
||||
let(:error_response) do
|
||||
{
|
||||
'error' => {
|
||||
'message' => error_message,
|
||||
'code' => 100
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
let(:error_response_object) do
|
||||
instance_double(
|
||||
HTTParty::Response,
|
||||
body: error_response.to_json,
|
||||
parsed_response: error_response
|
||||
)
|
||||
end
|
||||
|
||||
before do
|
||||
allow(Rails.logger).to receive(:error)
|
||||
end
|
||||
|
||||
context 'when there is a message' do
|
||||
it 'logs error and updates message status' do
|
||||
service.instance_variable_set(:@message, message)
|
||||
service.send(:handle_error, error_response_object)
|
||||
|
||||
expect(message.reload.status).to eq('failed')
|
||||
expect(message.reload.external_error).to eq(error_message)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when error message is blank' do
|
||||
let(:error_response_object) do
|
||||
instance_double(
|
||||
HTTParty::Response,
|
||||
body: '{}',
|
||||
parsed_response: {}
|
||||
)
|
||||
end
|
||||
|
||||
it 'logs error but does not update message' do
|
||||
service.instance_variable_set(:@message, message)
|
||||
service.send(:handle_error, error_response_object)
|
||||
|
||||
expect(message.reload.status).not_to eq('failed')
|
||||
expect(message.reload.external_error).to be_nil
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -12,13 +12,30 @@ describe Whatsapp::SendOnWhatsappService do
|
||||
describe '#perform' do
|
||||
before do
|
||||
stub_request(:post, 'https://waba.360dialog.io/v1/configs/webhook')
|
||||
stub_request(:post, 'https://waba.360dialog.io/v1/messages')
|
||||
end
|
||||
|
||||
context 'when a valid message' do
|
||||
let(:whatsapp_request) { double }
|
||||
let(:whatsapp_request) { instance_double(HTTParty::Response) }
|
||||
let!(:whatsapp_channel) { create(:channel_whatsapp, sync_templates: false) }
|
||||
let!(:contact_inbox) { create(:contact_inbox, inbox: whatsapp_channel.inbox, source_id: '123456789') }
|
||||
let!(:conversation) { create(:conversation, contact_inbox: contact_inbox, inbox: whatsapp_channel.inbox) }
|
||||
let(:api_key) { 'test_key' }
|
||||
let(:headers) { { 'D360-API-KEY' => api_key, 'Content-Type' => 'application/json' } }
|
||||
let(:template_body) do
|
||||
{
|
||||
to: '123456789',
|
||||
template: {
|
||||
name: 'sample_shipping_confirmation',
|
||||
namespace: '23423423_2342423_324234234_2343224',
|
||||
language: { 'policy': 'deterministic', 'code': 'en_US' },
|
||||
components: [{ 'type': 'body', 'parameters': [{ 'type': 'text', 'text': '3' }] }]
|
||||
},
|
||||
type: 'template'
|
||||
}
|
||||
end
|
||||
|
||||
let(:success_response) { { 'messages' => [{ 'id' => '123456789' }] }.to_json }
|
||||
|
||||
it 'calls channel.send_message when with in 24 hour limit' do
|
||||
# to handle the case of 24 hour window limit.
|
||||
@@ -26,14 +43,14 @@ describe Whatsapp::SendOnWhatsappService do
|
||||
conversation: conversation)
|
||||
message = create(:message, message_type: :outgoing, content: 'test',
|
||||
conversation: conversation)
|
||||
allow(HTTParty).to receive(:post).and_return(whatsapp_request)
|
||||
allow(whatsapp_request).to receive(:success?).and_return(true)
|
||||
allow(whatsapp_request).to receive(:[]).with('messages').and_return([{ 'id' => '123456789' }])
|
||||
expect(HTTParty).to receive(:post).with(
|
||||
'https://waba.360dialog.io/v1/messages',
|
||||
headers: { 'D360-API-KEY' => 'test_key', 'Content-Type' => 'application/json' },
|
||||
body: { 'to' => '123456789', 'text' => { 'body' => 'test' }, 'type' => 'text' }.to_json
|
||||
)
|
||||
|
||||
stub_request(:post, 'https://waba.360dialog.io/v1/messages')
|
||||
.with(
|
||||
headers: headers,
|
||||
body: { 'to' => '123456789', 'text' => { 'body' => 'test' }, 'type' => 'text' }.to_json
|
||||
)
|
||||
.to_return(status: 200, body: success_response, headers: { 'content-type' => 'application/json' })
|
||||
|
||||
described_class.new(message: message).perform
|
||||
expect(message.reload.source_id).to eq('123456789')
|
||||
end
|
||||
@@ -41,23 +58,13 @@ describe Whatsapp::SendOnWhatsappService do
|
||||
it 'calls channel.send_template when after 24 hour limit' do
|
||||
message = create(:message, message_type: :outgoing, content: 'Your package has been shipped. It will be delivered in 3 business days.',
|
||||
conversation: conversation)
|
||||
allow(HTTParty).to receive(:post).and_return(whatsapp_request)
|
||||
allow(whatsapp_request).to receive(:success?).and_return(true)
|
||||
allow(whatsapp_request).to receive(:[]).with('messages').and_return([{ 'id' => '123456789' }])
|
||||
expect(HTTParty).to receive(:post).with(
|
||||
'https://waba.360dialog.io/v1/messages',
|
||||
headers: { 'D360-API-KEY' => 'test_key', 'Content-Type' => 'application/json' },
|
||||
body: {
|
||||
to: '123456789',
|
||||
template: {
|
||||
name: 'sample_shipping_confirmation',
|
||||
namespace: '23423423_2342423_324234234_2343224',
|
||||
language: { 'policy': 'deterministic', 'code': 'en_US' },
|
||||
components: [{ 'type': 'body', 'parameters': [{ 'type': 'text', 'text': '3' }] }]
|
||||
},
|
||||
type: 'template'
|
||||
}.to_json
|
||||
)
|
||||
|
||||
stub_request(:post, 'https://waba.360dialog.io/v1/messages')
|
||||
.with(
|
||||
headers: headers,
|
||||
body: template_body.to_json
|
||||
).to_return(status: 200, body: success_response, headers: { 'content-type' => 'application/json' })
|
||||
|
||||
described_class.new(message: message).perform
|
||||
expect(message.reload.source_id).to eq('123456789')
|
||||
end
|
||||
@@ -65,23 +72,12 @@ describe Whatsapp::SendOnWhatsappService do
|
||||
it 'calls channel.send_template if template_params are present' do
|
||||
message = create(:message, additional_attributes: { template_params: template_params },
|
||||
content: 'Your package will be delivered in 3 business days.', conversation: conversation, message_type: :outgoing)
|
||||
allow(HTTParty).to receive(:post).and_return(whatsapp_request)
|
||||
allow(whatsapp_request).to receive(:success?).and_return(true)
|
||||
allow(whatsapp_request).to receive(:[]).with('messages').and_return([{ 'id' => '123456789' }])
|
||||
expect(HTTParty).to receive(:post).with(
|
||||
'https://waba.360dialog.io/v1/messages',
|
||||
headers: { 'D360-API-KEY' => 'test_key', 'Content-Type' => 'application/json' },
|
||||
body: {
|
||||
to: '123456789',
|
||||
template: {
|
||||
name: 'sample_shipping_confirmation',
|
||||
namespace: '23423423_2342423_324234234_2343224',
|
||||
language: { 'policy': 'deterministic', 'code': 'en_US' },
|
||||
components: [{ 'type': 'body', 'parameters': [{ 'type': 'text', 'text': '3' }] }]
|
||||
},
|
||||
type: 'template'
|
||||
}.to_json
|
||||
)
|
||||
stub_request(:post, 'https://waba.360dialog.io/v1/messages')
|
||||
.with(
|
||||
headers: headers,
|
||||
body: template_body.to_json
|
||||
).to_return(status: 200, body: success_response, headers: { 'content-type' => 'application/json' })
|
||||
|
||||
described_class.new(message: message).perform
|
||||
expect(message.reload.source_id).to eq('123456789')
|
||||
end
|
||||
@@ -93,23 +89,22 @@ describe Whatsapp::SendOnWhatsappService do
|
||||
content: 'عميلنا العزيز الرجاء الرد على هذه الرسالة بكلمة *نعم* للرد على إستفساركم من قبل خدمة العملاء.',
|
||||
conversation: conversation
|
||||
)
|
||||
allow(HTTParty).to receive(:post).and_return(whatsapp_request)
|
||||
allow(whatsapp_request).to receive(:success?).and_return(true)
|
||||
allow(whatsapp_request).to receive(:[]).with('messages').and_return([{ 'id' => '123456789' }])
|
||||
expect(HTTParty).to receive(:post).with(
|
||||
'https://waba.360dialog.io/v1/messages',
|
||||
headers: { 'D360-API-KEY' => 'test_key', 'Content-Type' => 'application/json' },
|
||||
body: {
|
||||
to: '123456789',
|
||||
template: {
|
||||
name: 'customer_yes_no',
|
||||
namespace: '2342384942_32423423_23423fdsdaf23',
|
||||
language: { 'policy': 'deterministic', 'code': 'ar' },
|
||||
components: [{ 'type': 'body', 'parameters': [] }]
|
||||
},
|
||||
type: 'template'
|
||||
}.to_json
|
||||
)
|
||||
|
||||
stub_request(:post, 'https://waba.360dialog.io/v1/messages')
|
||||
.with(
|
||||
headers: headers,
|
||||
body: {
|
||||
to: '123456789',
|
||||
template: {
|
||||
name: 'customer_yes_no',
|
||||
namespace: '2342384942_32423423_23423fdsdaf23',
|
||||
language: { 'policy': 'deterministic', 'code': 'ar' },
|
||||
components: [{ 'type': 'body', 'parameters': [] }]
|
||||
},
|
||||
type: 'template'
|
||||
}.to_json
|
||||
).to_return(status: 200, body: success_response, headers: { 'content-type' => 'application/json' })
|
||||
|
||||
described_class.new(message: message).perform
|
||||
expect(message.reload.source_id).to eq('123456789')
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user