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,21 @@
class Twilio::DeliveryStatusController < ApplicationController
def create
::Twilio::DeliveryStatusService.new(params: permitted_params).perform
head :no_content
end
private
def permitted_params
params.permit(
:AccountSid,
:From,
:MessageSid,
:MessagingServiceSid,
:MessageStatus,
:ErrorCode,
:ErrorMessage
)
end
end

View File

@@ -201,7 +201,7 @@ export default {
return !!this.sourceId;
}
if (this.isAWhatsAppChannel) {
if (this.isAWhatsAppChannel || this.isATwilioChannel) {
return this.sourceId && this.isSent;
}
return false;
@@ -211,7 +211,7 @@ export default {
return false;
}
if (this.isAWhatsAppChannel) {
if (this.isAWhatsAppChannel || this.isATwilioChannel) {
return this.sourceId && this.isDelivered;
}
@@ -227,7 +227,7 @@ export default {
return contactLastSeenAt >= this.createdAt;
}
if (this.isAWhatsAppChannel) {
if (this.isAWhatsAppChannel || this.isATwilioChannel) {
return this.sourceId && this.isRead;
}

View File

@@ -22,6 +22,7 @@
class Channel::TwilioSms < ApplicationRecord
include Channelable
include Rails.application.routes.url_helpers
self.table_name = 'channel_twilio_sms'
@@ -47,6 +48,7 @@ class Channel::TwilioSms < ApplicationRecord
def send_message(to:, body:, media_url: nil)
params = send_message_from.merge(to: to, body: body)
params[:media_url] = media_url if media_url.present?
params[:status_callback] = twilio_delivery_status_index_url
client.messages.create(**params)
end

View File

@@ -0,0 +1,55 @@
class Twilio::DeliveryStatusService
pattr_initialize [:params!]
# Reference: https://www.twilio.com/docs/messaging/api/message-resource#message-status-values
def perform
return if twilio_channel.blank?
return unless supported_status?
process_statuses if message.present?
end
private
def process_statuses
@message.status = params[:MessageStatus]
@message.external_error = external_error if error_occurred?
@message.save!
end
def supported_status?
%w[sent delivered read failed undelivered].include?(params[:MessageStatus])
end
def external_error
return nil unless error_occurred?
error_message = params[:ErrorMessage].presence
error_code = params[:ErrorCode]
if error_message.present?
"#{error_code} - #{error_message}"
elsif error_code.present?
I18n.t('conversations.messages.delivery_status.error_code', error_code: error_code)
end
end
def error_occurred?
params[:ErrorCode].present? && %w[failed undelivered].include?(params[:MessageStatus])
end
def twilio_channel
@twilio_channel ||= if params[:MessagingServiceSid].present?
::Channel::TwilioSms.find_by(messaging_service_sid: params[:MessagingServiceSid])
elsif params[:AccountSid].present? && params[:From].present?
::Channel::TwilioSms.find_by!(account_sid: params[:AccountSid], phone_number: params[:From])
end
end
def message
return unless params[:MessageSid]
@message ||= twilio_channel.inbox.messages.find_by(source_id: params[:MessageSid])
end
end