From 0a1b378009ea21b38f1f846dee72e3ad6f7b18f3 Mon Sep 17 00:00:00 2001 From: Matthew Rider Date: Wed, 20 Nov 2024 16:27:42 +0100 Subject: [PATCH] Allow configuration of WhatsApp quick reply button text --- .../whats_app_adapter/three_sixty_dialog_inbound.rb | 9 ++------- .../whats_app_adapter/three_sixty_dialog_outbound.rb | 3 +-- app/adapters/whats_app_adapter/twilio_outbound.rb | 2 +- app/dashboards/organization_dashboard.rb | 8 +++++--- .../20240621100548_add_settings_to_organizations.rb | 2 +- ...whats_app_quick_reply_button_text_to_organizations.rb | 7 +++++++ ...ut_to_whats_app_more_info_message_on_organizations.rb | 7 +++++++ db/schema.rb | 5 +++-- .../three_sixty_dialog_outbound_spec.rb | 7 ++++--- .../three_sixty_dialog/process_webhook_job_spec.rb | 9 ++++++--- spec/requests/whats_app/webhook_spec.rb | 2 +- 11 files changed, 38 insertions(+), 23 deletions(-) create mode 100644 db/migrate/20241120144127_add_whats_app_quick_reply_button_text_to_organizations.rb create mode 100644 db/migrate/20241120150844_change_whats_app_profile_about_to_whats_app_more_info_message_on_organizations.rb diff --git a/app/adapters/whats_app_adapter/three_sixty_dialog_inbound.rb b/app/adapters/whats_app_adapter/three_sixty_dialog_inbound.rb index f473aa430..999e65ced 100644 --- a/app/adapters/whats_app_adapter/three_sixty_dialog_inbound.rb +++ b/app/adapters/whats_app_adapter/three_sixty_dialog_inbound.rb @@ -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) @@ -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) diff --git a/app/adapters/whats_app_adapter/three_sixty_dialog_outbound.rb b/app/adapters/whats_app_adapter/three_sixty_dialog_outbound.rb index 2e425f3cd..b27fdeaa5 100644 --- a/app/adapters/whats_app_adapter/three_sixty_dialog_outbound.rb +++ b/app/adapters/whats_app_adapter/three_sixty_dialog_outbound.rb @@ -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 diff --git a/app/adapters/whats_app_adapter/twilio_outbound.rb b/app/adapters/whats_app_adapter/twilio_outbound.rb index 60bc2fa95..b77a25d50 100644 --- a/app/adapters/whats_app_adapter/twilio_outbound.rb +++ b/app/adapters/whats_app_adapter/twilio_outbound.rb @@ -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 diff --git a/app/dashboards/organization_dashboard.rb b/app/dashboards/organization_dashboard.rb index 1f326eeec..30840bd8e 100644 --- a/app/dashboards/organization_dashboard.rb +++ b/app/dashboards/organization_dashboard.rb @@ -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[ @@ -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 @@ -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 diff --git a/db/migrate/20240621100548_add_settings_to_organizations.rb b/db/migrate/20240621100548_add_settings_to_organizations.rb index f0e9f9f13..744b94e45 100644 --- a/db/migrate/20240621100548_add_settings_to_organizations.rb +++ b/db/migrate/20240621100548_add_settings_to_organizations.rb @@ -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 diff --git a/db/migrate/20241120144127_add_whats_app_quick_reply_button_text_to_organizations.rb b/db/migrate/20241120144127_add_whats_app_quick_reply_button_text_to_organizations.rb new file mode 100644 index 000000000..2dd4e893b --- /dev/null +++ b/db/migrate/20241120144127_add_whats_app_quick_reply_button_text_to_organizations.rb @@ -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 diff --git a/db/migrate/20241120150844_change_whats_app_profile_about_to_whats_app_more_info_message_on_organizations.rb b/db/migrate/20241120150844_change_whats_app_profile_about_to_whats_app_more_info_message_on_organizations.rb new file mode 100644 index 000000000..1ff37e404 --- /dev/null +++ b/db/migrate/20241120150844_change_whats_app_profile_about_to_whats_app_more_info_message_on_organizations.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 41a6f75f7..889b5fd83 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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" @@ -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 diff --git a/spec/adapters/whats_app_adapter/three_sixty_dialog_outbound_spec.rb b/spec/adapters/whats_app_adapter/three_sixty_dialog_outbound_spec.rb index 87c20572b..334aa71b7 100644 --- a/spec/adapters/whats_app_adapter/three_sixty_dialog_outbound_spec.rb +++ b/spec/adapters/whats_app_adapter/three_sixty_dialog_outbound_spec.rb @@ -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, @@ -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 { diff --git a/spec/jobs/whats_app_adapter/three_sixty_dialog/process_webhook_job_spec.rb b/spec/jobs/whats_app_adapter/three_sixty_dialog/process_webhook_job_spec.rb index d4ec33340..e1fa72b0b 100644 --- a/spec/jobs/whats_app_adapter/three_sixty_dialog/process_webhook_job_spec.rb +++ b/spec/jobs/whats_app_adapter/three_sixty_dialog/process_webhook_job_spec.rb @@ -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 { @@ -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 { diff --git a/spec/requests/whats_app/webhook_spec.rb b/spec/requests/whats_app/webhook_spec.rb index b568a2320..b1570fdb4 100644 --- a/spec/requests/whats_app/webhook_spec.rb +++ b/spec/requests/whats_app/webhook_spec.rb @@ -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 {