Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions app/services/whatsapp/incoming_message_whatsapp_cloud_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,23 @@
# https://developers.facebook.com/docs/whatsapp/api/media/

class Whatsapp::IncomingMessageWhatsappCloudService < Whatsapp::IncomingMessageBaseService
include Events::Types

def perform
return if processed_params.blank?

Rails.configuration.dispatcher.dispatch(PROVIDER_EVENT_RECEIVED, Time.zone.now, inbox: inbox, event: whatsapp_cloud_event_type,
payload: processed_params)

super
end

private

def whatsapp_cloud_event_type
params.dig(:entry, 0, :changes, 0, :field) || 'unknown'
end

def processed_params
@processed_params ||= params[:entry].try(:first).try(:[], 'changes').try(:first).try(:[], 'value')
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,53 @@
end
end

context 'when dispatching provider events' do
let(:message_params) do
{
phone_number: whatsapp_channel.phone_number,
object: 'whatsapp_business_account',
entry: [{
changes: [{
field: 'messages',
value: {
contacts: [{ profile: { name: 'Sojan Jose' }, wa_id: '2423423243' }],
messages: [{
from: '2423423243',
text: { body: 'Hello' },
timestamp: '1664799904', type: 'text'
}]
}
}]
}]
}.with_indifferent_access
end

before do
allow(Rails.configuration.dispatcher).to receive(:dispatch)
end

it 'dispatches provider_event_received with the webhook field as event type' do
described_class.new(inbox: whatsapp_channel.inbox, params: message_params).perform

expect(Rails.configuration.dispatcher).to have_received(:dispatch).with(
'provider.event_received',
anything,
hash_including(
inbox: whatsapp_channel.inbox,
event: 'messages',
payload: message_params[:entry][0][:changes][0][:value]
)
)
end

it 'does not dispatch when processed_params is blank' do
empty_params = { phone_number: whatsapp_channel.phone_number, object: 'whatsapp_business_account', entry: {} }.with_indifferent_access
described_class.new(inbox: whatsapp_channel.inbox, params: empty_params).perform

expect(Rails.configuration.dispatcher).not_to have_received(:dispatch).with('provider.event_received', anything, anything)
end
end

context 'when message is a reply (has context)' do
let(:reply_params) do
{
Expand Down