feat: Add delivery status for Twilio Channel (#8082)

Co-authored-by: Pranav Raj S <pranav@chatwoot.com>
This commit is contained in:
Muhsin Keloth
2023-10-11 21:17:46 +05:30
committed by GitHub
parent 88de3359a5
commit 0bc20873f6
10 changed files with 224 additions and 9 deletions

View File

@@ -0,0 +1,18 @@
require 'rails_helper'
RSpec.describe 'Twilio::DeliveryStatusController', type: :request do
include Rails.application.routes.url_helpers
let(:twilio_service) { instance_double(Twilio::DeliveryStatusService) }
before do
allow(Twilio::DeliveryStatusService).to receive(:new).and_return(twilio_service)
allow(twilio_service).to receive(:perform)
end
describe 'POST /twilio/delivery' do
it 'calls incoming message service' do
post twilio_delivery_status_index_url, params: {}
expect(twilio_service).to have_received(:perform)
end
end
end

View File

@@ -68,7 +68,8 @@ RSpec.describe Channel::TwilioSms do
expect(twilio_messages).to receive(:create).with(
messaging_service_sid: channel.messaging_service_sid,
to: '+15555550111',
body: 'hello world'
body: 'hello world',
status_callback: 'http://localhost:3000/twilio/delivery_status'
).once
channel.send_message(to: '+15555550111', body: 'hello world')
@@ -81,7 +82,8 @@ RSpec.describe Channel::TwilioSms do
expect(twilio_messages).to receive(:create).with(
from: channel.phone_number,
to: '+15555550111',
body: 'hello world'
body: 'hello world',
status_callback: 'http://localhost:3000/twilio/delivery_status'
).once
channel.send_message(to: '+15555550111', body: 'hello world')
@@ -94,7 +96,8 @@ RSpec.describe Channel::TwilioSms do
messaging_service_sid: channel.messaging_service_sid,
to: '+15555550111',
body: 'hello world',
media_url: ['https://example.com/1.jpg']
media_url: ['https://example.com/1.jpg'],
status_callback: 'http://localhost:3000/twilio/delivery_status'
).once
channel.send_message(to: '+15555550111', body: 'hello world', media_url: ['https://example.com/1.jpg'])

View File

@@ -0,0 +1,110 @@
require 'rails_helper'
describe Twilio::DeliveryStatusService do
let!(:account) { create(:account) }
let!(:twilio_channel) do
create(:channel_twilio_sms, account: account, account_sid: 'ACxxx',
inbox: create(:inbox, account: account, greeting_enabled: false))
end
let!(:contact) { create(:contact, account: account, phone_number: '+12345') }
let(:contact_inbox) { create(:contact_inbox, source_id: '+12345', contact: contact, inbox: twilio_channel.inbox) }
let!(:conversation) { create(:conversation, contact: contact, inbox: twilio_channel.inbox, contact_inbox: contact_inbox) }
describe '#perform' do
context 'when message delivery status is fired' do
before do
create(:message, account: account, inbox: twilio_channel.inbox, conversation: conversation, status: :sent,
source_id: 'SMd560ac79e4a4d36b3ce59f1f50471986')
end
it 'updates the message if the status is delivered' do
params = {
SmsSid: 'SMxx',
From: '+12345',
AccountSid: 'ACxxx',
MessagingServiceSid: twilio_channel.messaging_service_sid,
MessageSid: conversation.messages.last.source_id,
MessageStatus: 'delivered'
}
described_class.new(params: params).perform
expect(conversation.reload.messages.last.status).to eq('delivered')
end
it 'updates the message if the status is read' do
params = {
SmsSid: 'SMxx',
From: '+12345',
AccountSid: 'ACxxx',
MessagingServiceSid: twilio_channel.messaging_service_sid,
MessageSid: conversation.messages.last.source_id,
MessageStatus: 'read'
}
described_class.new(params: params).perform
expect(conversation.reload.messages.last.status).to eq('read')
end
it 'does not update the message if the status is not a support status' do
params = {
SmsSid: 'SMxx',
From: '+12345',
AccountSid: 'ACxxx',
MessagingServiceSid: twilio_channel.messaging_service_sid,
MessageSid: conversation.messages.last.source_id,
MessageStatus: 'queued'
}
described_class.new(params: params).perform
expect(conversation.reload.messages.last.status).to eq('sent')
end
it 'updates message status to failed if message status is failed' do
params = {
SmsSid: 'SMxx',
From: '+12345',
AccountSid: 'ACxxx',
MessagingServiceSid: twilio_channel.messaging_service_sid,
MessageSid: conversation.messages.last.source_id,
MessageStatus: 'failed'
}
described_class.new(params: params).perform
expect(conversation.reload.messages.last.status).to eq('failed')
end
it 'updates message status to failed and updates the error message if message status is undelivered' do
params = {
SmsSid: 'SMxx',
From: '+12345',
AccountSid: 'ACxxx',
MessagingServiceSid: twilio_channel.messaging_service_sid,
MessageSid: conversation.messages.last.source_id,
MessageStatus: 'failed',
ErrorCode: '30008',
ErrorMessage: 'Unknown error'
}
described_class.new(params: params).perform
expect(conversation.reload.messages.last.status).to eq('failed')
expect(conversation.reload.messages.last.external_error).to eq('30008 - Unknown error')
end
it 'updates the error message if message status is undelivered and error message is not present' do
params = {
SmsSid: 'SMxx',
From: '+12345',
AccountSid: 'ACxxx',
MessagingServiceSid: twilio_channel.messaging_service_sid,
MessageSid: conversation.messages.last.source_id,
MessageStatus: 'failed',
ErrorCode: '30008'
}
described_class.new(params: params).perform
expect(conversation.reload.messages.last.status).to eq('failed')
expect(conversation.reload.messages.last.external_error).to eq('Error code: 30008')
end
end
end
end

View File

@@ -41,17 +41,20 @@ describe Twilio::OneoffSmsCampaignService do
expect(twilio_messages).to receive(:create).with(
body: campaign.message,
messaging_service_sid: twilio_sms.messaging_service_sid,
to: contact_with_label1.phone_number
to: contact_with_label1.phone_number,
status_callback: 'http://localhost:3000/twilio/delivery_status'
).once
expect(twilio_messages).to receive(:create).with(
body: campaign.message,
messaging_service_sid: twilio_sms.messaging_service_sid,
to: contact_with_label2.phone_number
to: contact_with_label2.phone_number,
status_callback: 'http://localhost:3000/twilio/delivery_status'
).once
expect(twilio_messages).to receive(:create).with(
body: campaign.message,
messaging_service_sid: twilio_sms.messaging_service_sid,
to: contact_with_both_labels.phone_number
to: contact_with_both_labels.phone_number,
status_callback: 'http://localhost:3000/twilio/delivery_status'
).once
sms_campaign_service.perform