Skip to content

Commit

Permalink
chore: Provider APIs for SMS Channel - Bandwidth (#3889)
Browse files Browse the repository at this point in the history
fixes: #3888
  • Loading branch information
sojan-official committed Feb 3, 2022
1 parent fba7f40 commit cf10f3d
Show file tree
Hide file tree
Showing 40 changed files with 879 additions and 51 deletions.
20 changes: 13 additions & 7 deletions app/builders/contact_inbox_builder.rb
Expand Up @@ -4,7 +4,7 @@ class ContactInboxBuilder
def perform
@contact = Contact.find(contact_id)
@inbox = @contact.account.inboxes.find(inbox_id)
return unless ['Channel::TwilioSms', 'Channel::Email', 'Channel::Api', 'Channel::Whatsapp'].include? @inbox.channel_type
return unless ['Channel::TwilioSms', 'Channel::Sms', 'Channel::Email', 'Channel::Api', 'Channel::Whatsapp'].include? @inbox.channel_type

source_id = @source_id || generate_source_id
create_contact_inbox(source_id) if source_id.present?
Expand All @@ -13,12 +13,18 @@ def perform
private

def generate_source_id
return twilio_source_id if @inbox.channel_type == 'Channel::TwilioSms'
return wa_source_id if @inbox.channel_type == 'Channel::Whatsapp'
return @contact.email if @inbox.channel_type == 'Channel::Email'
return SecureRandom.uuid if @inbox.channel_type == 'Channel::Api'

nil
case @inbox.channel_type
when 'Channel::TwilioSms'
twilio_source_id
when 'Channel::Whatsapp'
wa_source_id
when 'Channel::Email'
@contact.email
when 'Channel::Sms'
@contact.phone_number
when 'Channel::Api'
SecureRandom.uuid
end
end

def wa_source_id
Expand Down
41 changes: 27 additions & 14 deletions app/controllers/api/v1/accounts/inboxes_controller.rb
Expand Up @@ -91,20 +91,9 @@ def inbox_name(channel)
end

def create_channel
case permitted_params[:channel][:type]
when 'web_widget'
Current.account.web_widgets.create!(permitted_params(Channel::WebWidget::EDITABLE_ATTRS)[:channel].except(:type))
when 'api'
Current.account.api_channels.create!(permitted_params(Channel::Api::EDITABLE_ATTRS)[:channel].except(:type))
when 'email'
Current.account.email_channels.create!(permitted_params(Channel::Email::EDITABLE_ATTRS)[:channel].except(:type))
when 'line'
Current.account.line_channels.create!(permitted_params(Channel::Line::EDITABLE_ATTRS)[:channel].except(:type))
when 'telegram'
Current.account.telegram_channels.create!(permitted_params(Channel::Telegram::EDITABLE_ATTRS)[:channel].except(:type))
when 'whatsapp'
Current.account.whatsapp_channels.create!(permitted_params(Channel::Whatsapp::EDITABLE_ATTRS)[:channel].except(:type))
end
return unless %w[web_widget api email line telegram whatsapp sms].include?(permitted_params[:channel][:type])

account_channels_method.create!(permitted_params(channel_type_from_params::EDITABLE_ATTRS)[:channel].except(:type))
end

def update_channel_feature_flags
Expand All @@ -123,6 +112,30 @@ def permitted_params(channel_attributes = [])
)
end

def channel_type_from_params
{
'web_widget' => Channel::WebWidget,
'api' => Channel::Api,
'email' => Channel::Email,
'line' => Channel::Line,
'telegram' => Channel::Telegram,
'whatsapp' => Channel::Whatsapp,
'sms' => Channel::Sms
}[permitted_params[:channel][:type]]
end

def account_channels_method
{
'web_widget' => Current.account.web_widgets,
'api' => Current.account.api_channels,
'email' => Current.account.email_channels,
'line' => Current.account.line_channels,
'telegram' => Current.account.telegram_channels,
'whatsapp' => Current.account.whatsapp_channels,
'sms' => Current.account.sms_channels
}[permitted_params[:channel][:type]]
end

