这是草稿,可能需要一些额外的工作。
词汇表
- trigger: 代表触发器的名称,例如:
user_added_to_group - triggerable: 代表与触发器相关的代码逻辑,例如:
triggers/user_added_to_group_.rb - script: 代表脚本的名称,例如:
send_pms - scriptable: 代表与脚本相关的代码逻辑,例如:
scripts/send_pms.rb
插件 API
add_automation_scriptable(name, &block)
add_automation_triggerable(name, &block)
Scriptable API
field
field :name, component: 允许您在自动化界面中添加可自定义的值。
有效组件列表:
# foo 必须是唯一的,并且代表您的字段名称。
field :foo, component: :text # 生成一个文本输入框
field :foo, component: :list # 生成一个多选文本输入框,用户可以在其中填写值
field :foo, component: :choices, extra: { content: [ {id: 1, name: 'your.own.i18n.key.path' } ] } # 生成一个带有自定义内容的组合框
field :foo, component: :boolean # 生成一个复选框输入框
field :foo, component: :category # 生成一个类别选择器
field :foo, component: :group # 生成一个组选择器
field :foo, component: :date_time # 生成一个日期时间选择器
field :foo, component: :tags # 生成一个标签选择器
field :foo, component: :user # 生成一个用户选择器
field :foo, component: :pms # 允许创建一种或多种 PM 模板
field :foo, component: :categories # 允许选择零个或多个类别
field :foo, component: :key-value # 允许创建键值对
field :foo, component: :message # 允许使用可替换变量编写 PM
field :foo, component: :trustlevel # 允许选择一个或多个信任级别
triggerables 和 triggerable!
# 允许您定义脚本允许的 triggerables 列表
triggerables %i[recurring]
# 允许您强制为脚本设置 triggerable,还可以强制设置字段的某些状态
field :recurring, component: :boolean
triggerable! :recurring, state: { foo: false }
placeholders
# 允许您使用占位符语法 `%%sender%%` 将键标记为文本中的可替换项
placeholder :sender
请注意,脚本有责任为占位符提供值,并使用 input = utils.apply_placeholders(input, { sender: 'bob' }) 应用替换。
script
这是自动化的核心,也是所有逻辑发生的地方。
# context 在触发自动化时发送,并且可能因触发器而异
script do |context, fields, automation|
end
本地化
您将使用的每个字段都将依赖于 i18n 键,并根据其触发器/脚本进行命名空间化。
例如,一个具有以下内容的 scriptable:
field :post_created_edited, component: :category
将在 client.en.yml 中需要以下键:
en:
js:
discourse_automation:
scriptables:
post_created_edited:
fields:
restricted_category:
label: Category
description: Optional, allows to limit trigger execution to this category
请注意,这里的 description 是可选的。
本文档受版本控制 - 在 github 上建议更改。