From e956e0c8d4e65e605565d122a0848a97e8f2b477 Mon Sep 17 00:00:00 2001 From: ayusht2810 Date: Tue, 16 Apr 2024 15:41:32 +0530 Subject: [PATCH 1/2] [MM-382] Add feature to use struct fields in the channel welcome message --- README.md | 10 ++++++- server/hooks.go | 11 ++++++-- server/message_template.go | 8 ++++++ server/welcomebot.go | 54 +++++++++++++++++++++++++++++++------- 4 files changed, 70 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index c8db5f98e..c01b9443a 100644 --- a/README.md +++ b/README.md @@ -83,7 +83,15 @@ The preview of the configured messages, as well as the creation of a channel wel * `/welcomebot help` - Displays usage information. * `/welcomebot list` - Lists the teams for which greetings were defined. * `/welcomebot preview [team-name]` - Sends ephemeral messages to the user calling the command, with the preview of the welcome message[s] for the given team name and the user that requested the preview. -* `/welcomebot set_channel_welcome [welcome-message]` - Sets the given text as current's channel welcome message. +* `/welcomebot set_channel_welcome [welcome-message]` - Sets the given text as current's channel welcome message. You can also include `{{.UserDisplayName}}` and `{{.Channel.DisplayName}}` in the command to print the user joined and the channel display name in the message, respectively. You can insert any variable from the `MessageTemplate` struct, which has the following fields: + ```go + type MessageTemplate struct { + WelcomeBot *model.User + User *model.User + DirectMessage *model.Channel + UserDisplayName string + } + ``` * `/welcomebot get_channel_welcome` - Gets the current channel's welcome message. * `/welcomebot delete_channel_welcome` - Deletes the current channel's welcome message. diff --git a/server/hooks.go b/server/hooks.go index 25b1b744e..751afed44 100644 --- a/server/hooks.go +++ b/server/hooks.go @@ -69,13 +69,20 @@ func (p *Plugin) UserHasJoinedChannel(c *plugin.Context, channelMember *model.Ch return } + templateData := p.constructChannelMessageTemplate(channelMember.UserId, channelMember.ChannelId) + if templateData == nil { + return + } + + message := p.getTemplateMessage([]string{string(data)}, templateData) + // We send a DM and an opportunistic ephemeral message to the channel. See // the discussion at the link below for more details: // https://github.com/mattermost/mattermost-plugin-welcomebot/pull/31#issuecomment-611691023 postDM := &model.Post{ UserId: p.botUserID, ChannelId: dmChannel.Id, - Message: string(data), + Message: message, } if _, appErr := p.API.CreatePost(postDM); appErr != nil { mlog.Error("failed to post welcome message to the channel", @@ -87,7 +94,7 @@ func (p *Plugin) UserHasJoinedChannel(c *plugin.Context, channelMember *model.Ch postChannel := &model.Post{ UserId: p.botUserID, ChannelId: channelMember.ChannelId, - Message: string(data), + Message: message, } time.Sleep(1 * time.Second) _ = p.API.SendEphemeralPost(channelMember.UserId, postChannel) diff --git a/server/message_template.go b/server/message_template.go index 717b36bc0..b00ffd2aa 100644 --- a/server/message_template.go +++ b/server/message_template.go @@ -11,3 +11,11 @@ type MessageTemplate struct { DirectMessage *model.Channel UserDisplayName string } + +type ChannelMessageTemplate struct { + WelcomeBot *model.User + User *model.User + Channel *model.Channel + DirectMessage *model.Channel + UserDisplayName string +} diff --git a/server/welcomebot.go b/server/welcomebot.go index 7f65ad45f..655165381 100644 --- a/server/welcomebot.go +++ b/server/welcomebot.go @@ -45,6 +45,37 @@ func (p *Plugin) constructMessageTemplate(userID, teamID string) *MessageTemplat return data } +func (p *Plugin) constructChannelMessageTemplate(userID, channelID string) *ChannelMessageTemplate { + data := &ChannelMessageTemplate{} + 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", "UserID", 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", "UserID", userID) + return nil + } + } + + if len(channelID) > 0 { + data.Channel, err = p.API.GetChannel(channelID) + if err != nil { + p.API.LogError("failed to query channel", "ChannelID", channelID) + return nil + } + } + + data.UserDisplayName = data.User.GetDisplayName(model.ShowNicknameFullName) + + return data +} + func (p *Plugin) getSiteURL() string { siteURL := "http://localhost:8065" @@ -133,18 +164,10 @@ func (p *Plugin) renderWelcomeMessage(messageTemplate MessageTemplate, configMes } } - tmpMsg, _ := template.New("Response").Parse(strings.Join(configMessage.Message, "\n")) - var message bytes.Buffer - err := tmpMsg.Execute(&message, messageTemplate) - if err != nil { - p.API.LogError( - "Failed to execute message template", - "err", err.Error(), - ) - } + message := p.getTemplateMessage(configMessage.Message, messageTemplate) post := &model.Post{ - Message: message.String(), + Message: message, UserId: p.botUserID, } @@ -177,6 +200,17 @@ func (p *Plugin) renderWelcomeMessage(messageTemplate MessageTemplate, configMes return post } +func (p *Plugin) getTemplateMessage(data []string, messageTemplate interface{}) string { + tmpMsg, _ := template.New("Response").Parse(strings.Join(data, "\n")) + var message bytes.Buffer + if appErr := tmpMsg.Execute(&message, messageTemplate); appErr != nil { + p.API.LogError("Failed to execute channel message template", "Error", appErr.Error()) + return "" + } + + return message.String() +} + func (p *Plugin) processWelcomeMessage(messageTemplate MessageTemplate, configMessage ConfigMessage) { time.Sleep(time.Second * time.Duration(configMessage.DelayInSeconds)) From 3cc7637ef88ce09ba5275e2d125b9c0b51ab446e Mon Sep 17 00:00:00 2001 From: ayusht2810 Date: Thu, 11 Jul 2024 15:53:58 +0530 Subject: [PATCH 2/2] [MM-382] Review fixes: Use p.client and update readme --- README.md | 3 ++- server/welcomebot.go | 18 +++++++++--------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index c01b9443a..9fae8d45c 100644 --- a/README.md +++ b/README.md @@ -83,12 +83,13 @@ The preview of the configured messages, as well as the creation of a channel wel * `/welcomebot help` - Displays usage information. * `/welcomebot list` - Lists the teams for which greetings were defined. * `/welcomebot preview [team-name]` - Sends ephemeral messages to the user calling the command, with the preview of the welcome message[s] for the given team name and the user that requested the preview. -* `/welcomebot set_channel_welcome [welcome-message]` - Sets the given text as current's channel welcome message. You can also include `{{.UserDisplayName}}` and `{{.Channel.DisplayName}}` in the command to print the user joined and the channel display name in the message, respectively. You can insert any variable from the `MessageTemplate` struct, which has the following fields: +* `/welcomebot set_channel_welcome [welcome-message]` - Sets the given text as current's channel welcome message. You can also include `{{.UserDisplayName}}` and `{{.Channel.DisplayName}}` in the `welcome-message` to print the user joined and the channel display name in the message, respectively. You can insert any variable from the `MessageTemplate` struct, which has the following fields: ```go type MessageTemplate struct { WelcomeBot *model.User User *model.User DirectMessage *model.Channel + Channel *model.Channel UserDisplayName string } ``` diff --git a/server/welcomebot.go b/server/welcomebot.go index 655165381..c8a7dbe49 100644 --- a/server/welcomebot.go +++ b/server/welcomebot.go @@ -47,26 +47,26 @@ func (p *Plugin) constructMessageTemplate(userID, teamID string) *MessageTemplat func (p *Plugin) constructChannelMessageTemplate(userID, channelID string) *ChannelMessageTemplate { data := &ChannelMessageTemplate{} - var err *model.AppError + var err error if len(userID) > 0 { - if data.User, err = p.API.GetUser(userID); err != nil { - p.API.LogError("failed to query user", "UserID", userID) + if data.User, err = p.client.User.Get(userID); err != nil { + p.client.Log.Error("failed to query user", "UserID", 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", "UserID", userID) + if data.DirectMessage, err = p.client.Channel.GetDirect(userID, p.botUserID); err != nil { + p.client.Log.Error("failed to query direct message channel", "UserID", userID) return nil } } if len(channelID) > 0 { - data.Channel, err = p.API.GetChannel(channelID) + data.Channel, err = p.client.Channel.Get(channelID) if err != nil { - p.API.LogError("failed to query channel", "ChannelID", channelID) + p.client.Log.Error("failed to query channel", "ChannelID", channelID) return nil } } @@ -203,8 +203,8 @@ func (p *Plugin) renderWelcomeMessage(messageTemplate MessageTemplate, configMes func (p *Plugin) getTemplateMessage(data []string, messageTemplate interface{}) string { tmpMsg, _ := template.New("Response").Parse(strings.Join(data, "\n")) var message bytes.Buffer - if appErr := tmpMsg.Execute(&message, messageTemplate); appErr != nil { - p.API.LogError("Failed to execute channel message template", "Error", appErr.Error()) + if err := tmpMsg.Execute(&message, messageTemplate); err != nil { + p.client.Log.Error("Failed to execute channel message template", "Error", err.Error()) return "" }