feat: support reply to for outgoing message in WhatsApp (#8107)

- This PR enables replies to WhatsApp.
This commit is contained in:
Shivam Mishra
2023-10-20 01:54:46 +05:30
committed by GitHub
parent b94c89ebf1
commit 7416bbb25e
8 changed files with 95 additions and 29 deletions

View File

@@ -0,0 +1,24 @@
class Messages::InReplyToMessageBuilder
pattr_initialize [:message!, :in_reply_to!, :in_reply_to_external_id!]
delegate :conversation, to: :message
def perform
set_in_reply_to_attribute if @in_reply_to.present? || @in_reply_to_external_id.present?
end
private
def set_in_reply_to_attribute
@message.content_attributes[:in_reply_to_external_id] = in_reply_to_message.try(:source_id)
@message.content_attributes[:in_reply_to] = in_reply_to_message.try(:id)
end
def in_reply_to_message
return conversation.messages.find_by(id: @in_reply_to) if @in_reply_to.present?
return conversation.messages.find_by(source_id: @in_reply_to_external_id) if @in_reply_to_external_id
nil
end
end

View File

@@ -144,8 +144,7 @@ class Whatsapp::IncomingMessageBaseService
message_type: :incoming,
sender: @contact,
source_id: message[:id].to_s,
in_reply_to_external_id: @in_reply_to_external_id,
in_reply_to: @in_reply_to
in_reply_to_external_id: @in_reply_to_external_id
)
end

View File

@@ -88,15 +88,7 @@ module Whatsapp::IncomingMessageServiceHelpers
end
def process_in_reply_to(message)
return if message['context'].blank?
@in_reply_to_external_id = message['context']['id']
return if @in_reply_to_external_id.blank?
in_reply_to_message = Message.find_by(source_id: @in_reply_to_external_id)
@in_reply_to = in_reply_to_message.id if in_reply_to_message.present?
@in_reply_to_external_id = message['context']&.[]('id')
end
def find_message_by_source_id(source_id)

View File

@@ -78,6 +78,7 @@ class Whatsapp::Providers::WhatsappCloudService < Whatsapp::Providers::BaseServi
headers: api_headers,
body: {
messaging_product: 'whatsapp',
context: whatsapp_reply_context(message),
to: phone_number,
text: { body: message.content },
type: 'text'
@@ -100,6 +101,7 @@ class Whatsapp::Providers::WhatsappCloudService < Whatsapp::Providers::BaseServi
headers: api_headers,
body: {
:messaging_product => 'whatsapp',
:context => whatsapp_reply_context(message),
'to' => phone_number,
'type' => type,
type.to_s => type_content
@@ -132,6 +134,15 @@ class Whatsapp::Providers::WhatsappCloudService < Whatsapp::Providers::BaseServi
}
end
def whatsapp_reply_context(message)
reply_to = message.content_attributes[:in_reply_to_external_id]
return nil if reply_to.blank?
{
message_id: reply_to
}
end
def send_interactive_text_message(phone_number, message)
payload = create_payload_based_on_items(message)