feat: implement CSAT message handling in WhatsApp services (#161)

This commit is contained in:
Gabriel Jablonski 2025-12-02 16:05:39 -03:00 committed by GitHub
parent c73ef7f1a2
commit 797bde6566
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 62 additions and 12 deletions

View File

@ -59,12 +59,12 @@ class Whatsapp::Providers::WhatsappBaileysService < Whatsapp::Providers::BaseSer
@message = message
@recipient_id = recipient_id
if message.content_attributes[:is_reaction]
if @message.content_attributes[:is_reaction]
@message_content = reaction_message_content
elsif message.attachments.present?
elsif @message.attachments.present?
@message_content = attachment_message_content
elsif message.content.present?
@message_content = { text: @message.content }
elsif @message.outgoing_content.present?
@message_content = { text: @message.outgoing_content }
else
@message.update!(is_unsupported: true)
return
@ -252,7 +252,7 @@ class Whatsapp::Providers::WhatsappBaileysService < Whatsapp::Providers::BaseSer
react: { key: { id: reply_to.source_id,
remoteJid: remote_jid,
fromMe: reply_to.message_type == 'outgoing' },
text: @message.content }
text: @message.outgoing_content }
}
end
@ -262,7 +262,7 @@ class Whatsapp::Providers::WhatsappBaileysService < Whatsapp::Providers::BaseSer
content = {
fileName: attachment.file.filename,
caption: @message.content
caption: @message.outgoing_content
}
case attachment.file_type
when 'image'

View File

@ -17,7 +17,7 @@ class Whatsapp::Providers::WhatsappZapiService < Whatsapp::Providers::BaseServic
send_reaction_message(phone, message, **params)
elsif message.attachments.present?
handle_message_with_attachment(message, phone, **params)
elsif message.content.present?
elsif message.outgoing_content.present?
send_text_message(phone, message, **params)
else
message.update!(is_unsupported: true)
@ -134,7 +134,7 @@ class Whatsapp::Providers::WhatsappZapiService < Whatsapp::Providers::BaseServic
headers: api_headers,
body: {
phone: phone,
message: message.content,
message: message.outgoing_content,
**params
}.compact.to_json
)
@ -188,7 +188,7 @@ class Whatsapp::Providers::WhatsappZapiService < Whatsapp::Providers::BaseServic
body: {
phone: phone,
image: buffer,
caption: message.content,
caption: message.outgoing_content,
**params
}.compact.to_json
)
@ -229,7 +229,7 @@ class Whatsapp::Providers::WhatsappZapiService < Whatsapp::Providers::BaseServic
phone: phone,
document: buffer,
fileName: attachment.file.filename.to_s,
caption: message.content,
caption: message.outgoing_content,
**params
}.compact.to_json
)
@ -246,7 +246,7 @@ class Whatsapp::Providers::WhatsappZapiService < Whatsapp::Providers::BaseServic
body: {
phone: phone,
video: buffer,
caption: message.content,
caption: message.outgoing_content,
**params
}.compact.to_json
)
@ -262,7 +262,7 @@ class Whatsapp::Providers::WhatsappZapiService < Whatsapp::Providers::BaseServic
headers: api_headers,
body: {
phone: phone,
reaction: message.content,
reaction: message.outgoing_content,
messageId: message.in_reply_to_external_id,
**params
}.compact.to_json

View File

@ -193,6 +193,33 @@ describe Whatsapp::Providers::WhatsappBaileysService do
end
end
context 'when message is input_csat' do
before do
allow(message).to receive(:outgoing_content).and_return('Please rate us http://example.com/survey')
message.content_type = :input_csat
end
it 'sends the message with survey url' do
stub_request(:post, request_path)
.with(
headers: stub_headers(whatsapp_channel),
body: {
jid: test_send_jid,
messageContent: { text: 'Please rate us http://example.com/survey' }
}.to_json
)
.to_return(
status: 200,
headers: { 'Content-Type' => 'application/json' },
body: result_body.to_json
)
result = service.send_message(test_send_phone_number, message)
expect(result).to eq('msg_123')
end
end
context 'when message has attachment' do
let(:base64_image) { 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=' }

View File

@ -285,6 +285,29 @@ describe Whatsapp::Providers::WhatsappZapiService do
end
end
context 'when message is input_csat' do
before do
allow(message).to receive(:outgoing_content).and_return('Please rate us http://example.com/survey')
message.content_type = :input_csat
end
it 'sends the message with survey url' do
stub_request(:post, request_path)
.with(
headers: stub_headers,
body: {
phone: test_send_phone_number,
message: 'Please rate us http://example.com/survey'
}.to_json
)
.to_return(status: 200, body: result_body.to_json, headers: { 'Content-Type' => 'application/json' })
result = service.send_message("+#{test_send_phone_number}", message)
expect(result).to eq('msg_123')
end
end
context 'when message is a text' do
it 'sends the text message' do
stub_request(:post, request_path)