Fix incoming Signal messages when conversation initiated from Zammad

This commit is contained in:
Darren Clarke 2025-07-25 22:39:07 +02:00
parent 46e1116bc5
commit 20ef676cf1
6 changed files with 27 additions and 11 deletions

View file

@ -213,11 +213,12 @@ class ChannelsCdrSignalController < ApplicationController
Rails.logger.info "Channel ID: #{channel.id}"
begin
all_tickets = Ticket.where(customer_id: customer.id)
.where.not(state_id: state_ids)
# For group messages, search all tickets regardless of customer
# since users may have duplicate phone numbers
all_tickets = Ticket.where.not(state_id: state_ids)
.order(updated_at: :desc)
Rails.logger.info "Found #{all_tickets.count} active tickets for customer"
Rails.logger.info "Found #{all_tickets.count} active tickets (searching all customers for group match)"
ticket = all_tickets.find do |t|
begin
@ -247,6 +248,11 @@ class ChannelsCdrSignalController < ApplicationController
if ticket
Rails.logger.info "=== FOUND MATCHING TICKET BY GROUP ID: ##{ticket.number} ==="
# Update customer if different (handles duplicate phone numbers)
if ticket.customer_id != customer.id
Rails.logger.info "Updating ticket customer from #{ticket.customer_id} to #{customer.id}"
ticket.customer_id = customer.id
end
else
Rails.logger.info "=== NO MATCHING TICKET BY GROUP ID - CHECKING BY PHONE NUMBER ==="
end

View file

@ -25,7 +25,8 @@ class CommunicateCdrSignalJob < ApplicationJob
log_error(article,
"Can't find ticket.preferences['cdr_signal']['bot_token'] for Ticket.find(#{article.ticket_id})")
end
unless ticket.preferences['cdr_signal']['chat_id']
# Only require chat_id if auto-groups is not enabled
if ENV['BRIDGE_SIGNAL_AUTO_GROUPS'].to_s.downcase != 'true' && ticket.preferences['cdr_signal']['chat_id'].blank?
log_error(article,
"Can't find ticket.preferences['cdr_signal']['chat_id'] for Ticket.find(#{article.ticket_id})")
end

View file

@ -7,6 +7,7 @@ Zammad::Application.routes.draw do
match "#{api_path}/channels_cdr_signal", to: 'channels_cdr_signal#add', via: :post
match "#{api_path}/channels_cdr_signal/:id", to: 'channels_cdr_signal#update', via: :put
match "#{api_path}/channels_cdr_signal_webhook/:token", to: 'channels_cdr_signal#webhook', via: :post
match "#{api_path}/channels_cdr_signal_webhook/:token/update_group", to: 'channels_cdr_signal#update_group', via: :post
match "#{api_path}/channels_cdr_signal_disable", to: 'channels_cdr_signal#disable', via: :post
match "#{api_path}/channels_cdr_signal_enable", to: 'channels_cdr_signal#enable', via: :post
match "#{api_path}/channels_cdr_signal", to: 'channels_cdr_signal#destroy', via: :delete

View file

@ -319,7 +319,14 @@ class CdrSignal
raise "No ticket found for article #{article.id}" unless ticket
recipient = ticket.preferences.dig('cdr_signal', 'chat_id')
raise "No Signal chat_id found in ticket preferences" unless recipient
# If auto-groups is enabled and no chat_id, use original_recipient
if recipient.blank? && ENV['BRIDGE_SIGNAL_AUTO_GROUPS'].to_s.downcase == 'true'
recipient = ticket.preferences.dig('cdr_signal', 'original_recipient')
raise "No Signal chat_id or original_recipient found in ticket preferences" unless recipient
elsif recipient.blank?
raise "No Signal chat_id found in ticket preferences"
end
Rails.logger.debug { "Sending to recipient: '#{recipient}'" }