Skip to content

Commit

Permalink
Adapt Twilio template message sending to content api (#2052)
Browse files Browse the repository at this point in the history
Closes #2051
  • Loading branch information
mattwr18 authored Oct 7, 2024
1 parent 81590c2 commit 39ed9e5
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 25 deletions.
6 changes: 2 additions & 4 deletions app/adapters/whats_app_adapter/twilio_outbound.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,8 @@ def send_resubscribe_error_message!(contributor, organization)

def send_message_template!(recipient, message)
recipient.update(whats_app_message_template_sent_at: Time.current)
text = I18n.t("adapter.whats_app.request_template.new_request_#{time_of_day}_#{rand(1..3)}", first_name: recipient.first_name,
request_title: message.request.title)
WhatsAppAdapter::TwilioOutbound::Text.perform_later(organization_id: message.organization.id, contributor_id: recipient.id,
text: text)
content_sid = message.organization.twilio_content_sids["new_request_#{time_of_day}#{rand(1..3)}"]
WhatsAppAdapter::TwilioOutbound::Template.perform_later(content_sid: content_sid, message_id: message.id)
end

private
Expand Down
29 changes: 29 additions & 0 deletions app/adapters/whats_app_adapter/twilio_outbound/template.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

module WhatsAppAdapter
class TwilioOutbound
class Template < ApplicationJob
queue_as :default

def perform(content_sid:, message_id:)
message = Message.find(message_id)
organization = message.organization
contributor = message.recipient

response = organization.twilio_instance.messages.create(
content_sid: content_sid,
from: "whatsapp:#{organization.whats_app_server_phone_number}",
to: "whatsapp:#{contributor.whats_app_phone_number}",
content_variables: {
'1' => contributor.first_name,
'2' => message.request.title
}.to_json
)

message.update(external_id: response.sid)
rescue ActiveRecord::RecordNotFound => e
ErrorNotifier.report(e)
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

class AddTwilioContentSidsToOrganizations < ActiveRecord::Migration[6.1]
def change
add_column :organizations, :twilio_content_sids, :jsonb, default: {
new_request_morning1: '',
new_request_morning2: '',
new_request_morning3: '',
new_request_day1: '',
new_request_day2: '',
new_request_day3: '',
new_request_evening1: '',
new_request_evening2: '',
new_request_evening3: '',
new_request_night1: '',
new_request_night2: '',
new_request_night3: ''
}
end
end
3 changes: 2 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_09_11_210001) do
ActiveRecord::Schema.define(version: 2024_10_02_151103) do

# These are extensions that must be enabled in order to support this database
enable_extension "pg_trgm"
Expand Down Expand Up @@ -232,6 +232,7 @@
t.string "email_from_address"
t.string "whats_app_profile_about", 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.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
20 changes: 9 additions & 11 deletions spec/adapters/whats_app_adapter/twilio_outbound_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
create(:organization, onboarding_success_heading: onboarding_success_heading, onboarding_success_text: onboarding_success_text)
end
let(:contributor) { create(:contributor, email: nil) }
let(:onboarding_success_heading) { 'Thanks for onboarding' }
let(:onboarding_success_text) { 'We will start sending messages soon.' }

describe '::send_welcome_message!' do
let(:expected_job_args) do
{ organization_id: organization.id, contributor_id: contributor.id,
text: ["*#{onboarding_success_heading}*", onboarding_success_text].join("\n\n") }
text: "*Thanks for onboarding*\n\nWe will start sending messages soon." }
end
let(:onboarding_success_heading) { 'Thanks for onboarding' }
let(:onboarding_success_text) { 'We will start sending messages soon.' }
subject { -> { described_class.send_welcome_message!(contributor, organization) } }
before do
message # we don't count the extra ::send here
Expand Down Expand Up @@ -57,10 +57,9 @@

describe 'contributor has not sent a message within 24 hours' do
it 'enqueues the Text job with WhatsApp template' do
expect { subject.call }.to(have_enqueued_job(WhatsAppAdapter::TwilioOutbound::Text).on_queue('default').with do |params|
expect(params[:contributor_id]).to eq(contributor.id)
expect(params[:text]).to include(contributor.first_name)
expect(params[:text]).to include(message.request.title)
expect { subject.call }.to(have_enqueued_job(WhatsAppAdapter::TwilioOutbound::Template).on_queue('default').with do |params|
expect(params[:content_sid]).to be_kind_of(String)
expect(params[:message_id]).to eq(message.id)
end)
end
end
Expand Down Expand Up @@ -94,10 +93,9 @@

context 'contributor has not sent a message within 24 hours' do
it 'enqueues the Text job with WhatsApp template' do
expect { subject.call }.to(have_enqueued_job(WhatsAppAdapter::TwilioOutbound::Text).on_queue('default').with do |params|
expect(params[:contributor_id]).to eq(contributor.id)
expect(params[:text]).to include(contributor.first_name)
expect(params[:text]).to include(message.request.title)
expect { subject.call }.to(have_enqueued_job(WhatsAppAdapter::TwilioOutbound::Template).on_queue('default').with do |params|
expect(params[:content_sid]).to be_kind_of(String)
expect(params[:message_id]).to eq(message.id)
end)
end
end
Expand Down
8 changes: 3 additions & 5 deletions spec/models/message_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,9 @@
let(:message) { create(:message, :outbound, recipient: create(:contributor, :whats_app_contributor)) }

it 'enqueues a job to send the message' do
expect { subject }.to(have_enqueued_job(WhatsAppAdapter::TwilioOutbound::Text).on_queue('default').with do |params|
expect(params[:organization_id]).to eq(message.organization_id)
expect(params[:contributor_id]).to eq(message.contributor.id)
expect(params[:text]).to include(message.contributor.first_name)
expect(params[:text]).to include(message.request.title)
expect { subject }.to(have_enqueued_job(WhatsAppAdapter::TwilioOutbound::Template).on_queue('default').with do |params|
expect(params[:content_sid]).to be_kind_of(String)
expect(params[:message_id]).to eq(message.id)
end)
end
end
Expand Down
7 changes: 3 additions & 4 deletions spec/requests/whats_app/webhook_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -374,10 +374,9 @@
let(:body_text) { "Hey #{contributor.first_name}, because it was sent outside the allowed window" }

it 'enqueues the Text job with WhatsApp template' do
expect { subject.call }.to(have_enqueued_job(WhatsAppAdapter::TwilioOutbound::Text).on_queue('default').with do |params|
expect(params[:contributor_id]).to eq(contributor.id)
expect(params[:text]).to include(contributor.first_name)
expect(params[:text]).to include(message.request.title)
expect { subject.call }.to(have_enqueued_job(WhatsAppAdapter::TwilioOutbound::Template).on_queue('default').with do |params|
expect(params[:content_sid]).to be_kind_of(String)
expect(params[:message_id]).to eq(message.id)
end)
end

Expand Down

0 comments on commit 39ed9e5

Please sign in to comment.