Skip to content

Commit

Permalink
Allow configuration of WhatsApp quick reply button text
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwr18 committed Nov 20, 2024
1 parent 60ee87f commit 0a1b378
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 23 deletions.
9 changes: 2 additions & 7 deletions app/adapters/whats_app_adapter/three_sixty_dialog_inbound.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def error_indicating_unsupported_content(errors)
def request_for_more_info?(text)
return false if text.blank?

text.strip.eql?(I18n.t('adapter.whats_app.quick_reply_button_text.more_info'))
text.strip.eql?(organization.whats_app_quick_reply_button_text['more_info'])
end

def request_to_receive_message?(contributor, text)
Expand All @@ -150,12 +150,7 @@ def request_to_receive_message?(contributor, text)
end

def quick_reply_response?(text)
quick_reply_keys = %w[answer more_info]
quick_reply_texts = []
quick_reply_keys.each do |key|
quick_reply_texts << I18n.t("adapter.whats_app.quick_reply_button_text.#{key}")
end
text.strip.in?(quick_reply_texts)
text.strip.in?(organization.whats_app_quick_reply_button_text.values)
end

def unsubscribe_text?(text)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,9 @@ def send_unsupported_content_message!(contributor, organization)
def send_more_info_message!(contributor, organization)
return unless contributor_can_receive_messages?(contributor)

text = [organization.whats_app_profile_about, "_#{I18n.t('adapter.shared.unsubscribe.instructions')}_"].join("\n\n")
WhatsAppAdapter::ThreeSixtyDialogOutbound::Text.perform_later(organization_id: organization.id,
payload: text_payload(
contributor, text
contributor, organization.whats_app_more_info_message
))
end

Expand Down
2 changes: 1 addition & 1 deletion app/adapters/whats_app_adapter/twilio_outbound.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def send_unsupported_content_message!(contributor, organization)
def send_more_info_message!(contributor, organization)
return unless contributor_can_receive_messages?(contributor)

text = [organization.whats_app_profile_about, "_#{I18n.t('adapter.shared.unsubscribe.instructions')}_"].join("\n\n")
text = [organization.whats_app_more_info_message, "_#{I18n.t('adapter.shared.unsubscribe.instructions')}_"].join("\n\n")
WhatsAppAdapter::TwilioOutbound::Text.perform_later(organization_id: organization.id, contributor_id: contributor.id, text: text)
end

Expand Down
8 changes: 5 additions & 3 deletions app/dashboards/organization_dashboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ class OrganizationDashboard < Administrate::BaseDashboard
onboarding_additional_consent_heading: Field::String,
onboarding_additional_consent_text: Field::String,
channel_image: Field::ActiveStorage,
whats_app_profile_about: Field::Text,
signal_complete_onboarding_link: Field::Url
whats_app_more_info_message: Field::Text,
signal_complete_onboarding_link: Field::Url,
whats_app_quick_reply_button_text: Field::JSONB
}.freeze

COLLECTION_ATTRIBUTES = %i[
Expand All @@ -55,7 +56,7 @@ class OrganizationDashboard < Administrate::BaseDashboard
contact_person
business_plan
upgrade_discount
whats_app_profile_about
whats_app_more_info_message
onboarding_data_protection_link
onboarding_data_processing_consent_additional_info
onboarding_imprint_link
Expand All @@ -65,6 +66,7 @@ class OrganizationDashboard < Administrate::BaseDashboard
onboarding_allowed
channel_image
signal_complete_onboarding_link
whats_app_quick_reply_button_text
].freeze

COLLECTION_FILTERS = {}.freeze
Expand Down
2 changes: 1 addition & 1 deletion db/migrate/20240621100548_add_settings_to_organizations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def change
t.string :three_sixty_dialog_client_id
t.string :three_sixty_dialog_client_waba_account_id
t.string :email_from_address
t.string :whats_app_profile_about, default: ''
t.string :whats_app_more_info_message, default: ''
t.jsonb :onboarding_allowed, default: { threema: true, telegram: true, email: true, signal: true, whats_app: true }
t.index :telegram_bot_username, unique: true
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class AddWhatsAppQuickReplyButtonTextToOrganizations < ActiveRecord::Migration[6.1]
def change
add_column :organizations, :whats_app_quick_reply_button_text, :jsonb, default: { answer_request: 'Antworten', more_info: 'Mehr Infos' }
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class ChangeWhatsAppProfileAboutToWhatsAppMoreInfoMessageOnOrganizations < ActiveRecord::Migration[6.1]
def change
rename_column :organizations, :whats_app_profile_about, :whats_app_more_info_message
end
end
5 changes: 3 additions & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2024_11_19_084424) do
ActiveRecord::Schema.define(version: 2024_11_20_150844) do

