# Creating a custom Automation

**URL:** https://meta.discourse.org/t/creating-a-custom-automation/272895
**Category:** Development
**Tags:** automation
**Created:** [February 21, 2023, 8:41am UTC](https://meta.discourse.org/t/creating-a-custom-automation/272895 "2023-02-21T08:41:09Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![yhh9xdq7dc](https://avatars.discourse-cdn.com/v4/letter/y/f17d59/32.png) [@yhh9xdq7dc](https://meta.discourse.org/u/yhh9xdq7dc)
#### Post date: [February 21, 2023, 8:41am UTC](https://meta.discourse.org/t/creating-a-custom-automation/272895/1 "2023-02-21T08:41:09Z")

</div>

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!

---

<div class="post-metadata">

### Author: ![j.jaffeux](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/j.jaffeux/32/60297_2.png) [@j.jaffeux](https://meta.discourse.org/u/j.jaffeux)
#### Post date: [February 21, 2023, 10:09am UTC](https://meta.discourse.org/t/creating-a-custom-automation/272895/2 "2023-02-21T10:09:22Z")

</div>

Yes it needs a plugin to define a custom script.

Something like this in plugin.rb

```ruby
after_initialize do
  if defined?(DiscourseAutomation)
    add_automation_scriptable("my_custom_script") do
      # ...
    end
  end
end

```

---

<div class="post-metadata">

### Author: ![yhh9xdq7dc](https://avatars.discourse-cdn.com/v4/letter/y/f17d59/32.png) [@yhh9xdq7dc](https://meta.discourse.org/u/yhh9xdq7dc)
#### Post date: [February 21, 2023, 10:37am UTC](https://meta.discourse.org/t/creating-a-custom-automation/272895/3 "2023-02-21T10:37:08Z")

</div>

> [@Discourse Automation](https://meta.discourse.org/t/discourse-automation/195773/1):
>
> `&block`

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?

---

<div class="post-metadata">

### Author: ![j.jaffeux](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/j.jaffeux/32/60297_2.png) [@j.jaffeux](https://meta.discourse.org/u/j.jaffeux)
#### Post date: [February 21, 2023, 10:37am UTC](https://meta.discourse.org/t/creating-a-custom-automation/272895/4 "2023-02-21T10:37:59Z")

</div>

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

---

<div class="post-metadata">

### Author: ![yhh9xdq7dc](https://avatars.discourse-cdn.com/v4/letter/y/f17d59/32.png) [@yhh9xdq7dc](https://meta.discourse.org/u/yhh9xdq7dc)
#### Post date: [February 21, 2023, 10:40am UTC](https://meta.discourse.org/t/creating-a-custom-automation/272895/5 "2023-02-21T10:40:15Z")

</div>

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

```plaintext
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

```

---

<div class="post-metadata">

### Author: ![j.jaffeux](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/j.jaffeux/32/60297_2.png) [@j.jaffeux](https://meta.discourse.org/u/j.jaffeux)
#### Post date: [February 21, 2023, 10:41am UTC](https://meta.discourse.org/t/creating-a-custom-automation/272895/6 "2023-02-21T10:41:26Z")

</div>

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](https://github.com/discourse/discourse-assign/blob/main/plugin.rb#L958)

---

<div class="post-metadata">

### Author: ![yhh9xdq7dc](https://avatars.discourse-cdn.com/v4/letter/y/f17d59/32.png) [@yhh9xdq7dc](https://meta.discourse.org/u/yhh9xdq7dc)
#### Post date: [February 21, 2023, 10:47am UTC](https://meta.discourse.org/t/creating-a-custom-automation/272895/7 "2023-02-21T10:47:43Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![j.jaffeux](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/j.jaffeux/32/60297_2.png) [@j.jaffeux](https://meta.discourse.org/u/j.jaffeux)
#### Post date: [February 21, 2023, 10:48am UTC](https://meta.discourse.org/t/creating-a-custom-automation/272895/8 "2023-02-21T10:48:46Z")

</div>

are you sure your new plugin is enabled?

---

<div class="post-metadata">

### Author: ![yhh9xdq7dc](https://avatars.discourse-cdn.com/v4/letter/y/f17d59/32.png) [@yhh9xdq7dc](https://meta.discourse.org/u/yhh9xdq7dc)
#### Post date: [February 21, 2023, 10:49am UTC](https://meta.discourse.org/t/creating-a-custom-automation/272895/9 "2023-02-21T10:49:47Z")

</div>

Yes, of course. It is absolutely enabled.

---

<div class="post-metadata">

### Author: ![j.jaffeux](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/j.jaffeux/32/60297_2.png) [@j.jaffeux](https://meta.discourse.org/u/j.jaffeux)
#### Post date: [February 21, 2023, 10:52am UTC](https://meta.discourse.org/t/creating-a-custom-automation/272895/10 "2023-02-21T10:52:51Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![yhh9xdq7dc](https://avatars.discourse-cdn.com/v4/letter/y/f17d59/32.png) [@yhh9xdq7dc](https://meta.discourse.org/u/yhh9xdq7dc)
#### Post date: [February 21, 2023, 10:57am UTC](https://meta.discourse.org/t/creating-a-custom-automation/272895/11 "2023-02-21T10:57:26Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![yhh9xdq7dc](https://avatars.discourse-cdn.com/v4/letter/y/f17d59/32.png) [@yhh9xdq7dc](https://meta.discourse.org/u/yhh9xdq7dc)
#### Post date: [February 21, 2023, 11:34am UTC](https://meta.discourse.org/t/creating-a-custom-automation/272895/12 "2023-02-21T11:34:19Z")

</div>

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.
