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-65] #75

Closed
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ To configure the Welcome Bot, edit your `config.json` file with a message you wa
"com.mattermost.welcomebot": {
"WelcomeMessages": [
{
"TeamName": "your-team-name",
"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."
Expand Down Expand Up @@ -63,7 +63,7 @@ To configure the Welcome Bot, edit your `config.json` file with a message you wa

where

- **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
- **TeamName**: The teams 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 . In case of multiple teams, use comma seperated fields. For example `"my-team, my-team-2"` to display the same messages for both `my-team` and `my-team-2`
- **DelayInSeconds**: The number of seconds after joining a team that the user receives a welcome message.
- **Message**: The message posted to the user.
- (Optional) **AttachmentMessage**: Message text in attachment containing user action buttons.
Expand Down Expand Up @@ -100,7 +100,7 @@ To accomplish the above, you can specify the following configuration in your `co
"com.mattermost.welcomebot": {
"WelcomeMessages": [
{
"TeamName": "staff",
"TeamName": "staff,devsecops",
"DelayInSeconds": 5,
"Message": [
"### Welcome {{.UserDisplayName}} to the Staff {{.Team.DisplayName}} team!",
Expand Down
25 changes: 22 additions & 3 deletions server/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,27 @@ func (p *Plugin) validateCommand(action string, parameters []string) string {
return ""
}

// contains checks if a string is present in a slice
func contains(s []string, str string) bool {
for _, v := range s {
if v == str {
return true
}
}

return false
}

func (p *Plugin) executeCommandPreview(teamName string, args *model.CommandArgs) {
found := false
for _, message := range p.getWelcomeMessages() {
if message.TeamName == teamName {
if err := p.previewWelcomeMessage(teamName, args, *message); err != nil {
// splits the string and removes white spaces from underlying strings
teamNameSlice := strings.Split(message.TeamName, ",")
for i := range teamNameSlice {
teamNameSlice[i] = strings.TrimSpace(teamNameSlice[i])
}
maisnamrajusingh marked this conversation as resolved.
Show resolved Hide resolved
if contains(teamNameSlice, teamName) {
if err := p.previewWelcomeMessage(args, *message); err != nil {
p.postCommandResponse(args, "error occurred while processing greeting for team `%s`: `%s`", teamName, err)
return
}
Expand All @@ -113,7 +129,10 @@ func (p *Plugin) executeCommandList(args *model.CommandArgs) {
// Deduplicate entries
teams := make(map[string]struct{})
for _, message := range wecomeMessages {
teams[message.TeamName] = struct{}{}
teamNameSlice := strings.Split(message.TeamName, ",")
for _, tn := range teamNameSlice {
teams[tn] = struct{}{}
}
Comment on lines +133 to +135
Copy link
Contributor

Choose a reason for hiding this comment

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

Does spaces need to also be trimmed here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@hanzei this won't be needed here.

}

var str strings.Builder
Expand Down
8 changes: 6 additions & 2 deletions server/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"strings"
"time"

"github.com/mattermost/mattermost-server/v5/mlog"
Expand All @@ -18,8 +19,11 @@ func (p *Plugin) UserHasJoinedTeam(c *plugin.Context, teamMember *model.TeamMemb
}

for _, message := range p.getWelcomeMessages() {
if message.TeamName == data.Team.Name {
go p.processWelcomeMessage(*data, *message)
teamNameSlice := strings.Split(message.TeamName, ",")
for _, tn := range teamNameSlice {
if tn == data.Team.Name {
go p.processWelcomeMessage(*data, *message)
}
}
}
}
Expand Down
16 changes: 10 additions & 6 deletions server/http_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"encoding/json"
"net/http"
"strings"

"github.com/mattermost/mattermost-server/v5/model"
"github.com/mattermost/mattermost-server/v5/plugin"
Expand Down Expand Up @@ -57,12 +58,15 @@ func (p *Plugin) ServeHTTP(c *plugin.Context, w http.ResponseWriter, r *http.Req
switch r.URL.Path {
case "/addchannels":
for _, wm := range p.getWelcomeMessages() {
if data.Team.Name == wm.TeamName {
for _, ac := range wm.Actions {
if ac.ActionName == action.Context.Action {
p.processActionMessage(*data, action, *ac)
p.encodeEphemeralMessage(w, "")
return
teamNameSlice := strings.Split(wm.TeamName, ",")
for _, tn := range teamNameSlice {
if data.Team.Name == tn {
for _, ac := range wm.Actions {
if ac.ActionName == action.Context.Action {
p.processActionMessage(*data, action, *ac)
p.encodeEphemeralMessage(w, "")
return
}
}
maisnamrajusingh marked this conversation as resolved.
Show resolved Hide resolved
}
}
Expand Down
16 changes: 8 additions & 8 deletions server/welcomebot.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (p *Plugin) getSiteURL() string {
return *config.ServiceSettings.SiteURL
}

func (p *Plugin) newSampleMessageTemplate(teamName, userID string) (*MessageTemplate, error) {
func (p *Plugin) newSampleMessageTemplate(teamID string, userID string) (*MessageTemplate, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

The changes in this file come from #74, right? Would you mind dropping them and we try on #74 go get merged?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@hanzei Yes, doing just that. Will update you once done.

Copy link
Contributor

Choose a reason for hiding this comment

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

These change still seem to come from #74 (comment). Once you dropped them from the file, I'n happy to approve it.

data := &MessageTemplate{}
var err *model.AppError

Expand All @@ -66,14 +66,14 @@ func (p *Plugin) newSampleMessageTemplate(teamName, userID string) (*MessageTemp
return nil, fmt.Errorf("failed to query user %s: %w", userID, err)
}

if data.Team, err = p.API.GetTeamByName(teamName); err != nil {
p.API.LogError("failed to query team", "team_name", teamName, "err", err)
return nil, fmt.Errorf("failed to query team %s: %w", teamName, err)
if data.Team, err = p.API.GetTeam(teamID); err != nil {
p.API.LogError("failed to query team", "team_id", teamID, "err", err)
return nil, fmt.Errorf("failed to query team %s: %w", teamID, err)
}

if data.Townsquare, err = p.API.GetChannelByName(data.Team.Id, "town-square", false); err != nil {
p.API.LogError("failed to query town-square", "team_name", teamName)
return nil, fmt.Errorf("failed to query town-square %s: %w", teamName, err)
p.API.LogError("failed to query town-square", "team_name", data.Team.Name)
return nil, fmt.Errorf("failed to query town-square %s: %w", data.Team.Name, err)
}

if data.DirectMessage, err = p.API.GetDirectChannel(data.User.Id, p.botUserID); err != nil {
Expand All @@ -86,8 +86,8 @@ func (p *Plugin) newSampleMessageTemplate(teamName, userID string) (*MessageTemp
return data, nil
}

func (p *Plugin) previewWelcomeMessage(teamName string, args *model.CommandArgs, configMessage ConfigMessage) error {
messageTemplate, err := p.newSampleMessageTemplate(teamName, args.UserId)
func (p *Plugin) previewWelcomeMessage(args *model.CommandArgs, configMessage ConfigMessage) error {
messageTemplate, err := p.newSampleMessageTemplate(args.TeamId, args.UserId)
if err != nil {
return err
}
Expand Down