iachat/spec/services/whatsapp/incoming_message_baileys_service_spec.rb
Cayo P. R. Oliveira 322f0d1922
feat: use message is unsupported content attribute (#44)
* refactor: simplify message_type method using a hash for message types

* refactor: streamline message handling and update unsupported message logic

* refactor: simplify create_message method by inlining sender_type and message_type assignments

* refactor: simplify message_type method for improved readability and performance

* feat: allow is_recorded_audio as boolean

* chore: refactor create message + attach media logic

* fix: unsupported message templates

---------

Co-authored-by: gabrieljablonski <contact@gabrieljablonski.com>
2025-05-09 10:52:07 -03:00

783 lines
32 KiB
Ruby

require 'rails_helper'
describe Whatsapp::IncomingMessageBaileysService do
describe '#perform' do
let(:webhook_verify_token) { 'valid_token' }
let!(:whatsapp_channel) do
create(:channel_whatsapp,
provider: 'baileys',
provider_config: { webhook_verify_token: webhook_verify_token },
validate_provider_config: false)
end
let(:inbox) { whatsapp_channel.inbox }
context 'when webhook verify token is invalid' do
it 'raises an InvalidWebhookVerifyToken error' do
params = { webhookVerifyToken: 'invalid_token' }
expect do
described_class.new(inbox: inbox, params: params).perform
end.to raise_error(Whatsapp::IncomingMessageBaileysService::InvalidWebhookVerifyToken)
end
end
context 'when event is blank' do
it 'returns early and does nothing' do
params = {
webhookVerifyToken: webhook_verify_token,
event: '',
data: { connection: 'open' }
}
service = described_class.new(inbox: inbox, params: params)
allow(service).to receive(:respond_to?)
service.perform
expect(service).not_to have_received(:respond_to?)
end
end
context 'when data is blank' do
it 'returns early and does nothing' do
params = {
webhookVerifyToken: webhook_verify_token,
event: 'connection.update',
data: {}
}
service = described_class.new(inbox: inbox, params: params)
allow(service).to receive(:respond_to?)
service.perform
expect(service).not_to have_received(:respond_to?)
end
end
context 'when event is unsupported' do
it 'logs a warning message' do
params = {
webhookVerifyToken: webhook_verify_token,
event: 'unsupported.event',
data: 'some-data'
}
allow(Rails.logger).to receive(:warn).with('Baileys unsupported event: unsupported.event')
described_class.new(inbox: inbox, params: params).perform
expect(Rails.logger).to have_received(:warn)
end
end
context 'when processing connection.update event' do
let(:base_params) { { webhookVerifyToken: webhook_verify_token, event: 'connection.update' } }
it 'updates the channel provider_connection' do
params = base_params.merge(
{
data: {
connection: 'open',
qrDataUrl: 'data:image/jpeg;base64,',
error: 'wrong_phone_number'
}
}
)
described_class.new(inbox: inbox, params: params).perform
expect(inbox.channel.provider_connection).to include(
'connection' => 'open',
'qr_data_url' => 'data:image/jpeg;base64,',
'error' => I18n.t('errors.inboxes.channel.provider_connection.wrong_phone_number')
)
end
it "logs an error message if there's an error" do
params = base_params.merge(
{
data: { error: 'wrong_phone_number' }
}
)
allow(Rails.logger).to receive(:error).with('Baileys connection error: wrong_phone_number')
described_class.new(inbox: inbox, params: params).perform
expect(Rails.logger).to have_received(:error)
end
it "keeps connection value if it's not present in the data" do
inbox.channel.update_provider_connection!(connection: 'connecting')
params = base_params.merge(
{
data: { qrDataUrl: 'data:image/jpeg;base64,' }
}
)
described_class.new(inbox: inbox, params: params).perform
expect(inbox.channel.provider_connection['connection']).to eq('connecting')
end
it "removes qr_data_url value if it's not present in the data" do
inbox.channel.update_provider_connection!(qr_data_url: 'data:image/jpeg;base64,')
params = base_params.merge(
{
data: { connection: 'open' }
}
)
described_class.new(inbox: inbox, params: params).perform
expect(inbox.channel.provider_connection['qr_data_url']).to be_nil
end
it "removes error value if it's not present in the data" do
inbox.channel.update_provider_connection!(error: 'wrong_phone_number')
params = base_params.merge(
{
data: { connection: 'open' }
}
)
described_class.new(inbox: inbox, params: params).perform
expect(inbox.channel.provider_connection['error']).to be_nil
end
end
context 'when processing messages.upsert event' do
context 'when message type is unsupported' do
let(:raw_message) do
{
key: { id: 'msg_123', remoteJid: '5511912345678@s.whatsapp.net', fromMe: false },
message: { unsupported: 'message' },
pushName: 'John Doe'
}
end
let(:params) do
{
webhookVerifyToken: webhook_verify_token,
event: 'messages.upsert',
data: {
type: 'notify',
messages: [raw_message]
}
}
end
it 'creates an unsupported message' do
described_class.new(inbox: inbox, params: params).perform
conversation = inbox.conversations.last
message = conversation.messages.last
expect(message).to be_present
expect(message.is_unsupported).to be(true)
end
end
context 'when message is protocol message' do
let(:raw_message) do
{ key: { id: 'msg_123', remoteJid: '5511912345678@s.whatsapp.net', fromMe: false },
message: { protocolMessage: { type: 1 } },
pushName: 'John Doe' }
end
let(:params) do
{ webhookVerifyToken: webhook_verify_token,
event: 'messages.upsert',
data: {
type: 'notify',
messages: [raw_message]
} }
end
it 'does not create contact inbox nor message' do
described_class.new(inbox: inbox, params: params).perform
expect(inbox.contact_inboxes.count).to be(0)
expect(inbox.messages.count).to be(0)
end
end
context 'when message is not from a user' do
let(:raw_message) do
{
key: { id: 'msg_123', remoteJid: 'status@broadcast', participant: '5511912345678@s.whatsapp.net', fromMe: false },
message: { extendedTextMessage: { text: 'message' } },
pushName: 'John Doe'
}
end
let(:params) do
{
webhookVerifyToken: webhook_verify_token,
event: 'messages.upsert',
data: {
type: 'notify',
messages: [raw_message]
}
}
end
it 'does not create a conversation' do
described_class.new(inbox: inbox, params: params).perform
expect(inbox.conversations).to be_empty
end
end
context 'when message type is text' do
let(:phone_number) { '5511912345678' }
let(:jid) { "#{phone_number}@s.whatsapp.net" }
let(:content_message) { 'Hello from Baileys' }
let(:raw_message) do
{
key: { id: 'msg_123', remoteJid: jid, fromMe: false },
pushName: 'John Doe',
message: { conversation: content_message }
}
end
let(:params) do
{
webhookVerifyToken: webhook_verify_token,
event: 'messages.upsert',
data: {
type: 'notify',
messages: [raw_message]
}
}
end
context 'when has key conversation' do # rubocop:disable RSpec/NestedGroups
it 'creates an incoming message' do
described_class.new(inbox: inbox, params: params).perform
conversation = inbox.conversations.last
message = conversation.messages.last
expect(message).to be_present
expect(message.content).to eq(content_message)
expect(message.sender).to be_present
expect(message.sender.name).to eq('John Doe')
expect(message.message_type).to eq('incoming')
end
it 'creates an outgoing message' do
raw_message_outgoing = raw_message.merge(key: { id: 'msg_123', remoteJid: jid, fromMe: true })
params_outgoing = params.merge(data: { type: 'notify', messages: [raw_message_outgoing] })
create(:account_user, account: inbox.account)
described_class.new(inbox: inbox, params: params_outgoing).perform
conversation = inbox.conversations.last
message = conversation.messages.last
expect(message).to be_present
expect(message.content).to eq(content_message)
expect(conversation.contact.name).to eq(phone_number)
expect(message.message_type).to eq('outgoing')
end
it 'creates an outgoing self message' do
self_jid = "#{whatsapp_channel.phone_number.delete('+')}@s.whatsapp.net"
raw_message_outgoing = raw_message.merge(key: { id: 'msg_123', remoteJid: self_jid, fromMe: true })
params_outgoing = params.merge(data: { type: 'notify', messages: [raw_message_outgoing] })
create(:account_user, account: inbox.account)
described_class.new(inbox: inbox, params: params_outgoing).perform
conversation = inbox.conversations.last
message = conversation.messages.last
expect(message).to be_present
expect(message.content).to eq(content_message)
expect(conversation.contact.name).to eq('John Doe')
expect(message.message_type).to eq('outgoing')
end
it 'updates the contact name when name is the phone number and message has a pushName' do
create(:contact, account: inbox.account, name: phone_number)
described_class.new(inbox: inbox, params: params).perform
conversation = inbox.conversations.last
expect(conversation.contact.name).to eq('John Doe')
end
it 'updates the contact name when name is the phone number and message has a verifiedBizName' do
raw_message[:verifiedBizName] = 'Verified John'
create(:contact, account: inbox.account, name: phone_number)
described_class.new(inbox: inbox, params: params).perform
conversation = inbox.conversations.last
expect(conversation.contact.name).to eq('Verified John')
end
it 'creates contact with phone number as name on outgoing message' do
raw_message_outgoing = raw_message.merge(key: { id: 'msg_123', remoteJid: jid, fromMe: true })
params_outgoing = params.merge(data: { type: 'notify', messages: [raw_message_outgoing] })
create(:account_user, account: inbox.account)
described_class.new(inbox: inbox, params: params_outgoing).perform
conversation = inbox.conversations.last
expect(conversation.contact.name).to eq(phone_number)
end
it 'creates a message on an existing conversation' do
contact = create(:contact, account: inbox.account, name: 'John Doe')
contact_inbox = create(:contact_inbox, inbox: inbox, contact: contact, source_id: '5511912345678')
existing_conversation = create(:conversation, inbox: inbox, contact_inbox: contact_inbox)
described_class.new(inbox: inbox, params: params).perform
message = existing_conversation.messages.last
expect(message.sender).to eq(contact)
end
it 'does not create a message if it already exists' do
message = create(:message, inbox: inbox, source_id: 'msg_123')
described_class.new(inbox: inbox, params: params).perform
conversation = inbox.conversations.last
messages = conversation.messages
expect(messages).to eq([message])
end
it 'does not create a message if it is already being processed' do
allow(Redis::Alfred).to receive(:get).with(format_message_source_key('msg_123')).and_return(true)
described_class.new(inbox: inbox, params: params).perform
expect(inbox.conversations).to be_empty
end
it 'caches the message source id in Redis and clears it' do
allow(Redis::Alfred).to receive(:setex).with(format_message_source_key('msg_123'), true)
allow(Redis::Alfred).to receive(:delete).with(format_message_source_key('msg_123'))
described_class.new(inbox: inbox, params: params).perform
expect(Redis::Alfred).to have_received(:setex)
expect(Redis::Alfred).to have_received(:delete)
end
end
context 'when is a extendedTextMessage that has key text' do # rubocop:disable RSpec/NestedGroups
let(:raw_message) do
{
key: { id: 'msg_123', remoteJid: '5511912345678@s.whatsapp.net', fromMe: false },
message: { extendedTextMessage: { text: 'Hello from Baileys' } },
pushName: 'John Doe'
}
end
it 'creates an incoming message' do
described_class.new(inbox: inbox, params: params).perform
conversation = inbox.conversations.last
message = conversation.messages.last
expect(message).to be_present
expect(message.content).to eq('Hello from Baileys')
expect(message.message_type).to eq('incoming')
expect(message.sender).to be_present
expect(message.sender.name).to eq('John Doe')
end
end
end
context 'when message type is reaction' do
let(:phone_number) { '5511912345678' }
let(:jid) { "#{phone_number}@s.whatsapp.net" }
let(:raw_message) do
{
key: { id: 'reaction_123', remoteJid: jid, fromMe: false },
message: { reactionMessage: { key: { remoteJid: jid, fromMe: true, id: 'msg_123' }, text: '👍' } },
pushName: 'John Doe'
}
end
let(:params) do
{
webhookVerifyToken: webhook_verify_token,
event: 'messages.upsert',
data: {
type: 'notify',
messages: [raw_message]
}
}
end
let!(:message) do
contact = create(:contact, account: inbox.account, name: phone_number)
contact_inbox = create(:contact_inbox, inbox: inbox, contact: contact, source_id: phone_number)
conversation = create(:conversation, inbox: inbox, contact_inbox: contact_inbox)
create(:message, inbox: inbox, conversation: conversation, source_id: 'msg_123')
end
it 'creates the reaction' do
described_class.new(inbox: inbox, params: params).perform
reaction = message.conversation.messages.last
expect(reaction.is_reaction).to be(true)
expect(reaction.in_reply_to).to eq(message.id)
expect(reaction.in_reply_to_external_id).to eq(message.source_id)
end
it 'does not create the reaction if content is empty' do
raw_message[:message][:reactionMessage][:text] = ''
described_class.new(inbox: inbox, params: params).perform
expect(message.conversation.messages.count).to eq(1)
end
end
context 'when message type is image' do
let(:raw_message) do
{
key: { id: 'msg_123', remoteJid: '5511912345678@s.whatsapp.net', fromMe: false },
message: { imageMessage: { caption: 'Hello from Baileys', mimetype: 'image/png' } },
pushName: 'John Doe'
}
end
let(:params) do
{
webhookVerifyToken: webhook_verify_token,
event: 'messages.upsert',
data: {
type: 'notify',
messages: [raw_message]
},
extra: {
media: {
'msg_123' => 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII='
}
}
}
end
it 'creates the message with caption' do
described_class.new(inbox: inbox, params: params).perform
conversation = inbox.conversations.last
message = conversation.messages.last
expect(message).to be_present
expect(message.content).to eq('Hello from Baileys')
end
it 'creates message attachment' do
freeze_time
described_class.new(inbox: inbox, params: params).perform
conversation = inbox.conversations.last
message = conversation.messages.last
attachment = message.attachments.last
expect(attachment).to be_present
expect(attachment.file).to be_present
expect(attachment.file_type).to eq('image')
expect(attachment.file.filename.to_s).to eq("image_msg_123_#{Time.current.strftime('%Y%m%d')}.png")
expect(attachment.file.content_type).to eq('image/png')
end
end
context 'when message type is video' do
let(:raw_message) do
{
key: { id: 'msg_123', remoteJid: '5511912345678@s.whatsapp.net', fromMe: false },
message: { videoMessage: { caption: 'Hello from Baileys', mimetype: 'video/mp4' } },
pushName: 'John Doe'
}
end
let(:params) do
{
webhookVerifyToken: webhook_verify_token,
event: 'messages.upsert',
data: {
type: 'notify',
messages: [raw_message]
},
extra: {
media: {
'msg_123' => 'AAAAHGZ0eXBpc29tAAACAGlzb21pc28ybXA0MQAAAAhmcmVlAA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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' # rubocop:disable Layout/LineLength
}
}
}
end
it 'creates the message with caption' do
described_class.new(inbox: inbox, params: params).perform
conversation = inbox.conversations.last
message = conversation.messages.last
expect(message).to be_present
expect(message.content).to eq('Hello from Baileys')
end
it 'creates message attachment' do
freeze_time
described_class.new(inbox: inbox, params: params).perform
conversation = inbox.conversations.last
message = conversation.messages.last
attachment = message.attachments.last
expect(attachment).to be_present
expect(attachment.file).to be_present
expect(attachment.file_type).to eq('video')
expect(attachment.file.filename.to_s).to eq("video_msg_123_#{Time.current.strftime('%Y%m%d')}.mp4")
expect(attachment.file.content_type).to eq('video/mp4')
end
end
context 'when message type is file' do
let(:filename) { 'file.pdf' }
let(:raw_message) do
{
key: { id: 'msg_123', remoteJid: '5511912345678@s.whatsapp.net', fromMe: false },
message: { documentMessage: { fileName: filename } },
pushName: 'John Doe'
}
end
let(:params) do
{
webhookVerifyToken: webhook_verify_token,
event: 'messages.upsert',
data: {
type: 'notify',
messages: [raw_message]
},
extra: {
media: {
'msg_123' => 'JVBERi0xLjQKJVRlc3QgUERGCjEgMCBvYmoKPDwgL1R5cGUgL0NhdGFsb2cgL091dGxpbmVzIDIgMCBSIC9QYWdlcyAzIDAgUiA+PgplbmRvYmoKMiAwIG9iago8PCAvVHlwZSAvT3V0bGluZXMgL0NvdW50IDAgPj4KZW5kb2JqCjMgMCBvYmoKPDwgL1R5cGUgL1BhZ2VzIC9LaWRzIFs0IDAgUl0gL0NvdW50IDEgPj4KZW5kb2JqCjQgMCBvYmoKPDwgL1R5cGUgL1BhZ2UgCiAgIC9QYXJlbnQgMyAwIFIgCiAgIC9NZWRpYUJveCBbMCAwIDMwMCAyMDBdIAogICAvQ29udGVudHMgNSAwIFIgCiAgIC9SZXNvdXJjZXMgPDwgL0ZvbnQgPDwgL0YxIDw8IC9UeXBlIC9Gb250IC9TdWJ0eXBlIC9UeXBlMSAvQmFzZUZvbnQgL0hlbHZldGljYSA+PiA+PiA+PgplbmRvYmoKNSAwIG9iago8PCAvTGVuZ3RoIDUzID4+CnN0cmVhbQpCVAovRjEgMTIgVGYKNzIgMTUwIFRkCihIZWxsbyBQREYhKSBUagpFVAplbmRzdHJlYW0KZW5kb2JqCnhyZWYKMCA2CjAwMDAwMDAwMDAgNjU1MzUgZiAKMDAwMDAwMDAxNyAwMDAwMCBuIAowMDAwMDAwMDg0IDAwMDAwIG4gCjAwMDAwMDAxMzQgMDAwMDAgbiAKMDAwMDAwMDE5MyAwMDAwMCBuIAowMDAwMDAwMzkxIDAwMDAwIG4gCnRyYWlsZXIKPDwgL1NpemUgNiAvUm9vdCAxIDAgUiA+PgpzdGFydHhyZWYKNDk0CiUlRU9GCg==' # rubocop:disable Layout/LineLength
}
}
}
end
it 'creates message attachment' do
described_class.new(inbox: inbox, params: params).perform
conversation = inbox.conversations.last
message = conversation.messages.last
attachment = message.attachments.last
expect(attachment).to be_present
expect(attachment.file_type).to eq('file')
expect(attachment.file).to be_present
expect(attachment.file.filename.to_s).to eq(filename)
expect(attachment.file.content_type).to eq('application/pdf')
end
end
context 'when message type is audio' do
let(:raw_message) do
{
key: { id: 'msg_123', remoteJid: '5511912345678@s.whatsapp.net', fromMe: false },
message: { audioMessage: { mimetype: 'audio/opus' } },
pushName: 'John Doe'
}
end
let(:params) do
{
webhookVerifyToken: webhook_verify_token,
event: 'messages.upsert',
data: {
type: 'notify',
messages: [raw_message]
},
extra: {
media: {
'msg_123' => 'T2dnUwACAAAAAAAAAAAAAAAAAAAAACqCBoIBE09wdXNIZWFkAQFoAIA+AAAAAABPZ2dTAAAAAAAAAAAAAAAAAAABAAAAjzLsvAEYT3B1c1RhZ3MIAAAAV2hhdHNBcHAAAAAAT2dnUwAAqOwAAAAAAAAAAAAAAgAAAJ5agxUOav8T7PLg/wTu9t3/DodLhgcIBwcaC+TBNuzFgAfJcifhROpQB8l5yMlXwAfJecjJUbCAAt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rZA==' # rubocop:disable Layout/LineLength
}
}
}
end
it 'creates message attachment' do
freeze_time
described_class.new(inbox: inbox, params: params).perform
conversation = inbox.conversations.last
message = conversation.messages.last
attachment = message.attachments.last
expect(attachment).to be_present
expect(attachment.file_type).to eq('audio')
expect(attachment.file).to be_present
expect(attachment.file.filename.to_s).to eq("audio_msg_123_#{Time.current.strftime('%Y%m%d')}.opus")
expect(attachment.file.content_type).to eq('audio/opus')
end
end
context 'when message type is sticker' do
let(:raw_message) do
{
key: { id: 'msg_123', remoteJid: '5511912345678@s.whatsapp.net', fromMe: false },
message: { stickerMessage: { mimetype: 'image/png' } },
pushName: 'John Doe'
}
end
let(:params) do
{
webhookVerifyToken: webhook_verify_token,
event: 'messages.upsert',
data: {
type: 'notify',
messages: [raw_message]
},
extra: {
media: {
'msg_123' => 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII='
}
}
}
end
it 'creates message attachment' do
freeze_time
described_class.new(inbox: inbox, params: params).perform
conversation = inbox.conversations.last
message = conversation.messages.last
attachment = message.attachments.last
expect(attachment).to be_present
expect(attachment.file_type).to eq('image')
expect(attachment.file).to be_present
expect(attachment.file.filename.to_s).to eq("image_msg_123_#{Time.current.strftime('%Y%m%d')}.png")
expect(attachment.file.content_type).to eq('image/png')
end
end
end
context 'when processing messages.update event' do
context 'when message is not found' do
let(:message_id) { 'msg_123' }
let(:update_payload) do
{
key: { id: message_id },
update: {
status: 2
}
}
end
it 'raises MessageNotFoundError' do
params = {
webhookVerifyToken: webhook_verify_token,
event: 'messages.update',
data: [update_payload]
}
expect do
described_class.new(inbox: inbox, params: params).perform
end.to raise_error(Whatsapp::IncomingMessageBaileysService::MessageNotFoundError)
end
end
context 'when message is found' do
let(:message_id) { 'msg_123' }
let!(:message) { create(:message, source_id: message_id, status: 'sent') }
it 'updates the message status' do
update_payload = { key: { id: message_id }, update: { status: 3 } }
params = {
webhookVerifyToken: webhook_verify_token,
event: 'messages.update',
data: [update_payload]
}
described_class.new(inbox: inbox, params: params).perform
expect(message.reload.status).to eq('delivered')
end
it 'updates the message content' do
update_payload = {
key: { id: message_id },
update: {
message: { editedMessage: { message: { conversation: 'New message content' } } }
}
}
params = {
webhookVerifyToken: webhook_verify_token,
event: 'messages.update',
data: [update_payload]
}
described_class.new(inbox: inbox, params: params).perform
original_content = message.content
expect(message.reload.content).to eq('New message content')
expect(message.is_edited).to be(true)
expect(message.previous_content).to eq(original_content)
end
end
context 'when the status transition is not allowed (message already read)' do
let(:message_id) { 'msg_123' }
let!(:message) { create(:message, source_id: message_id, status: 'read') }
it 'does not update the status' do
update_payload = { key: { id: message_id }, update: { status: 3 } }
params = {
webhookVerifyToken: webhook_verify_token,
event: 'messages.update',
data: [update_payload]
}
described_class.new(inbox: inbox, params: params).perform
expect(message.reload.status).to eq('read')
end
end
context 'when update unsupported status' do
let(:message_id) { 'msg_123' }
let!(:message) { create(:message, source_id: message_id) } # rubocop:disable RSpec/LetSetup
it 'logs warning for unsupported played status' do
update_payload = { key: { id: message_id }, update: { status: 5 } }
params = {
webhookVerifyToken: webhook_verify_token,
event: 'messages.update',
data: [update_payload]
}
allow(Rails.logger).to receive(:warn).with('Baileys unsupported message update status: PLAYED(5)')
described_class.new(inbox: inbox, params: params).perform
expect(Rails.logger).to have_received(:warn)
end
it 'logs warning for unsupported status' do
update_payload = { key: { id: message_id }, update: { status: 6 } }
params = {
webhookVerifyToken: webhook_verify_token,
event: 'messages.update',
data: [update_payload]
}
allow(Rails.logger).to receive(:warn).with('Baileys unsupported message update status: 6')
described_class.new(inbox: inbox, params: params).perform
expect(Rails.logger).to have_received(:warn)
end
end
end
end
def format_message_source_key(message_id)
format(Redis::RedisKeys::MESSAGE_SOURCE_KEY, id: message_id)
end
end