@@ -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?
|
||||
|
||||
|
||||
66
app/services/sms/incoming_message_service.rb
Normal file
66
app/services/sms/incoming_message_service.rb
Normal 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
|
||||
32
app/services/sms/oneoff_sms_campaign_service.rb
Normal file
32
app/services/sms/oneoff_sms_campaign_service.rb
Normal 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
|
||||
16
app/services/sms/send_on_sms_service.rb
Normal file
16
app/services/sms/send_on_sms_service.rb
Normal 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
|
||||
Reference in New Issue
Block a user