Skip to content

Commit

Permalink
Allow configuration of WhatsApp quick reply button text (#2079)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwr18 authored Nov 20, 2024
1 parent fdbe579 commit ea357e7
Show file tree
Hide file tree
Showing 14 changed files with 178 additions and 180 deletions.
16 changes: 8 additions & 8 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ GEM
activerecord (>= 6.0, < 7.1)
addressable (2.8.1)
public_suffix (>= 2.0.2, < 6.0)
administrate (0.18.0)
actionpack (>= 5.0)
actionview (>= 5.0)
activerecord (>= 5.0)
jquery-rails (>= 4.0)
kaminari (>= 1.0)
administrate (0.20.1)
actionpack (>= 6.0, < 8.0)
actionview (>= 6.0, < 8.0)
activerecord (>= 6.0, < 8.0)
jquery-rails (~> 4.6.0)
kaminari (~> 1.2.2)
sassc-rails (~> 2.1)
selectize-rails (~> 0.6)
administrate-field-active_storage (0.3.8)
Expand Down Expand Up @@ -204,7 +204,7 @@ GEM
image_processing (1.12.2)
mini_magick (>= 4.9.5, < 5)
ruby-vips (>= 2.0.17, < 3)
jquery-rails (4.5.0)
jquery-rails (4.6.0)
rails-dom-testing (>= 1, < 3)
railties (>= 4.2.0)
thor (>= 0.14, < 2.0)
Expand Down Expand Up @@ -499,4 +499,4 @@ RUBY VERSION
ruby 3.0.0p0

BUNDLED WITH
2.5.21
2.5.22
255 changes: 129 additions & 126 deletions ansible/inventories/staging/host_vars/staging.yml

Large diffs are not rendered by default.

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
28 changes: 1 addition & 27 deletions app/controllers/admin/organizations_controller.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,5 @@
# frozen_string_literal: true

module Admin
class OrganizationsController < Admin::ApplicationController
def update
organization = Organization.find(update_params[:id])
organization.business_plan.update(valid_from: nil, valid_until: nil)
business_plan = BusinessPlan.find(update_params[:organization][:business_plan_id])
business_plan.update(valid_from: Time.current, valid_until: 1.year.from_now)
organization.upgraded_business_plan_at =
(Time.current if business_plan.price_per_month > organization.business_plan.price_per_month)
if organization.update(update_params[:organization])
redirect_to admin_organization_path(organization), flash: { success: 'Organization was successfully updated.' }
else
render :edit, status: :unprocessable_entity
end
end

private

def update_params
params.permit(:id,
organization: %i[id business_plan_id upgrade_discount contact_person_id name
threemarb_api_identity twilio_account_sid onboarding_allowed
onboarding_data_protection_link onboarding_data_processing_consent_additional_info
onboarding_imprint_link onboarding_ask_for_additional_consent
onboarding_additional_consent_heading onboarding_additional_consent_text
channel_image about signal_complete_onboarding_link])
end
end
class OrganizationsController < Admin::ApplicationController; end
end
7 changes: 6 additions & 1 deletion app/dashboards/organization_dashboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ class OrganizationDashboard < Administrate::BaseDashboard
onboarding_additional_consent_heading: Field::String,
onboarding_additional_consent_text: Field::String,
channel_image: Field::ActiveStorage,
whats_app_more_info_message: Field::Text,
whats_app_profile_about: Field::Text,
signal_complete_onboarding_link: Field::Url
signal_complete_onboarding_link: Field::Url,
whats_app_quick_reply_button_text: Field::JSONB
}.freeze

COLLECTION_ATTRIBUTES = %i[
Expand All @@ -48,6 +50,7 @@ class OrganizationDashboard < Administrate::BaseDashboard
created_at
updated_at
upgraded_business_plan_at
whats_app_more_info_message
].freeze

FORM_ATTRIBUTES_EDIT = %i[
Expand All @@ -56,6 +59,7 @@ class OrganizationDashboard < Administrate::BaseDashboard
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 +69,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
4 changes: 4 additions & 0 deletions app/models/organization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ def onboarding_allowed=(value)
self[:onboarding_allowed] = value.is_a?(String) ? JSON.parse(value) : value
end

def whats_app_quick_reply_button_text=(value)
self[:whats_app_quick_reply_button_text] = value.is_a?(String) ? JSON.parse(value) : value
end

def whats_app_configured?
twilio_configured? || three_sixty_dialog_configured?
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

class AddWhatsAppColumnsToOrganizations < ActiveRecord::Migration[6.1]
def change
change_table :organizations, bulk: true do |t|
t.jsonb :whats_app_quick_reply_button_text, default: { answer_request: 'Antworten', more_info: 'Mehr Infos' }
t.string :whats_app_more_info_message, default: ''
end
end
end
4 changes: 3 additions & 1 deletion 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_182931) do

# These are extensions that must be enabled in order to support this database
enable_extension "pg_trgm"
Expand Down Expand Up @@ -234,6 +234,8 @@
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.string "whats_app_more_info_message", default: ""
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
2 changes: 2 additions & 0 deletions spec/system/admin/organizations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@

click_on 'Edit'
fill_in 'Name', with: 'Real name'
fill_in 'Whats app more info message', with: 'More info. Unsubscribe?'
click_on 'Update Organization'

expect(page).to have_text('Organization was successfully updated.')
expect(page).to have_text('Show Real name')
expect(page).to have_content('More info. Unsubscribe?')
end
end
end

0 comments on commit ea357e7

Please sign in to comment.