fix: Avoid processing reactions, ephemeral, request_welcome or unsupported messages (#8780)

Currently, we do not support reactions, ephemeral messages, or the request_welcome event for the WhatsApp channel. However, if this is the first event we receive in Chatwoot (i.e., there is no previous conversation or contact in Chatwoot), it will create a contact and a conversation without any messages. This confuses our customer, as it may appear that Chatwoot has missed some messages. There are multiple cases where this might be the first event we receive in Chatwoot. One quick example is when the user has sent an outbound campaign from another tool and their customers reacted to the message.

Another event like this is request_welcome event. WhatsApp has a concept for welcome messages. You can send an outbound message even though the user has not send a message. You can receive notifications through a webhook whenever a WhatsApp user initiates a chat with you for the first time. (Read the Welcome message section: https://developers.facebook.com/docs/whatsapp/cloud-api/phone-numbers/conversational-components/ ). Although this can help the business send a pro-active message to the user, we don't have it scoped in our feature set. For now, I'm ignoring this event.

Fixes https://linear.app/chatwoot/issue/CW-3018/whatsapp-handle-request-welcome-case-properly
Fixes https://linear.app/chatwoot/issue/CW-3017/whatsapp-handle-reactions-properly
This commit is contained in:
Pranav Raj S
2024-01-24 23:40:18 -08:00
committed by GitHub
parent 904d76420d
commit b7c9f779ad
4 changed files with 69 additions and 10 deletions

View File

@@ -19,7 +19,13 @@ class Whatsapp::IncomingMessageBaseService
private
def process_messages
# message allready exists so we don't need to process
# We don't support reactions & ephemeral message now, we need to skip processing the message
# if the webhook event is a reaction or an ephermal message or an unsupported message.
return if unprocessable_message_type?(message_type)
# Multiple webhook event can be received against the same message due to misconfigurations in the Meta
# business manager account. While we have not found the core reason yet, the following line ensure that
# there are no duplicate messages created.
return if find_message_by_source_id(@processed_params[:messages].first[:id]) || message_under_process?
cache_message_source_id_in_redis
@@ -49,8 +55,6 @@ class Whatsapp::IncomingMessageBaseService
end
def create_messages
return if unprocessable_message_type?(message_type)
message = @processed_params[:messages].first
log_error(message) && return if error_webhook_event?(message)

View File

@@ -44,7 +44,7 @@ module Whatsapp::IncomingMessageServiceHelpers
end
def unprocessable_message_type?(message_type)
%w[reaction ephemeral unsupported].include?(message_type)
%w[reaction ephemeral unsupported request_welcome].include?(message_type)
end
def brazil_phone_number?(phone_number)