# These are extensions that must be enabled in order to support this database
enable_extension "pg_trgm"
Expand Down Expand Up @@ -230,10 +230,11 @@
t.string "three_sixty_dialog_client_id"
t.string "three_sixty_dialog_client_waba_account_id"
t.string "email_from_address"
t.string "whats_app_profile_about", default: ""
t.string "whats_app_more_info_message", default: ""
t.jsonb "onboarding_allowed", default: {"email"=>true, "signal"=>true, "threema"=>true, "telegram"=>true, "whats_app"=>true}
t.jsonb "twilio_content_sids", default: {"new_request_day1"=>"", "new_request_day2"=>"", "new_request_day3"=>"", "new_request_night1"=>"", "new_request_night2"=>"", "new_request_night3"=>"", "new_request_evening1"=>"", "new_request_evening2"=>"", "new_request_evening3"=>"", "new_request_morning1"=>"", "new_request_morning2"=>"", "new_request_morning3"=>""}
t.string "signal_complete_onboarding_link"
t.jsonb "whats_app_quick_reply_button_text", default: {"more_info"=>"Mehr Infos", "answer_request"=>"Antworten"}
t.index ["business_plan_id"], name: "index_organizations_on_business_plan_id"
t.index ["contact_person_id"], name: "index_organizations_on_contact_person_id"
t.index ["telegram_bot_username"], name: "index_organizations_on_telegram_bot_username", unique: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
RSpec.describe WhatsAppAdapter::ThreeSixtyDialogOutbound do
let(:adapter) { described_class.new }
let(:organization) do
create(:organization, project_name: 'Great project')
create(:organization,
project_name: 'Great project',
whats_app_more_info_message: "We're cool, but if you want to unsubscribe, write 'unsubscribe'")
end
let!(:message) do
create(:message, :outbound, text: '360dialog is great!', broadcasted: true, recipient: contributor,
Expand Down Expand Up @@ -210,8 +212,7 @@
end

before do
text_payload[:text][:body] =
[organization.whats_app_profile_about, "_#{I18n.t('adapter.shared.unsubscribe.instructions')}_"].join("\n\n")
text_payload[:text][:body] = organization.whats_app_more_info_message
end

it {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
subject { -> { described_class.new.perform(organization_id: organization.id, components: components) } }

let(:organization) do
create(:organization, three_sixty_dialog_client_api_key: 'valid_api_key')
create(:organization,
three_sixty_dialog_client_api_key: 'valid_api_key',
whats_app_quick_reply_button_text: { answer_request: 'Mehr Infos', more_info: 'Über uns' },
whats_app_more_info_message: "Please do not unsubscribe. Unless you want to. Then send a 'unsubscribe'")
end
let(:components) do
{
Expand Down Expand Up @@ -180,9 +183,9 @@
context 'request for more info' do
before do
components[:messages].first.delete(:text)
components[:messages].first[:button] = { text: 'Mehr Infos' }
components[:messages].first[:button] = { text: 'Über uns' }
end
let(:text) { [organization.whats_app_profile_about, "_#{I18n.t('adapter.shared.unsubscribe.instructions')}_"].join("\n\n") }
let(:text) { organization.whats_app_more_info_message }

it 'marks that contributor has responded to template message' do
expect { subject.call }.to change {
Expand Down
2 changes: 1 addition & 1 deletion spec/requests/whats_app/webhook_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@
context 'request for more info' do
before do
params['Body'] = 'Mehr Infos'
organization.update(whats_app_profile_about: 'Here is more info')
organization.update(whats_app_more_info_message: 'Here is more info')
end
let(:more_info_job_args) do
{
Expand Down

0 comments on commit 0a1b378

Please sign in to comment.