0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-11 02:59:07 +08:00
discourse/plugins/automation/lib/discourse_automation/scripts/auto_responder.rb
Régis Hanol 6a79b59ac8
FEATURE: Track automation errors and improve logging (#36938)
When automations fail silently (e.g., PM sending blocked due to
validation errors), admins have no visibility into what's going wrong.
This leaves them guessing why their automations aren't working as
expected.

This change addresses the problem in three ways:

1. Error tracking in automation stats

The Stat model now tracks `total_errors` alongside existing run metrics.
When an automation raises an exception during execution, it's caught and
counted. This gives admins visibility into which automations are
experiencing problems.

2. Centralized logging module

Created `DiscourseAutomation::Logger` to replace scattered
`Rails.logger.warn("[discourse-automation] ...")` calls throughout the
codebase. This eliminates the fragile manual prefix pattern and ensures
consistent log formatting across all automation code. All existing
logging calls have been migrated to use this module.

3. Error count display in admin UI

The automation list now shows recent error counts next to run
statistics. Clicking the error count links directly to `/logs` filtered
for discourse-automation entries, making it easy to investigate
failures. The link uses `data-auto-route="true"` to ensure proper
navigation outside Ember's client-side router.

<img width="1397" height="1041" alt="2026-01-01 @ 23 03 34"
src="https://github.com/user-attachments/assets/6a6733f1-999c-44de-ab04-78dd764a1a43"
/>

Ref - https://meta.discourse.org/t/389922
2026-01-19 18:50:13 +01:00

90 lines
2.6 KiB
Ruby
Vendored

# frozen_string_literal: true
DiscourseAutomation::Scriptable.add(DiscourseAutomation::Scripts::AUTO_RESPONDER) do
field :word_answer_list, component: :"key-value", accepts_placeholders: true
field :answering_user, component: :user
field :once, component: :boolean
version 1
triggerables %i[post_created_edited pm_created]
placeholder :sender_username
placeholder :word
script do |context, fields, automation|
key = DiscourseAutomation::AUTO_RESPONDER_TRIGGERED_IDS
answering_username = fields.dig("answering_user", "value") || Discourse.system_user.username
placeholders = { sender_username: answering_username }
post = context["post"]
next if !post.topic
next if fields.dig("once", "value") && post.topic.custom_fields[key]&.include?(automation.id)
answers = Set.new
word_answer_list_json = fields.dig("word_answer_list", "value")
next if word_answer_list_json.blank?
word_answer_list = JSON.parse(word_answer_list_json)
next if word_answer_list.blank?
word_answer_list.each do |word_answer_pair|
if word_answer_pair["key"].blank?
answers.add(word_answer_pair)
next
end
if post.is_first_post?
if match = post.topic.title.match(/\b(#{word_answer_pair["key"]})\b/i)
word_answer_pair["key"] = match.captures.first
answers.add(word_answer_pair)
next
end
end
if match = post.raw.match(/\b(#{word_answer_pair["key"]})\b/i)
word_answer_pair["key"] = match.captures.first
answers.add(word_answer_pair)
end
end
next if answers.blank?
answering_user = User.find_by(username: answering_username)
next if post.user == answering_user
replies =
post
.replies
.where(user_id: answering_user.id, deleted_at: nil)
.secured(Guardian.new(post.user))
next if replies.present?
answers =
answers
.to_a
.map do |answer|
utils.apply_placeholders(answer["value"], placeholders.merge(key: answer["key"]))
end
.join("\n\n")
automation.add_id_to_custom_field(post.topic, key)
creator =
PostCreator.new(
answering_user,
topic_id: post.topic.id,
reply_to_post_number: post.post_number,
raw: answers,
skip_validations: true,
)
result = creator.create
if result.blank? || creator.errors.present?
DiscourseAutomation::Logger.error(
"Failed to create auto-response in topic #{post.topic.id}: #{creator.errors.full_messages.join(", ")}",
)
end
end
end