From 2396b59f114a745a7df9a46d57445aa3b45f1e12 Mon Sep 17 00:00:00 2001 From: Sojan Jose Date: Tue, 14 Sep 2021 22:44:53 +0530 Subject: [PATCH] chore: API fixes (#3014) - Minor API fixes - Configuration screen for LINE inbox --- .../api/v1/accounts/inboxes_controller.rb | 1 + .../dashboard/i18n/locale/en/inboxMgmt.json | 4 +++ .../dashboard/settings/inbox/AddAgents.vue | 2 +- .../dashboard/settings/inbox/FinishSetup.vue | 10 +++++-- .../routes/dashboard/settings/inbox/Index.vue | 3 ++ .../dashboard/settings/inbox/Settings.vue | 23 +++++++++++++-- app/javascript/shared/mixins/inboxMixin.js | 4 +++ app/models/inbox.rb | 5 ++-- app/services/line/incoming_message_service.rb | 8 +++++ app/views/api/v1/models/_inbox.json.jbuilder | 29 ++++++++++++++----- .../v1/accounts/inboxes_controller_spec.rb | 3 +- 11 files changed, 76 insertions(+), 16 deletions(-) diff --git a/app/controllers/api/v1/accounts/inboxes_controller.rb b/app/controllers/api/v1/accounts/inboxes_controller.rb index cb01fc380..df055923c 100644 --- a/app/controllers/api/v1/accounts/inboxes_controller.rb +++ b/app/controllers/api/v1/accounts/inboxes_controller.rb @@ -100,6 +100,7 @@ class Api::V1::Accounts::InboxesController < Api::V1::Accounts::BaseController end def update_channel_feature_flags + return unless @inbox.web_widget? return unless permitted_params(Channel::WebWidget::EDITABLE_ATTRS)[:channel].key? :selected_feature_flags @inbox.channel.selected_feature_flags = permitted_params(Channel::WebWidget::EDITABLE_ATTRS)[:channel][:selected_feature_flags] diff --git a/app/javascript/dashboard/i18n/locale/en/inboxMgmt.json b/app/javascript/dashboard/i18n/locale/en/inboxMgmt.json index 329c48d31..4cd9de91f 100644 --- a/app/javascript/dashboard/i18n/locale/en/inboxMgmt.json +++ b/app/javascript/dashboard/i18n/locale/en/inboxMgmt.json @@ -195,6 +195,10 @@ "SUBMIT_BUTTON": "Create LINE Channel", "API": { "ERROR_MESSAGE": "We were not able to save the LINE channel" + }, + "API_CALLBACK": { + "TITLE": "Callback URL", + "SUBTITLE": "You have to configure the webhook URL in LINE application with the URL mentioned here." } }, "TELEGRAM_CHANNEL": { diff --git a/app/javascript/dashboard/routes/dashboard/settings/inbox/AddAgents.vue b/app/javascript/dashboard/routes/dashboard/settings/inbox/AddAgents.vue index 3a8c93350..b42e5e9c3 100644 --- a/app/javascript/dashboard/routes/dashboard/settings/inbox/AddAgents.vue +++ b/app/javascript/dashboard/routes/dashboard/settings/inbox/AddAgents.vue @@ -88,7 +88,7 @@ export default { const selectedAgents = this.selectedAgents.map(x => x.id); try { - await InboxMembersAPI.create({ inboxId, agentList: selectedAgents }); + await InboxMembersAPI.update({ inboxId, agentList: selectedAgents }); router.replace({ name: 'settings_inbox_finish', params: { diff --git a/app/javascript/dashboard/routes/dashboard/settings/inbox/FinishSetup.vue b/app/javascript/dashboard/routes/dashboard/settings/inbox/FinishSetup.vue index ad2bab48d..95cbd7646 100644 --- a/app/javascript/dashboard/routes/dashboard/settings/inbox/FinishSetup.vue +++ b/app/javascript/dashboard/routes/dashboard/settings/inbox/FinishSetup.vue @@ -17,7 +17,7 @@ @@ -25,7 +25,7 @@ @@ -93,6 +93,12 @@ export default { )}`; } + if (this.isALineInbox) { + return `${this.$t('INBOX_MGMT.FINISH.MESSAGE')}. ${this.$t( + 'INBOX_MGMT.ADD.LINE_CHANNEL.API_CALLBACK.SUBTITLE' + )}`; + } + if (this.isAEmailInbox) { return this.$t('INBOX_MGMT.ADD.EMAIL_CHANNEL.FINISH_MESSAGE'); } diff --git a/app/javascript/dashboard/routes/dashboard/settings/inbox/Index.vue b/app/javascript/dashboard/routes/dashboard/settings/inbox/Index.vue index dc5ec797a..a708f8064 100644 --- a/app/javascript/dashboard/routes/dashboard/settings/inbox/Index.vue +++ b/app/javascript/dashboard/routes/dashboard/settings/inbox/Index.vue @@ -51,6 +51,9 @@ Telegram + + Line + {{ globalConfig.apiChannelName || 'API' }} diff --git a/app/javascript/dashboard/routes/dashboard/settings/inbox/Settings.vue b/app/javascript/dashboard/routes/dashboard/settings/inbox/Settings.vue index 686497baa..4a241102f 100644 --- a/app/javascript/dashboard/routes/dashboard/settings/inbox/Settings.vue +++ b/app/javascript/dashboard/routes/dashboard/settings/inbox/Settings.vue @@ -259,7 +259,21 @@ :title="$t('INBOX_MGMT.ADD.TWILIO.API_CALLBACK.TITLE')" :sub-title="$t('INBOX_MGMT.ADD.TWILIO.API_CALLBACK.SUBTITLE')" > - + + + +
+ +
@@ -398,7 +412,12 @@ export default { ]; } - if (this.isATwilioChannel || this.isAPIInbox || this.isAnEmailChannel) { + if ( + this.isATwilioChannel || + this.isALineChannel || + this.isAPIInbox || + this.isAnEmailChannel + ) { return [ ...visibleToAllChannelTabs, { diff --git a/app/javascript/shared/mixins/inboxMixin.js b/app/javascript/shared/mixins/inboxMixin.js index 361ffdce2..c08c5876b 100644 --- a/app/javascript/shared/mixins/inboxMixin.js +++ b/app/javascript/shared/mixins/inboxMixin.js @@ -5,6 +5,7 @@ export const INBOX_TYPES = { TWILIO: 'Channel::TwilioSms', API: 'Channel::Api', EMAIL: 'Channel::Email', + LINE: 'Channel::Line', }; export default { @@ -27,6 +28,9 @@ export default { isATwilioChannel() { return this.channelType === INBOX_TYPES.TWILIO; }, + isALineChannel() { + return this.channelType === INBOX_TYPES.LINE; + }, isAnEmailChannel() { return this.channelType === INBOX_TYPES.EMAIL; }, diff --git a/app/models/inbox.rb b/app/models/inbox.rb index 9853ce79d..437e7fd99 100644 --- a/app/models/inbox.rb +++ b/app/models/inbox.rb @@ -31,6 +31,7 @@ class Inbox < ApplicationRecord include Avatarable include OutOfOffisable + validates :name, presence: true validates :account_id, presence: true validates :timezone, inclusion: { in: TZInfo::Timezone.all_identifiers } @@ -93,9 +94,9 @@ class Inbox < ApplicationRecord } end - def webhook_url + def callback_webhook_url case channel_type - when 'Channel::TwilioSMS' + when 'Channel::TwilioSms' "#{ENV['FRONTEND_URL']}/twilio/callback" when 'Channel::Line' "#{ENV['FRONTEND_URL']}/webhooks/line/#{channel.line_channel_id}" diff --git a/app/services/line/incoming_message_service.rb b/app/services/line/incoming_message_service.rb index 60204c8ad..80e10c2e1 100644 --- a/app/services/line/incoming_message_service.rb +++ b/app/services/line/incoming_message_service.rb @@ -1,9 +1,17 @@ +# ref : https://developers.line.biz/en/docs/messaging-api/receiving-messages/#webhook-event-types +# https://developers.line.biz/en/reference/messaging-api/#message-event + class Line::IncomingMessageService include ::FileTypeHelper pattr_initialize [:inbox!, :params!] def perform + # probably test events + return if params[:events].blank? + line_contact_info + return if line_contact_info['userId'].blank? + set_contact set_conversation # TODO: iterate over the events and handle the attachments in future diff --git a/app/views/api/v1/models/_inbox.json.jbuilder b/app/views/api/v1/models/_inbox.json.jbuilder index 4d97d0a18..20e1ca36c 100644 --- a/app/views/api/v1/models/_inbox.json.jbuilder +++ b/app/views/api/v1/models/_inbox.json.jbuilder @@ -1,4 +1,5 @@ json.id resource.id +json.avatar_url resource.try(:avatar_url) json.channel_id resource.channel_id json.name resource.name json.channel_type resource.channel_type @@ -6,30 +7,42 @@ json.greeting_enabled resource.greeting_enabled json.greeting_message resource.greeting_message json.working_hours_enabled resource.working_hours_enabled json.enable_email_collect resource.enable_email_collect -json.out_of_office_message resource.out_of_office_message json.csat_survey_enabled resource.csat_survey_enabled +json.enable_auto_assignment resource.enable_auto_assignment +json.out_of_office_message resource.out_of_office_message json.working_hours resource.weekly_schedule json.timezone resource.timezone -json.webhook_url resource.webhook_url -json.avatar_url resource.try(:avatar_url) -json.page_id resource.channel.try(:page_id) +json.callback_webhook_url resource.callback_webhook_url + +## Channel specific settings +## TODO : Clean up and move the attributes into channel sub section + +## WebWidget Attributes json.widget_color resource.channel.try(:widget_color) json.website_url resource.channel.try(:website_url) json.welcome_title resource.channel.try(:welcome_title) json.welcome_tagline resource.channel.try(:welcome_tagline) -json.enable_auto_assignment resource.enable_auto_assignment json.web_widget_script resource.channel.try(:web_widget_script) json.website_token resource.channel.try(:website_token) -json.forward_to_email resource.channel.try(:forward_to_email) -json.phone_number resource.channel.try(:phone_number) json.selected_feature_flags resource.channel.try(:selected_feature_flags) json.reply_time resource.channel.try(:reply_time) -json.reauthorization_required resource.channel.try(:reauthorization_required?) if resource.facebook? if resource.web_widget? json.hmac_token resource.channel.try(:hmac_token) json.pre_chat_form_enabled resource.channel.try(:pre_chat_form_enabled) json.pre_chat_form_options resource.channel.try(:pre_chat_form_options) end + +## Facebook Attributes +json.page_id resource.channel.try(:page_id) +json.reauthorization_required resource.channel.try(:reauthorization_required?) if resource.facebook? + +## Twilio Attributes +json.phone_number resource.channel.try(:phone_number) + +## Email Channel Attributes +json.forward_to_email resource.channel.try(:forward_to_email) json.email resource.channel.try(:email) if resource.email? + +## API Channel Attributes json.webhook_url resource.channel.try(:webhook_url) if resource.api? json.inbox_identifier resource.channel.try(:identifier) if resource.api? diff --git a/spec/controllers/api/v1/accounts/inboxes_controller_spec.rb b/spec/controllers/api/v1/accounts/inboxes_controller_spec.rb index 2e8ecdaea..6148c5148 100644 --- a/spec/controllers/api/v1/accounts/inboxes_controller_spec.rb +++ b/spec/controllers/api/v1/accounts/inboxes_controller_spec.rb @@ -306,6 +306,7 @@ RSpec.describe 'Inboxes API', type: :request do expect(response).to have_http_status(:success) expect(response.body).to include('Line Inbox') + expect(response.body).to include('callback_webhook_url') end end end @@ -352,7 +353,7 @@ RSpec.describe 'Inboxes API', type: :request do patch "/api/v1/accounts/#{account.id}/inboxes/#{api_inbox.id}", headers: admin.create_new_auth_token, - params: { enable_auto_assignment: false, channel: { webhook_url: 'webhook.test' } }, + params: { enable_auto_assignment: false, channel: { webhook_url: 'webhook.test', selected_feature_flags: [] } }, as: :json expect(response).to have_http_status(:success)