How can I install my own plugin written in Ruby?

I have a CSV file with my discourse user emails. I need to create a script that will run daily and send a specific email template to a specific set of users. Then, every day I need to run this script with the next set of users. How can I do that?

According to https://ask.discourse.com/ I need to create my code to automate this and place the code and data in the plugins folder. The code suggestion is here:

# 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

And my plugin structure is like this:
/var/discourse/plugins/auto-email/
β”œβ”€β”€ README.rb
β”œβ”€β”€ data/
β”œβ”€β”€ mamapedia_users_data_01.csv
β”œβ”€β”€ lib/
β”œβ”€β”€ plugin.rb

However, when I restart or rebuild the app I can not see the new plugin in the admin panel. I don’t see any errors in the logs.

What am I doing wrong? Do I need to do something else?

4 Likes

I’d start at Developing Discourse Plugins - Part 1 - Create a basic plugin.

8 Likes

I tried that one, is very simple. But unfortunately is not working for me.

I tested the same solution in two different apps. Here is the plugin.rb file I used:

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

I also checked my plugins folder in the container (/var/www/discourse/plugins) and it seems that my plugin is not being copied. Maybe that is part of the problem.

For this second test, I also tried:

./launcher rebuild app --clean

and

./launcher restart app

This is the version I’m running:
3.4.0.beta4-dev
(9f41ce7fce)

1 Like

Did you put your plugin in a github repo?

You can’t just stick a plugin.rb in /lib it has to be in plugins, and in production, you need to add it via github as described in Install plugins on a self-hosted site

Yes. If it’s not there, it’s not there.

You really want to read all of those plugin topics. You also want to do your testing in a development environment.

4 Likes