Skip to content

Commit

Permalink
[MM-387] Add a global welcome message field
Browse files Browse the repository at this point in the history
  • Loading branch information
ayusht2810 committed Apr 17, 2024
1 parent 502eb34 commit 9878691
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 1 deletion.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ where
- **ActionName**: Sets the action name used by the plugin to identify which action is taken by a user.
- **ActionSuccessfulMessage**: Message posted after the user takes this action and joins the specified channels.
- **ChannelsAddedTo**: List of channel names the user is added to. Must be the channel handle used in the URL, in lowercase. For example, in the following URL the **channel name** value is `my-channel`: https://example.com/my-team/channels/my-channel
- **GlobalWelcomeMessage**: The welcome message to send globally to a new user from the Welcome Bot. Adding this field will ignore the `TeamName`, `Message`, `AttachmentMessage` and `Actions` fields in the config to post a global message, irrespective of the team the user is part of.

The preview of the configured messages, as well as the creation of a channel welcome message, can be done via bot commands:
* `/welcomebot help` - Displays usage information.
Expand Down Expand Up @@ -179,6 +180,12 @@ To accomplish the above, you can specify the following configuration in your `co
"ChannelsAddedTo": ["escalation-process", "incidents"]
}
]
},
{
"DelayInSeconds": 5,
"GlobalWelcomeMessage": [
"### Welcome {{.UserDisplayName}} to the Mattermost!",
],
}
]
}
Expand Down
3 changes: 3 additions & 0 deletions server/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ type ConfigMessageAction struct {

// ConfigMessage represents the message to send in channel
type ConfigMessage struct {
// This message will send a global welcome message using the bot
GlobalWelcomeMessage []string

// This message will fire when it matches the supplied team
TeamName string

Expand Down
40 changes: 39 additions & 1 deletion server/hooks.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,52 @@
package main

import (
"bytes"
"fmt"
"html/template"
"strings"
"time"

"github.com/mattermost/mattermost-server/v6/model"
"github.com/mattermost/mattermost-server/v6/plugin"
"github.com/mattermost/mattermost-server/v6/shared/mlog"
)

// UserHasBeenCreated is invoked after a user was created.
func (p *Plugin) UserHasBeenCreated(c *plugin.Context, user *model.User) {
data := p.constructGlobalMessageTemplate(user.Id)
if data == nil {
return
}

for _, message := range p.getWelcomeMessages() {
if data.User.IsGuest() && !message.IncludeGuests {
continue
}

if len(message.GlobalWelcomeMessage) > 0 {
tmpMsg, _ := template.New("Response").Parse(strings.Join(message.GlobalWelcomeMessage, "\n"))
var message bytes.Buffer
err := tmpMsg.Execute(&message, data)
if err != nil {
p.API.LogError("Failed to execute message template", "Error", err.Error())
}

post := &model.Post{
Message: message.String(),
UserId: p.botUserID,
ChannelId: data.DirectMessage.Id,
}

if _, err := p.API.CreatePost(post); err != nil {
p.API.LogError("We could not create the response post", "UserID", post.UserId, "Error", err.Error())
}

return
}
}
}

// UserHasJoinedTeam is invoked after the membership has been committed to the database. If
// actor is not nil, the user was added to the team by the actor.
func (p *Plugin) UserHasJoinedTeam(c *plugin.Context, teamMember *model.TeamMember, actor *model.User) {
Expand All @@ -22,7 +60,7 @@ func (p *Plugin) UserHasJoinedTeam(c *plugin.Context, teamMember *model.TeamMemb
continue
}

if message.TeamName == data.Team.Name {
if message.TeamName == data.Team.Name && len(message.GlobalWelcomeMessage) == 0 {
go p.processWelcomeMessage(*data, *message)
}
}
Expand Down
8 changes: 8 additions & 0 deletions server/message_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,11 @@ type MessageTemplate struct {
DirectMessage *model.Channel
UserDisplayName string
}

// GloablMessageTemplate represents all the data that can be used in the template for a welcomebot global message
type GloablMessageTemplate struct {
WelcomeBot *model.User
User *model.User
DirectMessage *model.Channel
UserDisplayName string
}
23 changes: 23 additions & 0 deletions server/welcomebot.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,29 @@ func (p *Plugin) constructMessageTemplate(userID, teamID string) *MessageTemplat
return data
}

func (p *Plugin) constructGlobalMessageTemplate(userID string) *GloablMessageTemplate {
data := &GloablMessageTemplate{}
var err *model.AppError

if len(userID) > 0 {
if data.User, err = p.API.GetUser(userID); err != nil {
p.API.LogError("failed to query user", "user_id", userID)
return nil
}
}

if data.User != nil {
if data.DirectMessage, err = p.API.GetDirectChannel(userID, p.botUserID); err != nil {
p.API.LogError("failed to query direct message channel", "user_id", userID)
return nil
}
}

data.UserDisplayName = data.User.GetDisplayName(model.ShowNicknameFullName)

return data
}

func (p *Plugin) getSiteURL() string {
siteURL := "http://localhost:8065"

Expand Down

0 comments on commit 9878691

Please sign in to comment.