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:
parent
fd8e099f4d
commit
8dc893d322
@ -32,13 +32,17 @@ class Whatsapp::Providers::WhatsappBaileysService < Whatsapp::Providers::BaseSer
|
|||||||
def send_message(phone_number, message)
|
def send_message(phone_number, message)
|
||||||
@message = message
|
@message = message
|
||||||
@phone_number = phone_number
|
@phone_number = phone_number
|
||||||
|
|
||||||
if message.attachments.present?
|
if message.attachments.present?
|
||||||
send_attachment_message
|
@message_content = attachment_message_content
|
||||||
elsif message.content.present?
|
elsif message.content.present?
|
||||||
send_text_message
|
@message_content = { text: @message.content }
|
||||||
else
|
else
|
||||||
message.update!(content: I18n.t('errors.messages.send.unsupported'), status: 'failed')
|
message.update!(content: I18n.t('errors.messages.send.unsupported'), status: 'failed')
|
||||||
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
|
send_message_request
|
||||||
end
|
end
|
||||||
|
|
||||||
def send_template(phone_number, template_info); 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
|
whatsapp_channel.provider_config['api_key'].presence || DEFAULT_API_KEY
|
||||||
end
|
end
|
||||||
|
|
||||||
def send_attachment_message
|
def attachment_message_content
|
||||||
@attachment = @message.attachments.first
|
attachment = @message.attachments.first
|
||||||
|
buffer = Base64.strict_encode64(attachment.file.download)
|
||||||
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)
|
|
||||||
|
|
||||||
content = {
|
content = {
|
||||||
fileName: @attachment.file.filename,
|
fileName: attachment.file.filename,
|
||||||
caption: @message.content
|
caption: @message.content
|
||||||
}
|
}
|
||||||
case @attachment.file_type
|
case attachment.file_type
|
||||||
when 'image'
|
when 'image'
|
||||||
content[:image] = buffer
|
content[:image] = buffer
|
||||||
when 'audio'
|
when 'audio'
|
||||||
@ -110,13 +98,13 @@ class Whatsapp::Providers::WhatsappBaileysService < Whatsapp::Providers::BaseSer
|
|||||||
content.compact
|
content.compact
|
||||||
end
|
end
|
||||||
|
|
||||||
def send_text_message
|
def send_message_request
|
||||||
response = HTTParty.post(
|
response = HTTParty.post(
|
||||||
"#{provider_url}/connections/#{whatsapp_channel.phone_number}/send-message",
|
"#{provider_url}/connections/#{whatsapp_channel.phone_number}/send-message",
|
||||||
headers: api_headers,
|
headers: api_headers,
|
||||||
body: {
|
body: {
|
||||||
recipient: @phone_number,
|
jid: "#{@phone_number.delete('+')}@s.whatsapp.net",
|
||||||
messageContent: { text: @message.content }
|
messageContent: @message_content
|
||||||
}.to_json
|
}.to_json
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@ -7,6 +7,7 @@ describe Whatsapp::Providers::WhatsappBaileysService do
|
|||||||
let(:message) { create(:message) }
|
let(:message) { create(:message) }
|
||||||
|
|
||||||
let(:test_send_phone_number) { '+5511987654321' }
|
let(:test_send_phone_number) { '+5511987654321' }
|
||||||
|
let(:test_send_jid) { '5511987654321@s.whatsapp.net' }
|
||||||
|
|
||||||
before do
|
before do
|
||||||
stub_const('Whatsapp::Providers::WhatsappBaileysService::DEFAULT_CLIENT_NAME', 'chatwoot-test')
|
stub_const('Whatsapp::Providers::WhatsappBaileysService::DEFAULT_CLIENT_NAME', 'chatwoot-test')
|
||||||
@ -91,6 +92,17 @@ describe Whatsapp::Providers::WhatsappBaileysService do
|
|||||||
end
|
end
|
||||||
|
|
||||||
describe '#send_message' do
|
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
|
context 'when message has attachment' do
|
||||||
let(:base64_image) { 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=' }
|
let(:base64_image) { 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=' }
|
||||||
|
|
||||||
@ -111,7 +123,7 @@ describe Whatsapp::Providers::WhatsappBaileysService do
|
|||||||
.with(
|
.with(
|
||||||
headers: stub_headers(whatsapp_channel),
|
headers: stub_headers(whatsapp_channel),
|
||||||
body: {
|
body: {
|
||||||
recipient: test_send_phone_number,
|
jid: test_send_jid,
|
||||||
messageContent: { fileName: 'image.png', caption: message.content, image: base64_image }
|
messageContent: { fileName: 'image.png', caption: message.content, image: base64_image }
|
||||||
}.to_json
|
}.to_json
|
||||||
)
|
)
|
||||||
@ -132,7 +144,7 @@ describe Whatsapp::Providers::WhatsappBaileysService do
|
|||||||
.with(
|
.with(
|
||||||
headers: stub_headers(whatsapp_channel),
|
headers: stub_headers(whatsapp_channel),
|
||||||
body: {
|
body: {
|
||||||
recipient: test_send_phone_number,
|
jid: test_send_jid,
|
||||||
messageContent: { fileName: 'image.png', image: base64_image }
|
messageContent: { fileName: 'image.png', image: base64_image }
|
||||||
}.to_json
|
}.to_json
|
||||||
)
|
)
|
||||||
@ -163,16 +175,6 @@ describe Whatsapp::Providers::WhatsappBaileysService do
|
|||||||
end
|
end
|
||||||
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
|
context 'when message is a text' do
|
||||||
it 'sends the message' do
|
it 'sends the message' do
|
||||||
stub_request(:post, "#{whatsapp_channel.provider_config['provider_url']}/connections/#{whatsapp_channel.phone_number}/send-message")
|
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.to raise_error(Whatsapp::Providers::WhatsappBaileysService::MessageNotSentError)
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
||||||
describe '#api_headers' do
|
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",
|
"#{whatsapp_channel.provider_config['provider_url']}/connections/#{whatsapp_channel.phone_number}/send-message",
|
||||||
headers: stub_headers(whatsapp_channel),
|
headers: stub_headers(whatsapp_channel),
|
||||||
body: {
|
body: {
|
||||||
recipient: test_send_phone_number,
|
jid: test_send_jid,
|
||||||
messageContent: { text: message.content }
|
messageContent: { text: message.content }
|
||||||
}.to_json
|
}.to_json
|
||||||
).and_raise(HTTParty::ResponseError.new(OpenStruct.new(status_code: 500)))
|
).and_raise(HTTParty::ResponseError.new(OpenStruct.new(status_code: 500)))
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user