Creating a custom Automation

Is it possible to create own scripts without any problem? Just putting it in the folder and with the plugin updates, nothing happens? Or do i have to create a plugin that interfaces with this plugin? For example, I need a script to create a topic. There is a post creation script but I need to create automatic topics. Thanks!

2 Likes

Yes it needs a plugin to define a custom script.

Something like this in plugin.rb

after_initialize do
  if defined?(DiscourseAutomation)
    add_automation_scriptable("my_custom_script") do
      # ...
    end
  end
end
3 Likes

I tried but it says:

Couldn’t find script lb-script for automation testing, ensure the associated plugin is installed

Maybe I’m wrong, but should I create a separate file or is it okay to put everything in the plugin.rb file?

1 Like

Show me some code or it will be hard for me to help you :slight_smile:

1 Like

I just tried with the post script copied from the scripts folder of the Automation plugin:

after_initialize do
    if defined?(DiscourseAutomation)
        add_automation_scriptable("lb-script") do
            version 1

            placeholder :creator_username
          
            field :creator, component: :user
            field :topic, component: :text, required: true
            field :post, component: :post, required: true
          
            triggerables %i[recurring point_in_time]
          
            script do |context, fields, automation|
              creator_username = fields.dig("creator", "value") || Discourse.system_user.username
          
              placeholders = { creator_username: creator_username }.merge(context["placeholders"] || {})
          
              creator = User.find_by!(username: creator_username)
          
              PostCreator.new(
                creator,
                topic_id: fields.dig("topic", "value"),
                raw: fields.dig("post", "value"),
              ).create!
            end
        end
    end
end

I lack context around what you are doing here, here is a full blown example in an external plugin: discourse-assign/plugin.rb at main · discourse/discourse-assign · GitHub

1 Like

I don’t understand what the difference is compared to what I did. Obviously I used the post script just to see if it worked. But when I go to create an automation, it doesn’t find me any trigger and it says it can’t find the script. But the code should let me select between the 2 triggers to create a post, right? I’m definitely wrong but I don’t really understand.

are you sure your new plugin is enabled?

1 Like

Yes, of course. It is absolutely enabled.

Can you try to use assign locally and see if you manage to get to show in the list? If not theres something else going on.

I can confirm that Random Assign works even if the Assign plugin is not enabled. There’s probably something I’m missing, I don’t want to waste your time.

3 Likes

I figured out what the problem was.

I changed lb-script to lb_script and it works now. Evidently you can’t use the hyphen(-) symbol.

3 Likes