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

[MM-387] Add a global welcome message field #131

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
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.getGlobalMessageTemplateData(user.Id)
if data == nil {
return
}

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

if len(message.GlobalWelcomeMessage) > 0 {
ayusht2810 marked this conversation as resolved.
Show resolved Hide resolved
tmpMsg, _ := template.New(templateNameResponse).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
ayusht2810 marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

// 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 {
ayusht2810 marked this conversation as resolved.
Show resolved Hide resolved
WelcomeBot *model.User
User *model.User
DirectMessage *model.Channel
UserDisplayName string
}
2 changes: 2 additions & 0 deletions server/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const (
botDisplayName = "Welcomebot"
botDescription = "A bot account created by the Welcomebot plugin."

templateNameResponse = "Response"

welcomebotChannelWelcomeKey = "chanmsg_"
)

Expand Down
27 changes: 25 additions & 2 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) getGlobalMessageTemplateData(userID string) *GloablMessageTemplate {
ayusht2810 marked this conversation as resolved.
Show resolved Hide resolved
data := &GloablMessageTemplate{}
var err *model.AppError
ayusht2810 marked this conversation as resolved.
Show resolved Hide resolved

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 Expand Up @@ -133,7 +156,7 @@ func (p *Plugin) renderWelcomeMessage(messageTemplate MessageTemplate, configMes
}
}

tmpMsg, _ := template.New("Response").Parse(strings.Join(configMessage.Message, "\n"))
tmpMsg, _ := template.New(templateNameResponse).Parse(strings.Join(configMessage.Message, "\n"))
var message bytes.Buffer
err := tmpMsg.Execute(&message, messageTemplate)
if err != nil {
Expand Down Expand Up @@ -202,7 +225,7 @@ func (p *Plugin) processActionMessage(messageTemplate MessageTemplate, action *A
p.joinChannel(action, channelName)
}

tmpMsg, _ := template.New("Response").Parse(strings.Join(configMessageAction.ActionSuccessfulMessage, "\n"))
tmpMsg, _ := template.New(templateNameResponse).Parse(strings.Join(configMessageAction.ActionSuccessfulMessage, "\n"))
var message bytes.Buffer
err := tmpMsg.Execute(&message, messageTemplate)
if err != nil {
Expand Down