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

@@ -69,6 +69,7 @@ class Account < ApplicationRecord
has_many :web_widgets, dependent: :destroy_async, class_name: '::Channel::WebWidget'
has_many :webhooks, dependent: :destroy_async
has_many :whatsapp_channels, dependent: :destroy_async, class_name: '::Channel::Whatsapp'
has_many :sms_channels, dependent: :destroy_async, class_name: '::Channel::Sms'
has_many :working_hours, dependent: :destroy_async
has_many :automation_rules, dependent: :destroy

View File

@@ -58,6 +58,7 @@ class Campaign < ApplicationRecord
return if completed?
Twilio::OneoffSmsCampaignService.new(campaign: self).perform if inbox.inbox_type == 'Twilio SMS'
Sms::OneoffSmsCampaignService.new(campaign: self).perform if inbox.inbox_type == 'Sms'
end
private
@@ -69,14 +70,14 @@ class Campaign < ApplicationRecord
def validate_campaign_inbox
return unless inbox
errors.add :inbox, 'Unsupported Inbox type' unless ['Website', 'Twilio SMS'].include? inbox.inbox_type
errors.add :inbox, 'Unsupported Inbox type' unless ['Website', 'Twilio SMS', 'Sms'].include? inbox.inbox_type
end
# TO-DO we clean up with better validations when campaigns evolve into more inboxes
def ensure_correct_campaign_attributes
return if inbox.blank?
if inbox.inbox_type == 'Twilio SMS'
if ['Twilio SMS', 'Sms'].include?(inbox.inbox_type)
self.campaign_type = 'one_off'
self.scheduled_at ||= Time.now.utc
else

81
app/models/channel/sms.rb Normal file
View File

@@ -0,0 +1,81 @@
# == Schema Information
#
# Table name: channel_sms
#
# id :bigint not null, primary key
# phone_number :string not null
# provider :string default("default")
# provider_config :jsonb
# created_at :datetime not null
# updated_at :datetime not null
# account_id :integer not null
#
# Indexes
#
# index_channel_sms_on_phone_number (phone_number) UNIQUE
#
class Channel::Sms < ApplicationRecord
include Channelable
self.table_name = 'channel_sms'
EDITABLE_ATTRS = [:phone_number, { provider_config: {} }].freeze
validates :phone_number, presence: true, uniqueness: true
# before_save :validate_provider_config
def name
'Sms'
end
# all this should happen in provider service . but hack mode on
def api_base_path
'https://messaging.bandwidth.com/api/v2'
end
# Extract later into provider Service
def send_message(phone_number, message)
if message.attachments.present?
send_attachment_message(phone_number, message)
else
send_text_message(phone_number, message.content)
end
end
def send_text_message(contact_number, message)
response = HTTParty.post(
"#{api_base_path}/users/#{provider_config['account_id']}/messages",
basic_auth: bandwidth_auth,
headers: { 'Content-Type' => 'application/json' },
body: {
'to' => contact_number,
'from' => phone_number,
'text' => message,
'applicationId' => provider_config['application_id']
}.to_json
)
response.success? ? response.parsed_response['id'] : nil
end
private
def send_attachment_message(phone_number, message)
# fix me
end
def bandwidth_auth
{ username: provider_config['api_key'], password: provider_config['api_secret'] }
end
# Extract later into provider Service
# let's revisit later
def validate_provider_config
response = HTTParty.post(
"#{api_base_path}/users/#{provider_config['account_id']}/messages",
basic_auth: bandwidth_auth,
headers: { 'Content-Type': 'application/json' }
)
errors.add(:provider_config, 'error setting up') unless response.success?
end
end

View File

@@ -149,6 +149,6 @@ class Channel::Whatsapp < ApplicationRecord
url: "#{ENV['FRONTEND_URL']}/webhooks/whatsapp/#{phone_number}"
}.to_json
)
errors.add(:bot_token, 'error setting up the webook') unless response.success?
errors.add(:provider_config, 'error setting up the webook') unless response.success?
end
end

View File

@@ -107,6 +107,8 @@ class Inbox < ApplicationRecord
case channel_type
when 'Channel::TwilioSms'
"#{ENV['FRONTEND_URL']}/twilio/callback"
when 'Channel::Sms'
"#{ENV['FRONTEND_URL']}/webhooks/sms/#{channel.phone_number.delete_prefix('+')}"
when 'Channel::Line'
"#{ENV['FRONTEND_URL']}/webhooks/line/#{channel.line_channel_id}"
end