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

[GH-108]: Added the setting in the system console for plugin settings #126

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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 @@ -21,14 +21,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 @@ -65,7 +69,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:

```
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
```
```json

[
{
"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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we instead JSON.stringify the data if it's not originally a string?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mickmister Can you elaborate a bit on this, I am not really able to understand it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding the other review comments, I am not able to push the code, as we don't have access to push the code. Will fix it afterwards

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, if it's the case that the setting is an object (and not a JSON string), then the frontend can call JSON.stringify to make it into a string to put into the text box. This would require the plugin setting to be custom I think. Otherwise the admin cannot go and edit the current configuration if it just says [Object object]

Ideally we can just keep it as an object in the config file too, and not have a JSON string in the config.json file. Which would also require it to be a custom admin console setting


### 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)."
}
]
Comment on lines +21 to +27
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The indentation seems off here

Suggested change
{
"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)."
}
]
{
"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