Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add configuration via system console #92

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 46 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,18 @@ Use this plugin to improve onboarding and HR processes. It adds a Welcome Bot th

## Configuration

1. Go to **System Console > Plugins > Management** and click **Enable** to enable the Welcome Bot plugin.
1. Go to **System Console > Plugins > Plugin Management** and click **Enable** to enable the Welcome Bot plugin.
- If you are running Mattermost v5.11 or earlier, you must first go to the [releases page of this GitHub repository](https://github.com/mattermost/mattermost-plugin-welcomebot/releases), download the latest release, and upload it to your Mattermost instance [following this documentation](https://docs.mattermost.com/administration/plugins.html#plugin-uploads).

2. Modify your `config.json` file to include your Welcome Bot's messages and actions, under the `PluginSettings`. See below for an example of what this should look like.
2. Modify your configuration to include your Welcome Bot's messages and actions. See below for examples.

## Usage

To configure the Welcome Bot, edit your `config.json` file with a message you want to send to a user in the following format:
To configure the Welcome Bot, choose one of the below options:

### config.json

Edit your `config.json` file with a message you want to send to a user in the following format:

```
"Plugins": {
Expand Down Expand Up @@ -61,7 +65,45 @@ To configure the Welcome Bot, edit your `config.json` file with a message you wa
},
```

where
This field can be JSON or a string containing JSON.

### System Console

In the UI, go to **System Console -> Plugins -> Welcome Bot** and edit the **Welcome Messages** text field to include the JSON array for messages you want to send. Use the following format:

```
[
{
"TeamName": "your-team-name, your-second-team-name",
"DelayInSeconds": 3,
"Message": [
"Your welcome message here. Each list item specifies one line in the message text."
],
"AttachmentMessage": [
"Attachment message containing user actions"
],
"Actions" : [
{
"ActionType": "button",
"ActionDisplayName": "User Action",
"ActionName": "action-name",
"ActionSuccessfulMessage": [
"Message posted after the user takes this action and joins channels specified by 'ChannelsAddedTo'."
],
"ChannelsAddedTo": ["channel-1", "channel-2"]
},
{
"ActionType": "automatic",
"ChannelsAddedTo": ["channel-3", "channel-4"]
}
]
}
]
```

If you see `[Object object]` in the text field, that's because the configuration was configured as JSON directly in your `config.json` instead of as a string.

### Reference

- **TeamName**: The team for which the Welcome Bot sends a message for. Must be the team handle used in the URL, in lowercase. For example, in the following URL the **TeamName** value is `my-team`: https://example.com/my-team/channels/my-channel
- **DelayInSeconds**: The number of seconds after joining a team that the user receives a welcome message.
Expand Down
9 changes: 8 additions & 1 deletion plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@
}
},
"settings_schema": {
"header": "Configure this plugin directly in the config.json file. Learn more [in our documentation](https://github.com/mattermost/mattermost-plugin-welcomebot/blob/master/README.md).\n\n To report an issue, make a suggestion, or submit a contribution, [check the plugin repository](https://github.com/mattermost/mattermost-plugin-welcomebot)."
"settings": [
{
"key": "WelcomeMessages",
"type": "longtext",
"display_name": "Welcome Messages:",
"help_text": "JSON formatted configuration for the welcome messages. See [usage here](https://github.com/mattermost/mattermost-plugin-welcomebot#usage)."
}
]
}
}
39 changes: 39 additions & 0 deletions server/configuration.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package main

import (
"encoding/json"
)

const (
actionTypeAutomatic = "automatic"
actionTypeButton = "button"
Expand Down Expand Up @@ -54,6 +58,41 @@ func (p *Plugin) getWelcomeMessages() []*ConfigMessage {
return p.welcomeMessages.Load().([]*ConfigMessage)
}

// Custom JSON unmarshal function for Configuration.
// To allow for configuration in the System Console we need to support Configuration.WelcomeMessages
// being either a string or slice of *ConfigMessage.
func (c *Configuration) UnmarshalJSON(b []byte) error {
var s struct {
WelcomeMessages string
}

err := json.Unmarshal(b, &s)
if err == nil {
var configMessages []*ConfigMessage
err = json.Unmarshal([]byte(s.WelcomeMessages), &configMessages)
if err != nil {
return err
}

c.WelcomeMessages = configMessages

return nil
}

var tc struct {
WelcomeMessages []*ConfigMessage
}

err = json.Unmarshal(b, &tc)
if err != nil {
return err
}

c.WelcomeMessages = tc.WelcomeMessages

return nil
}

// OnConfigurationChange is invoked when configuration changes may have been made.
func (p *Plugin) OnConfigurationChange() error {
var c Configuration
Expand Down
Loading