Add user ID support for Baileys 7 LIDs and Signal UUIDs
Baileys 7 uses LIDs (Linked IDs) instead of phone numbers in remoteJid for some messages. This caused messages to be matched to wrong tickets because the LID was used as the sender identifier. This commit adds proper support for both phone numbers and user IDs across WhatsApp and Signal channels. Changes: Database: - Add migration for whatsapp_user_id and signal_user_id fields on users table Zammad controllers: - Update user lookup with 3-step fallback: phone → dedicated user_id field → user_id in phone field (legacy) - Store user IDs in dedicated fields when available - Update phone field when we receive actual phone number for legacy records - Fix redundant condition in Signal controller Bridge services: - Extract both phone (from senderPn/participantPn) and LID (from remoteJid) - Send both identifiers to Zammad via webhooks - Use camelCase (userId) in bridge-whatsapp, convert to snake_case (user_id) in bridge-worker for Zammad compatibility Baileys 7 compliance: - Remove broken loadAllUnreadMessages() call (removed in Baileys 7) - Return descriptive error directing users to use webhooks instead Misc: - Add docs/ to .gitignore
This commit is contained in:
parent
57d7173485
commit
3d8f794cab
8 changed files with 154 additions and 36 deletions
|
|
@ -123,12 +123,16 @@ class ChannelsCdrWhatsappController < ApplicationController
|
|||
errors = {}
|
||||
|
||||
%i[to
|
||||
from
|
||||
message_id
|
||||
sent_at].each do |field|
|
||||
errors[field] = 'required' if params[field].blank?
|
||||
end
|
||||
|
||||
# At least one of from (phone) or user_id must be present
|
||||
if params[:from].blank? && params[:user_id].blank?
|
||||
errors[:from] = 'required (or user_id)'
|
||||
end
|
||||
|
||||
if errors.present?
|
||||
render json: {
|
||||
errors: errors
|
||||
|
|
@ -141,9 +145,25 @@ class ChannelsCdrWhatsappController < ApplicationController
|
|||
return if Ticket::Article.exists?(message_id: "cdr_whatsapp.#{message_id}")
|
||||
|
||||
receiver_phone_number = params[:to].strip
|
||||
sender_phone_number = params[:from].strip
|
||||
customer = User.find_by(phone: sender_phone_number)
|
||||
customer ||= User.find_by(mobile: sender_phone_number)
|
||||
sender_phone_number = params[:from].present? ? params[:from].strip : nil
|
||||
sender_user_id = params[:user_id].present? ? params[:user_id].strip : nil
|
||||
|
||||
# Lookup customer with fallback chain:
|
||||
# 1. Phone number in phone/mobile fields (preferred)
|
||||
# 2. WhatsApp user ID in whatsapp_user_id field
|
||||
# 3. User ID in phone/mobile fields (legacy - we used to store LIDs there)
|
||||
customer = nil
|
||||
if sender_phone_number.present?
|
||||
customer = User.find_by(phone: sender_phone_number)
|
||||
customer ||= User.find_by(mobile: sender_phone_number)
|
||||
end
|
||||
if customer.nil? && sender_user_id.present?
|
||||
customer = User.find_by(whatsapp_user_id: sender_user_id)
|
||||
# Legacy fallback: user ID might be stored in phone field
|
||||
customer ||= User.find_by(phone: sender_user_id)
|
||||
customer ||= User.find_by(mobile: sender_user_id)
|
||||
end
|
||||
|
||||
unless customer
|
||||
role_ids = Role.signup_role_ids
|
||||
customer = User.create(
|
||||
|
|
@ -151,7 +171,8 @@ class ChannelsCdrWhatsappController < ApplicationController
|
|||
lastname: '',
|
||||
email: '',
|
||||
password: '',
|
||||
phone: sender_phone_number,
|
||||
phone: sender_phone_number.presence || sender_user_id,
|
||||
whatsapp_user_id: sender_user_id,
|
||||
note: 'CDR Whatsapp',
|
||||
active: true,
|
||||
role_ids: role_ids,
|
||||
|
|
@ -160,6 +181,15 @@ class ChannelsCdrWhatsappController < ApplicationController
|
|||
)
|
||||
end
|
||||
|
||||
# Update whatsapp_user_id if we have it and customer doesn't
|
||||
if sender_user_id.present? && customer.whatsapp_user_id.blank?
|
||||
customer.update(whatsapp_user_id: sender_user_id)
|
||||
end
|
||||
# Update phone if we have it and customer only has user_id in phone field
|
||||
if sender_phone_number.present? && customer.phone == sender_user_id
|
||||
customer.update(phone: sender_phone_number)
|
||||
end
|
||||
|
||||
# set current user
|
||||
UserInfo.current_user_id = customer.id
|
||||
current_user_set(customer, 'token_auth')
|
||||
|
|
@ -188,7 +218,8 @@ class ChannelsCdrWhatsappController < ApplicationController
|
|||
attachment_data_base64 = params[:attachment]
|
||||
attachment_filename = params[:filename]
|
||||
attachment_mimetype = params[:mime_type]
|
||||
title = "Message from #{sender_phone_number} at #{sent_at}"
|
||||
sender_display = sender_phone_number.presence || sender_user_id
|
||||
title = "Message from #{sender_display} at #{sent_at}"
|
||||
body = message
|
||||
|
||||
# find ticket or create one
|
||||
|
|
@ -207,8 +238,9 @@ class ChannelsCdrWhatsappController < ApplicationController
|
|||
preferences: {
|
||||
channel_id: channel.id,
|
||||
cdr_whatsapp: {
|
||||
bot_token: channel.options[:bot_token], # change to bot id
|
||||
chat_id: sender_phone_number
|
||||
bot_token: channel.options[:bot_token],
|
||||
chat_id: sender_phone_number.presence || sender_user_id,
|
||||
user_id: sender_user_id
|
||||
}
|
||||
}
|
||||
)
|
||||
|
|
@ -217,7 +249,7 @@ class ChannelsCdrWhatsappController < ApplicationController
|
|||
ticket.save!
|
||||
|
||||
article_params = {
|
||||
from: sender_phone_number,
|
||||
from: sender_display,
|
||||
to: receiver_phone_number,
|
||||
sender_id: Ticket::Article::Sender.find_by(name: 'Customer').id,
|
||||
subject: title,
|
||||
|
|
@ -230,7 +262,8 @@ class ChannelsCdrWhatsappController < ApplicationController
|
|||
cdr_whatsapp: {
|
||||
timestamp: sent_at,
|
||||
message_id: message_id,
|
||||
from: sender_phone_number
|
||||
from: sender_phone_number,
|
||||
user_id: sender_user_id
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue