# 사용자 정의 자동화 만들기

**URL:** https://meta.discourse.org/t/create-custom-automations/275931
**Category:** Developer Guides
**Tags:** automation, how-to
**Created:** [8월 20, 2023, 2:57오후 UTC](https://meta.discourse.org/t/create-custom-automations/275931 "2023-08-20T14:57:33Z")
**Posts on this page:** 1
**Showing post:** 6

<div class="post-metadata">

### Author: ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)
#### Post date: [10월 5, 2023, 6:03오전 UTC](https://meta.discourse.org/t/create-custom-automations/275931/6 "2023-10-05T06:03:29Z")

</div>

> [@McUles](#):
>
> hello world 예제가 있으면 좋겠네요.

가장 먼저 살펴볼 만한 곳은 [Data Explorer](https://meta.discourse.org/t/32566?silent=true) 플러그인에 추가되는 자동화 스크립트입니다: [discourse-data-explorer/plugin.rb at main · discourse/discourse-data-explorer · GitHub](https://github.com/discourse/discourse-data-explorer/blob/main/plugin.rb#L82-L122). 또한 Automation 플러그인의 기존 스크립트와 트리거를 살펴보는 것도 좋습니다: [https://github.com/discourse/discourse-automation/tree/main/lib/discourse\_automation](https://github.com/discourse/discourse-automation/tree/main/lib/discourse_automation)

Meta에 커스텀 자동화를 추가하는 방법에 대한 정보가 많지 않아, 사용자의 Activity Summary 이메일 설정을 업데이트하는 스크립트를 추가하는 `plugin.rb` 파일 예시를 여기에 공유합니다. 이 스크립트는 Automation 플러그인의 ‘user\_added\_to\_group’ 또는 ‘user\_removed\_from\_group’ 트리거로 실행될 수 있습니다.

```ruby
# frozen_string_literal: true

# name: automation-script-example
# about: An example of how to add a script to an automation
# version: 0.0.1
# authors: scossar

enabled_site_setting :automation_script_example_enabled

after_initialize do
  reloadable_patch do
    if defined?(DiscourseAutomation)
      DiscourseAutomation::Scriptable::USER_UPDATE_SUMMARY_EMAIL_OPTIONS =
        "user_update_summary_email_options"
      add_automation_scriptable(
        DiscourseAutomation::Scriptable::USER_UPDATE_SUMMARY_EMAIL_OPTIONS
      ) do

        field :email_digests, component: :boolean

        version 1
        triggerables [:user_added_to_group, :user_removed_from_group]

        script do |context, fields, automation|
          if automation.script == "user_update_summary_email_options" && (context["kind"] == "user_added_to_group" || context["kind"] == "user_removed_from_group")
            user_id = context["user"].id
            digest_option = fields.dig("email_digests", "value")
            user_option = UserOption.find_by(user_id: user_id)

            if (user_option)
              user_option.update(email_digests: digest_option)
            end
          end
        end
      end
    end
  end
end

```

전체 플러그인 코드는 여기에 있습니다: [GitHub - scossar/automation-script-example: An example of how to add a custom script to the Discourse Automation plugin. · GitHub](https://github.com/scossar/automation-script-example).

⚠ 이 코드를 그대로 프로덕션 사이트에서 사용하지 마세요. 저는 오늘 밤까지 Automation 코드를 확인하지 않았습니다. 코드에 대한 잠재적 문제점에 대한 피드백을 받으면 이 게시글과 GitHub 저장소를 업데이트하겠습니다.

수정: 제 우려는 ‘user\_added\_to\_group’ 또는 ‘user\_removed\_from\_group’ 트리거 중 하나로 여러 자동화 스크립트가 트리거되는 경우를 어떻게 가장 잘 처리할 것인가였습니다. 플러그인의 초기 버전은 다음을 확인하고 있었습니다:

`fields.has_key?("email_digests")`

하지만 그 방식은 다소 불안정하게 느껴졌습니다. 만약 `email_digests` 키를 가진 다른 스크립트가 추가되면 어떻게 될까요?

업데이트된 코드는 `automation` 매개변수를 코드 블록으로 전달하고 다음을 확인합니다:

`automation.script == "user_update_summary_email_options"`

이렇게 하면 스크립트가 잘못된 자동화에 의해 실행되지 않도록 보장할 수 있습니다.

… 더 생각해보니, 스크립트가 설정되지 않은 자동화에 의해 트리거될 가능성은 낮을 것 같습니다 🙂

---

_[View the full topic](https://meta.discourse.org/t/create-custom-automations/275931)._
