Custom automation - modifications flow for successful integration on Automation plugin

Hi all,

I’m trying to create a custom automation that when a query is marked as solution/solved, the creator of the topic will receive a pms (or alternatively, a new reply is created in the thread with a pre-defined message - in both cases it would be a poll).

It needs to be a custom automation as the current available ‘send pms’ script into the automation plugin sends the pms to whomever has sent the reply that was marked as solution/solved (and not to the person who has created the topic, as intended in my use case).

Below is the script I have created to attempt to post a new reply in the topic once a reply is marked as solution (in the following sample, I’m not sending the poll as reply, just giving the freedom for the admin to choose what is going to be returned or let is as default):

# frozen_string_literal: true

DiscourseAutomation::Scriptable.add(DiscourseAutomation::Scripts::REPLY_ON_SOLUTION) do
    field :reply_text, component: :text
    # field :answering_user, component: :user
    field :once, component: :boolean
  
    version 1
  
    triggerables %i[:first_accepted_solution] if defined?(DiscourseSolved)
  
    placeholder :sender_username
    placeholder :word
  
    script do |context, fields, automation|
      topic = context["topic"]
      # user = context["user"]
      reply_text = fields.dig("reply_text", "value")
  
      # Post a reply in the topic where a solution was marked
      PostCreator.create!(
        Discourse.system_user,
        topic_id: topic.id,
        raw: reply_text || "A solution has been marked for this topic!",
      )
    end
  end

But I’m not sure if it should be PostCreator.create!, or PostCreator.reply, or perhaps something else. What brings me to one question:

  • Is there anywhere documented the keywords to be used during a custom automation script creation?

Based on the above automation, once I trigger it marking a reply as solution, after 1s I’m getting a pop-up message with a 500 internal server error message on the screen.

  • Am I missing something? My deployment was done using devcontainers, in my local machine for development test purposes.
  • What is necessary to do, after creating the custom script? Maybe I am missing something in this phase? Or maybe it is problem in the script itself?
  • Is anywhere documented, the steps to proceed after your script is done? For instance: which files needs to be modified in order to everything work smoothly? (I found hard way I needed to change the client.en.yml for the correct name and description shows in the automation list)

I’m planning to give it an attempt, based in the send pms built-in script, to change my script for instead of create a reply, send the pms, but not sure on the following:

  • how I would tag the person who had created the topic and not the one that had the reply marked as solution?

Thank a lot in advance for any pointers and help.

Did you follow this guide?

1 Like

Hi @NateDhaliwal ,

Many thanks for your reply.

Yes, I have followed that guide… I will comment the guide with some suggestions afterwards.

I made the custom automation work btw.

The problem with the 500 internal server error was that I was tagging the wrong “context”, I found it out looking at the logs and tagging the correct one.

Once the content of the right context was added to a variable, the 500 internal server error was solved.

Further logic in the code was also modified.

Edit: To make the custom automation work in my local deployed instance I had to modify also the following files:

Create your custom automation script

Update: server.en.yml

add custom automation name; title; and description on the scriptables section of the yml file.

Update: client.en.yml

add custom automation name on scriptables; add the ‘field’ keyword; inside field keyword add ‘field_name’ followed by ‘label’ and ‘description’

Update: scripts.rb

add the custom automation name in the list of scripts. Sample: FILE_NAME = “file_name”

Update: plugin.rb

inside ‘after_initialize do’, add the path to the custom automation script. Sample: ‘lib/discourse_automation/scripts/file_name’

1 Like