def get_channel_attributes(channel_type)
if channel_type.constantize.const_defined?('EDITABLE_ATTRS')
channel_type.constantize::EDITABLE_ATTRS.presence
Expand Down
6 changes: 6 additions & 0 deletions app/controllers/webhooks/sms_controller.rb
@@ -0,0 +1,6 @@
class Webhooks::SmsController < ActionController::API
def process_payload
Webhooks::SmsEventsJob.perform_later(params['_json']&.first&.to_unsafe_hash)
head :ok
end
end
52 changes: 50 additions & 2 deletions app/javascript/dashboard/i18n/locale/en/inboxMgmt.json
Expand Up @@ -136,8 +136,56 @@
}
},
"SMS": {
"TITLE": "SMS Channel via Twilio",
"DESC": "Start supporting your customers via SMS with Twilio integration."
"TITLE": "SMS Channel",
"DESC": "Start supporting your customers via SMS.",
"PROVIDERS": {
"LABEL": "API Provider",
"TWILIO": "Twilio",
"BANDWIDTH": "Bandwidth"
},
"API": {
"ERROR_MESSAGE": "We were not able to save the SMS channel"
},
"BANDWIDTH": {
"ACCOUNT_ID": {
"LABEL": "Account ID",
"PLACEHOLDER": "Please enter your Bandwidth Account ID",
"ERROR": "This field is required"
},
"API_KEY": {
"LABEL": "API Key",
"PLACEHOLDER": "Please enter your Bandwith API Key",
"ERROR": "This field is required"
},
"API_SECRET": {
"LABEL": "API Secret",
"PLACEHOLDER": "Please enter your Bandwith API Secret",
"ERROR": "This field is required"
},
"APPLICATION_ID": {
"LABEL": "Application ID",
"PLACEHOLDER": "Please enter your Bandwidth Application ID",
"ERROR": "This field is required"
},
"INBOX_NAME": {
"LABEL": "Inbox Name",
"PLACEHOLDER": "Please enter a inbox name",
"ERROR": "This field is required"
},
"PHONE_NUMBER": {
"LABEL": "Phone number",
"PLACEHOLDER": "Please enter the phone number from which message will be sent.",
"ERROR": "Please enter a valid value. Phone number should start with `+` sign."
},
"SUBMIT_BUTTON": "Create Bandwidth Channel",
"API": {
"ERROR_MESSAGE": "We were not able to authenticate Bandwidth credentials, please try again"
},
"API_CALLBACK": {
"TITLE": "Callback URL",
"SUBTITLE": "You have to configure the message callback URL in Bandwidth with the URL mentioned here."
}
}
},
"WHATSAPP": {
"TITLE": "WhatsApp Channel",
Expand Down
Expand Up @@ -247,7 +247,7 @@ export default {
if (this.isOngoingType) {
return this.$store.getters['inboxes/getWebsiteInboxes'];
}
return this.$store.getters['inboxes/getTwilioSMSInboxes'];
return this.$store.getters['inboxes/getSMSInboxes'];
},
sendersAndBotList() {
return [
Expand Down
Expand Up @@ -171,7 +171,7 @@ export default {
if (this.isOngoingType) {
return this.$store.getters['inboxes/getWebsiteInboxes'];
}
return this.$store.getters['inboxes/getTwilioSMSInboxes'];
return this.$store.getters['inboxes/getSMSInboxes'];
},
pageTitle() {
return `${this.$t('CAMPAIGN.EDIT.TITLE')} - ${
Expand Down
Expand Up @@ -50,7 +50,7 @@ export default {
{ key: 'facebook', name: 'Messenger' },
{ key: 'twitter', name: 'Twitter' },
{ key: 'whatsapp', name: 'WhatsApp' },
{ key: 'sms', name: 'SMS via Twilio' },
{ key: 'sms', name: 'SMS' },
{ key: 'email', name: 'Email' },
{
key: 'api',
Expand Down
Expand Up @@ -29,6 +29,14 @@
>
</woot-code>
</div>
<div class="medium-6 small-offset-3">
<woot-code
v-if="isASmsInbox"
lang="html"
:script="currentInbox.callback_webhook_url"
>
</woot-code>
</div>
<div class="medium-6 small-offset-3">
<woot-code
v-if="isAEmailInbox"
Expand Down Expand Up @@ -86,13 +94,22 @@ export default {
isALineInbox() {
return this.currentInbox.channel_type === 'Channel::Line';
},
isASmsInbox() {
return this.currentInbox.channel_type === 'Channel::Sms';
},
message() {
if (this.isATwilioInbox) {
return `${this.$t('INBOX_MGMT.FINISH.MESSAGE')}. ${this.$t(
'INBOX_MGMT.ADD.TWILIO.API_CALLBACK.SUBTITLE'
)}`;
}
if (this.isASmsInbox) {
return `${this.$t('INBOX_MGMT.FINISH.MESSAGE')}. ${this.$t(
'INBOX_MGMT.ADD.SMS.BANDWIDTH.API_CALLBACK.SUBTITLE'
)}`;
}
if (this.isALineInbox) {
return `${this.$t('INBOX_MGMT.FINISH.MESSAGE')}. ${this.$t(
'INBOX_MGMT.ADD.LINE_CHANNEL.API_CALLBACK.SUBTITLE'
Expand All @@ -103,10 +120,11 @@ export default {
return this.$t('INBOX_MGMT.ADD.EMAIL_CHANNEL.FINISH_MESSAGE');
}
if (!this.currentInbox.web_widget_script) {
return this.$t('INBOX_MGMT.FINISH.MESSAGE');
if (this.currentInbox.web_widget_script) {
return this.$t('INBOX_MGMT.FINISH.WEBSITE_SUCCESS');
}
return this.$t('INBOX_MGMT.FINISH.WEBSITE_SUCCESS');
return this.$t('INBOX_MGMT.FINISH.MESSAGE');
},
},
};
Expand Down
Expand Up @@ -48,6 +48,9 @@
<span v-if="item.channel_type === 'Channel::Whatsapp'">
Whatsapp
</span>
<span v-if="item.channel_type === 'Channel::Sms'">
Sms
</span>
<span v-if="item.channel_type === 'Channel::Email'">
Email
</span>
Expand Down

0 comments on commit cf10f3d

Please sign in to comment.