chore: Provider APIs for SMS Channel - Bandwidth (#3889)

fixes: #3888
This commit is contained in:
Sojan Jose
2022-02-03 15:22:13 -08:00
committed by GitHub
parent fba7f40bee
commit cf10f3d03b
40 changed files with 879 additions and 51 deletions

View File

@@ -14,6 +14,8 @@ class Contacts::ContactableInboxesService
twilio_contactable_inbox(inbox)
when 'Channel::Whatsapp'
whatsapp_contactable_inbox(inbox)
when 'Channel::Sms'
sms_contactable_inbox(inbox)
when 'Channel::Email'
email_contactable_inbox(inbox)
when 'Channel::Api'
@@ -52,6 +54,12 @@ class Contacts::ContactableInboxesService
{ source_id: @contact.phone_number.delete('+'), inbox: inbox }
end
def sms_contactable_inbox(inbox)
return unless @contact.phone_number
{ source_id: @contact.phone_number, inbox: inbox }
end
def twilio_contactable_inbox(inbox)
return if @contact.phone_number.blank?

View File

@@ -0,0 +1,66 @@
class Sms::IncomingMessageService
include ::FileTypeHelper
pattr_initialize [:inbox!, :params!]
def perform
set_contact
set_conversation
@message = @conversation.messages.create(
content: params[:text],
account_id: @inbox.account_id,
inbox_id: @inbox.id,
message_type: :incoming,
sender: @contact,
source_id: params[:id]
)
end
private
def account
@account ||= @inbox.account
end
def phone_number
params[:from]
end
def formatted_phone_number
TelephoneNumber.parse(phone_number).international_number
end
def set_contact
contact_inbox = ::ContactBuilder.new(
source_id: params[:from],
inbox: @inbox,
contact_attributes: contact_attributes
).perform
@contact_inbox = contact_inbox
@contact = contact_inbox.contact
end
def conversation_params
{
account_id: @inbox.account_id,
inbox_id: @inbox.id,
contact_id: @contact.id,
contact_inbox_id: @contact_inbox.id
}
end
def set_conversation
@conversation = @contact_inbox.conversations.first
return if @conversation
@conversation = ::Conversation.create!(conversation_params)
end
def contact_attributes
{
name: formatted_phone_number,
phone_number: phone_number
}
end
end

View File

@@ -0,0 +1,32 @@
class Sms::OneoffSmsCampaignService
pattr_initialize [:campaign!]
def perform
raise "Invalid campaign #{campaign.id}" if campaign.inbox.inbox_type != 'Sms' || !campaign.one_off?
raise 'Completed Campaign' if campaign.completed?
# marks campaign completed so that other jobs won't pick it up
campaign.completed!
audience_label_ids = campaign.audience.select { |audience| audience['type'] == 'Label' }.pluck('id')
audience_labels = campaign.account.labels.where(id: audience_label_ids).pluck(:title)
process_audience(audience_labels)
end
private
delegate :inbox, to: :campaign
delegate :channel, to: :inbox
def process_audience(audience_labels)
campaign.account.contacts.tagged_with(audience_labels, any: true).each do |contact|
next if contact.phone_number.blank?
send_message(to: contact.phone_number, content: campaign.message)
end
end
def send_message(to:, content:)
channel.send_text_message(to, content)
end
end

View File

@@ -0,0 +1,16 @@
class Sms::SendOnSmsService < Base::SendOnChannelService
private
def channel_class
Channel::Sms
end
def perform_reply
send_on_sms
end
def send_on_sms
message_id = channel.send_message(message.conversation.contact_inbox.source_id, message)
message.update!(source_id: message_id) if message_id.present?
end
end