feat: Bandwidth sms channel delivery reports (#8198)
Fixes: https://linear.app/chatwoot/issue/CW-2566/delivery-report-for-bandwidth-sms
This commit is contained in:
@@ -204,7 +204,8 @@ export default {
|
||||
if (
|
||||
this.isAWhatsAppChannel ||
|
||||
this.isATwilioChannel ||
|
||||
this.isAFacebookInbox
|
||||
this.isAFacebookInbox ||
|
||||
this.isASmsInbox
|
||||
) {
|
||||
return this.sourceId && this.isSent;
|
||||
}
|
||||
@@ -215,7 +216,11 @@ export default {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (this.isAWhatsAppChannel || this.isATwilioChannel) {
|
||||
if (
|
||||
this.isAWhatsAppChannel ||
|
||||
this.isATwilioChannel ||
|
||||
this.isASmsInbox
|
||||
) {
|
||||
return this.sourceId && this.isDelivered;
|
||||
}
|
||||
// We will consider messages as delivered for web widget inbox and API inbox if they are sent
|
||||
|
||||
@@ -1,13 +1,28 @@
|
||||
class Webhooks::SmsEventsJob < ApplicationJob
|
||||
queue_as :default
|
||||
|
||||
SUPPORTED_EVENTS = %w[message-received message-delivered message-failed].freeze
|
||||
|
||||
def perform(params = {})
|
||||
return unless params[:type] == 'message-received'
|
||||
return unless SUPPORTED_EVENTS.include?(params[:type])
|
||||
|
||||
channel = Channel::Sms.find_by(phone_number: params[:to])
|
||||
return unless channel
|
||||
|
||||
# TODO: pass to appropriate provider service from here
|
||||
Sms::IncomingMessageService.new(inbox: channel.inbox, params: params[:message].with_indifferent_access).perform
|
||||
process_event_params(channel, params)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def process_event_params(channel, params)
|
||||
if delivery_event?(params)
|
||||
Sms::DeliveryStatusService.new(channel: channel, params: params[:message].with_indifferent_access).perform
|
||||
else
|
||||
Sms::IncomingMessageService.new(inbox: channel.inbox, params: params[:message].with_indifferent_access).perform
|
||||
end
|
||||
end
|
||||
|
||||
def delivery_event?(params)
|
||||
params[:type] == 'message-delivered' || params[:type] == 'message-failed'
|
||||
end
|
||||
end
|
||||
|
||||
52
app/services/sms/delivery_status_service.rb
Normal file
52
app/services/sms/delivery_status_service.rb
Normal file
@@ -0,0 +1,52 @@
|
||||
class Sms::DeliveryStatusService
|
||||
pattr_initialize [:inbox!, :params!]
|
||||
|
||||
def perform
|
||||
return unless supported_status?
|
||||
|
||||
process_status if message.present?
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def process_status
|
||||
@message.status = status
|
||||
@message.external_error = external_error if error_occurred?
|
||||
@message.save!
|
||||
end
|
||||
|
||||
def supported_status?
|
||||
%w[message-delivered message-failed].include?(params[:type])
|
||||
end
|
||||
|
||||
# Relevant documentation:
|
||||
# https://dev.bandwidth.com/docs/mfa/webhooks/international/message-delivered
|
||||
# https://dev.bandwidth.com/docs/mfa/webhooks/international/message-failed
|
||||
def status
|
||||
type_mapping = {
|
||||
'message-delivered' => 'delivered',
|
||||
'message-failed' => 'failed'
|
||||
}
|
||||
|
||||
type_mapping[params[:type]]
|
||||
end
|
||||
|
||||
def external_error
|
||||
return nil unless error_occurred?
|
||||
|
||||
error_message = params[:description]
|
||||
error_code = params[:errorCode]
|
||||
|
||||
"#{error_code} - #{error_message}"
|
||||
end
|
||||
|
||||
def error_occurred?
|
||||
params[:errorCode] && params[:type] == 'message-failed'
|
||||
end
|
||||
|
||||
def message
|
||||
return unless params[:message][:id]
|
||||
|
||||
@message ||= inbox.messages.find_by(source_id: params[:message][:id])
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user