DEV: send_chat_message supports topic_tags_changed (#28755)

This will allow admins to use this script and trigger together.
This commit is contained in:
Joffrey JAFFEUX 2024-09-05 13:05:19 +02:00 committed by GitHub
parent e991574389
commit 81c5f1d75f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 66 additions and 21 deletions

View File

@ -452,32 +452,29 @@ after_initialize do
}
end
if defined?(DiscourseAutomation)
add_automation_scriptable("send_chat_message") do
field :chat_channel_id, component: :text, required: true
field :message, component: :message, required: true, accepts_placeholders: true
field :sender, component: :user
add_automation_scriptable("send_chat_message") do
field :chat_channel_id, component: :text, required: true
field :message, component: :message, required: true, accepts_placeholders: true
field :sender, component: :user
placeholder :channel_name
placeholder :channel_name
triggerables [:recurring]
triggerables %i[recurring topic_tags_changed]
script do |context, fields, automation|
sender = User.find_by(username: fields.dig("sender", "value")) || Discourse.system_user
channel = Chat::Channel.find_by(id: fields.dig("chat_channel_id", "value"))
script do |context, fields, automation|
sender = User.find_by(username: fields.dig("sender", "value")) || Discourse.system_user
channel = Chat::Channel.find_by(id: fields.dig("chat_channel_id", "value"))
placeholders = { channel_name: channel.title(sender) }.merge(context["placeholders"] || {})
placeholders = { channel_name: channel.title(sender) }.merge(context["placeholders"] || {})
creator =
::Chat::CreateMessage.call(
chat_channel_id: channel.id,
guardian: sender.guardian,
message: utils.apply_placeholders(fields.dig("message", "value"), placeholders),
)
creator =
::Chat::CreateMessage.call(
chat_channel_id: channel.id,
guardian: sender.guardian,
message: utils.apply_placeholders(fields.dig("message", "value"), placeholders),
)
if creator.failure?
Rails.logger.warn "[discourse-automation] Chat message failed to send:\n#{creator.inspect_steps.inspect}\n#{creator.inspect_steps.error}"
end
if creator.failure?
Rails.logger.warn "[discourse-automation] Chat message failed to send:\n#{creator.inspect_steps.inspect}\n#{creator.inspect_steps.error}"
end
end
end

View File

@ -339,4 +339,52 @@ describe Chat do
).by(1)
end
end
describe "when using topic tags changed trigger automation" do
describe "with the send message script" do
fab!(:automation_1) do
Fabricate(
:automation,
trigger: DiscourseAutomation::Triggers::TOPIC_TAGS_CHANGED,
script: :send_chat_message,
)
end
fab!(:tag_1) { Fabricate(:tag) }
fab!(:user_1) { Fabricate(:admin) }
fab!(:topic_1) { Fabricate(:topic) }
fab!(:channel_1) { Fabricate(:chat_channel) }
before do
SiteSetting.discourse_automation_enabled = true
SiteSetting.tagging_enabled = true
automation_1.upsert_field!(
"watching_tags",
"tags",
{ value: [tag_1.name] },
target: "trigger",
)
automation_1.upsert_field!(
"chat_channel_id",
"text",
{ value: channel_1.id },
target: "script",
)
automation_1.upsert_field!(
"message",
"message",
{ value: "[{{topic_title}}]({{topic_url}})" },
target: "script",
)
end
it "sends the message" do
DiscourseTagging.tag_topic_by_names(topic_1, Guardian.new(user_1), [tag_1.name])
expect(channel_1.chat_messages.last.message).to eq(
"[#{topic_1.title}](#{topic_1.relative_url})",
)
end
end
end
end