# 我如何安装自己用 Ruby 编写的插件？

**URL:** https://meta.discourse.org/t/how-can-i-install-my-own-plugin-written-in-ruby/349762
**Category:** Development
**Tags:** email
**Created:** [2025年一月30日 16:42 UTC](https://meta.discourse.org/t/how-can-i-install-my-own-plugin-written-in-ruby/349762 "2025-01-30T16:42:45Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Xavier\_Garzon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/xavier_garzon/32/485602_2.png) [@Xavier\_Garzon](https://meta.discourse.org/u/Xavier_Garzon)
#### Post date: [2025年一月30日 16:42 UTC](https://meta.discourse.org/t/how-can-i-install-my-own-plugin-written-in-ruby/349762/1 "2025-01-30T16:42:45Z")

</div>

我有一个包含 discourse 用户电子邮件的 CSV 文件。我需要创建一个每天运行的脚本，向特定用户发送特定的电子邮件模板。然后，我需要每天使用下一组用户运行此脚本。我该怎么做？

根据 [https://ask.discourse.com/，我需要创建代码来自动化此过程，并将代码和数据放在\*plugins\*文件夹中。代码建议如下：](https://ask.discourse.com/%EF%BC%8C%E6%88%91%E9%9C%80%E8%A6%81%E5%88%9B%E5%BB%BA%E4%BB%A3%E7%A0%81%E6%9D%A5%E8%87%AA%E5%8A%A8%E5%8C%96%E6%AD%A4%E8%BF%87%E7%A8%8B%EF%BC%8C%E5%B9%B6%E5%B0%86%E4%BB%A3%E7%A0%81%E5%92%8C%E6%95%B0%E6%8D%AE%E6%94%BE%E5%9C%A8*plugins*%E6%96%87%E4%BB%B6%E5%A4%B9%E4%B8%AD%E3%80%82%E4%BB%A3%E7%A0%81%E5%BB%BA%E8%AE%AE%E5%A6%82%E4%B8%8B%EF%BC%9A)

```plaintext
# frozen_string_literal: true
# name: auto-email
# about: A custom plugin to send bulk emails using automation and CSV files
# version: 0.3

require 'csv'

after_initialize do
  if defined?(DiscourseAutomation)
    # Define a unique scriptable for the automation
    DiscourseAutomation::Scriptable::AUTO_EMAIL_SCRIPT = "auto_email_script"

    add_automation_scriptable(DiscourseAutomation::Scriptable::AUTO_EMAIL_SCRIPT) do
      # Define fields for the automations UI:
      field :email_template, component: :message # Email body to be sent
      field :csv_file_name, component: :text # Name of CSV file in the "data" folder

      script do |context, fields, automation|
        # Define the path to the data folder
        data_folder_path = Rails.root.join("plugins", "auto-email", "data")

        # Get the CSV file name from the admin automation field
        csv_file_name = fields["csv_file_name"]["value"]
        csv_file_path = File.join(data_folder_path, csv_file_name)

        # Ensure the file exists
        unless File.exist?(csv_file_path)
          Rails.logger.warn("CSV file #{csv_file_path} not found!")
          next
        end

        # Parse the CSV file
        begin
          users = CSV.read(csv_file_path, headers: true).map(&:to_hash)
        rescue StandardError => e
          Rails.logger.error("Error reading CSV file (#{csv_file_name}): #{e.message}")
          next
        end

        # Get the email message content from the automation field
        email_body = fields["email_template"]["value"]

        # Iterate through users from the CSV and send emails
        users.each do |user_data|
          email = user_data["email"] || user_data["normalized_email"]
          user = User.find_by_email(email)
          
          if user
            begin
              # Send an automated email using SystemMessage
              SystemMessage.new(user).send_email(
                "custom_email_template", # Ensure this email template exists
                message_body: email_body
              )
              Rails.logger.info("Email sent to: #{email}")
            rescue StandardError => e
              Rails.logger.error("Failed to send email to #{email}: #{e.message}")
            end
          else
            Rails.logger.warn("No user found with email: #{email}")
          end
        end
      end
    end
  end
end

```

我的插件结构如下：  
/var/discourse/plugins/auto-email/  
├── README.rb  
├── data/  
├── mamapedia\_users\_data\_01.csv  
├── lib/  
├── plugin.rb

但是，当我重启或重建应用程序时，在管理面板中看不到新插件。日志中也没有任何错误。

我哪里做错了？我还需要做些什么？

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [2025年一月30日 16:45 UTC](https://meta.discourse.org/t/how-can-i-install-my-own-plugin-written-in-ruby/349762/2 "2025-01-30T16:45:06Z")

</div>

我将从[开发 Discourse 插件 - 第 1 部分 - 创建一个基本插件](https://meta.discourse.org/t/developing-discourse-plugins-part-1-create-a-basic-plugin/30515)开始。

---

<div class="post-metadata">

### Author: ![Xavier\_Garzon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/xavier_garzon/32/485602_2.png) [@Xavier\_Garzon](https://meta.discourse.org/u/Xavier_Garzon)
#### Post date: [2025年一月31日 19:03 UTC](https://meta.discourse.org/t/how-can-i-install-my-own-plugin-written-in-ruby/349762/3 "2025-01-31T19:03:31Z")

</div>

我试过那个了，很简单。但不幸的是，它对我不起作用。

我在两个不同的应用程序中测试了相同的解决方案。这是我使用的 plugin.rb 文件：

```plaintext
# name: auto-email
# about: A super simple plugin to demonstrate how plugins work
# version: 0.0.1
# authors: Xavier Garzon

```

我还检查了容器中的插件文件夹（/var/www/discourse/plugins），似乎我的插件没有被复制。也许这是问题的一部分。

对于第二个测试，我也尝试了：

```plaintext
./launcher rebuild app --clean

```

和

```plaintext
./launcher restart app

```

这是我正在运行的版本：  
3.4.0.beta4-dev  
([9f41ce7fce](https://github.com/discourse/discourse/commits/9f41ce7fce3160cdfeb2818fd11d51326eb7d8a0))

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [2025年一月31日 19:36 UTC](https://meta.discourse.org/t/how-can-i-install-my-own-plugin-written-in-ruby/349762/4 "2025-01-31T19:36:15Z")

</div>

你的插件放在 GitHub 仓库了吗？

你不能只是把 `plugin.rb` 放在 `/lib` 目录下，它必须放在 plugins 目录下，并且在生产环境中，你需要按照 [在自托管站点上安装插件](https://meta.discourse.org/t/install-plugins-on-a-self-hosted-site/19157) 中的说明通过 GitHub 添加它。

> [@Xavier\_Garzon](#):
>
> 我还检查了容器中的插件文件夹（/var/www/discourse/plugins），似乎我的插件没有被复制。也许这是问题的一部分。

是的。如果不在那里，它就不存在。

你真的应该阅读所有这些插件主题。你也应该在开发环境中进行测试。
