fix: baileys send message with jid (#32)

* fix: update send_message method to handle  baileys-api send-message changes

* chore: simplify update of is_unsupported

* refactor: remove useless helper

* fix: update send_message method to handle unsupported message content

* chore: handle unsupported message content in send_message method and update message status

---------

Co-authored-by: gabrieljablonski <contact@gabrieljablonski.com>
This commit is contained in:
Cayo P. R. Oliveira 2025-04-29 20:52:09 -03:00 committed by GitHub
parent fd8e099f4d
commit 8dc893d322
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 29 additions and 50 deletions

View File

@ -32,13 +32,17 @@ class Whatsapp::Providers::WhatsappBaileysService < Whatsapp::Providers::BaseSer
def send_message(phone_number, message)
@message = message
@phone_number = phone_number
if message.attachments.present?
send_attachment_message
@message_content = attachment_message_content
elsif message.content.present?
send_text_message
@message_content = { text: @message.content }
else
message.update!(content: I18n.t('errors.messages.send.unsupported'), status: 'failed')
return
end
send_message_request
end
def send_template(phone_number, template_info); end
@ -70,31 +74,15 @@ class Whatsapp::Providers::WhatsappBaileysService < Whatsapp::Providers::BaseSer
whatsapp_channel.provider_config['api_key'].presence || DEFAULT_API_KEY
end
def send_attachment_message
@attachment = @message.attachments.first
response = HTTParty.post(
"#{provider_url}/connections/#{whatsapp_channel.phone_number}/send-message",
headers: api_headers,
body: {
recipient: @phone_number,
messageContent: message_content
}.to_json
)
return response.parsed_response.dig('data', 'key', 'id') if process_response(response)
raise MessageNotSentError
end
def message_content
buffer = Base64.strict_encode64(@attachment.file.download)
def attachment_message_content
attachment = @message.attachments.first
buffer = Base64.strict_encode64(attachment.file.download)
content = {
fileName: @attachment.file.filename,
fileName: attachment.file.filename,
caption: @message.content
}
case @attachment.file_type
case attachment.file_type
when 'image'
content[:image] = buffer
when 'audio'
@ -110,13 +98,13 @@ class Whatsapp::Providers::WhatsappBaileysService < Whatsapp::Providers::BaseSer
content.compact
end
def send_text_message
def send_message_request
response = HTTParty.post(
"#{provider_url}/connections/#{whatsapp_channel.phone_number}/send-message",
headers: api_headers,
body: {
recipient: @phone_number,
messageContent: { text: @message.content }
jid: "#{@phone_number.delete('+')}@s.whatsapp.net",
messageContent: @message_content
}.to_json
)

View File

@ -7,6 +7,7 @@ describe Whatsapp::Providers::WhatsappBaileysService do
let(:message) { create(:message) }
let(:test_send_phone_number) { '+5511987654321' }
let(:test_send_jid) { '5511987654321@s.whatsapp.net' }
before do
stub_const('Whatsapp::Providers::WhatsappBaileysService::DEFAULT_CLIENT_NAME', 'chatwoot-test')
@ -91,6 +92,17 @@ describe Whatsapp::Providers::WhatsappBaileysService do
end
describe '#send_message' do
context 'when message does not have content nor attachments' do
it 'updates the message with content attribute is_unsupported' do
message.update!(content: nil)
service.send_message(test_send_phone_number, message)
expect(message.content).to eq(I18n.t('errors.messages.send.unsupported'))
expect(message.status).to eq('failed')
end
end
context 'when message has attachment' do
let(:base64_image) { 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=' }
@ -111,7 +123,7 @@ describe Whatsapp::Providers::WhatsappBaileysService do
.with(
headers: stub_headers(whatsapp_channel),
body: {
recipient: test_send_phone_number,
jid: test_send_jid,
messageContent: { fileName: 'image.png', caption: message.content, image: base64_image }
}.to_json
)
@ -132,7 +144,7 @@ describe Whatsapp::Providers::WhatsappBaileysService do
.with(
headers: stub_headers(whatsapp_channel),
body: {
recipient: test_send_phone_number,
jid: test_send_jid,
messageContent: { fileName: 'image.png', image: base64_image }
}.to_json
)
@ -163,16 +175,6 @@ describe Whatsapp::Providers::WhatsappBaileysService do
end
end
context 'when message does not have content nor attachments' do
it 'updates the message status to failed' do
message.update!(content: nil)
service.send_message(test_send_phone_number, message)
expect(message.status).to eq('failed')
end
end
context 'when message is a text' do
it 'sends the message' do
stub_request(:post, "#{whatsapp_channel.provider_config['provider_url']}/connections/#{whatsapp_channel.phone_number}/send-message")
@ -200,17 +202,6 @@ describe Whatsapp::Providers::WhatsappBaileysService do
end.to raise_error(Whatsapp::Providers::WhatsappBaileysService::MessageNotSentError)
end
end
context "when message doesn't have attachment and content" do
it 'does not send the message' do
message.update!(content: nil)
service.send_message(test_send_phone_number, message)
expect(message.status).to eq('failed')
expect(message.content).to eq(I18n.t('errors.messages.send.unsupported'))
end
end
end
describe '#api_headers' do
@ -250,7 +241,7 @@ describe Whatsapp::Providers::WhatsappBaileysService do
"#{whatsapp_channel.provider_config['provider_url']}/connections/#{whatsapp_channel.phone_number}/send-message",
headers: stub_headers(whatsapp_channel),
body: {
recipient: test_send_phone_number,
jid: test_send_jid,
messageContent: { text: message.content }
}.to_json
).and_raise(HTTParty::ResponseError.new(OpenStruct.new(status_code: 500)))