134 lines
4.7 KiB
Ruby
134 lines
4.7 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
class CommunicateCdrSignalJob < ApplicationJob
|
|
retry_on StandardError, attempts: 4, wait: lambda { |executions|
|
|
executions * 120.seconds
|
|
}
|
|
|
|
def perform(article_id)
|
|
article = Ticket::Article.find(article_id)
|
|
|
|
# set retry count
|
|
article.preferences['delivery_retry'] ||= 0
|
|
article.preferences['delivery_retry'] += 1
|
|
|
|
ticket = Ticket.lookup(id: article.ticket_id)
|
|
unless ticket.preferences
|
|
log_error(article,
|
|
"Can't find ticket.preferences for Ticket.find(#{article.ticket_id})")
|
|
end
|
|
unless ticket.preferences['cdr_signal']
|
|
log_error(article,
|
|
"Can't find ticket.preferences['cdr_signal'] for Ticket.find(#{article.ticket_id})")
|
|
end
|
|
unless ticket.preferences['cdr_signal']['bot_token']
|
|
log_error(article,
|
|
"Can't find ticket.preferences['cdr_signal']['bot_token'] for Ticket.find(#{article.ticket_id})")
|
|
end
|
|
# 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
|
|
|
|
# Check if this is a group chat and if the user has joined
|
|
chat_id = ticket.preferences['cdr_signal']['chat_id']
|
|
is_group_chat = chat_id&.start_with?('group.')
|
|
group_joined = ticket.preferences.dig('cdr_signal', 'group_joined')
|
|
|
|
# If this is a group chat and user hasn't joined yet, don't send the message
|
|
if is_group_chat && group_joined == false
|
|
Rails.logger.info "Ticket ##{ticket.number}: User hasn't joined Signal group yet, skipping message delivery"
|
|
|
|
# Mark article as pending delivery
|
|
article.preferences['delivery_status'] = 'pending'
|
|
article.preferences['delivery_status_message'] = 'Waiting for user to join Signal group'
|
|
article.preferences['delivery_status_date'] = Time.zone.now
|
|
article.save!
|
|
|
|
# Retry later when user might have joined
|
|
raise 'User has not joined Signal group yet'
|
|
end
|
|
channel = ::CdrSignal.bot_by_bot_token(ticket.preferences['cdr_signal']['bot_token'])
|
|
channel ||= ::Channel.lookup(id: ticket.preferences['channel_id'])
|
|
unless channel
|
|
log_error(article,
|
|
"No such channel for bot #{ticket.preferences['cdr_signal']['bot_token']} or channel id #{ticket.preferences['channel_id']}")
|
|
end
|
|
if channel.options[:bot_token].blank?
|
|
log_error(article,
|
|
"Channel.find(#{channel.id}) has no cdr signal api token!")
|
|
end
|
|
|
|
has_error = false
|
|
|
|
begin
|
|
result = channel.deliver(article)
|
|
rescue StandardError => e
|
|
log_error(article, e.message)
|
|
has_error = true
|
|
end
|
|
|
|
Rails.logger.debug { "send result: #{result}" }
|
|
|
|
if result.nil? || result[:error].present?
|
|
log_error(article, 'Delivering signal message failed!')
|
|
has_error = true
|
|
end
|
|
|
|
return if has_error
|
|
|
|
article.to = result['result']['to']
|
|
article.from = result['result']['from']
|
|
|
|
message_id = format('%<from>s@%<timestamp>s', from: result['result']['from'],
|
|
timestamp: result['result']['timestamp'])
|
|
article.preferences['cdr_signal'] = {
|
|
timestamp: result['result']['timestamp'],
|
|
message_id: message_id,
|
|
from: result['result']['from'],
|
|
to: result['result']['to']
|
|
}
|
|
|
|
# set delivery status
|
|
article.preferences['delivery_status_message'] = nil
|
|
article.preferences['delivery_status'] = 'success'
|
|
article.preferences['delivery_status_date'] = Time.zone.now
|
|
|
|
article.message_id = "cdr_signal.#{message_id}"
|
|
|
|
article.save!
|
|
|
|
Rails.logger.info "Sent signal message to: '#{article.to}' (from #{article.from})"
|
|
|
|
article
|
|
end
|
|
|
|
def log_error(local_record, message)
|
|
local_record.preferences['delivery_status'] = 'fail'
|
|
local_record.preferences['delivery_status_message'] =
|
|
message.encode!('UTF-8', 'UTF-8', invalid: :replace, replace: '?')
|
|
local_record.preferences['delivery_status_date'] = Time.zone.now
|
|
local_record.save
|
|
Rails.logger.error message
|
|
|
|
if local_record.preferences['delivery_retry'] > 3
|
|
Ticket::Article.create(
|
|
ticket_id: local_record.ticket_id,
|
|
content_type: 'text/plain',
|
|
body: "Unable to send cdr signal message: #{message}",
|
|
internal: true,
|
|
sender: Ticket::Article::Sender.find_by(name: 'System'),
|
|
type: Ticket::Article::Type.find_by(name: 'note'),
|
|
preferences: {
|
|
delivery_article_id_related: local_record.id,
|
|
delivery_message: true
|
|
},
|
|
updated_by_id: 1,
|
|
created_by_id: 1
|
|
)
|
|
end
|
|
|
|
raise message
|
|
end
|
|
end
|