From e34d2fad467ccb3bf8e0eb2e244a742fa22c22db Mon Sep 17 00:00:00 2001 From: Paul Rothrock Date: Mon, 24 May 2021 18:18:20 -0400 Subject: [PATCH 1/7] Added the ability to send DMs from bot accounts instead of just inviting to channels Signed-off-by: Paul Rothrock --- README.md | 6 +++-- plugin.json | 2 +- server/action_context.go | 1 + server/configuration.go | 3 +++ server/manifest.go | 2 +- server/welcomebot.go | 55 ++++++++++++++++++++++++++++++++++++---- 6 files changed, 60 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 6884cad8c..9e38b6808 100644 --- a/README.md +++ b/README.md @@ -45,14 +45,16 @@ To configure the Welcome Bot, edit your `config.json` file with a message you wa "ActionType": "button", "ActionDisplayName": "User Action", "ActionName": "action-name", + "ActionDirectMessagePost": "Message send to new direct messages", "ActionSuccessfulMessage": [ "Message posted after the user takes this action and joins channels specified by 'ChannelsAddedTo'." ], - "ChannelsAddedTo": ["channel-1", "channel-2"] + "ChannelsAddedTo": ["channel-1", "channel-2", "@example-bot"] }, { "ActionType": "automatic", - "ChannelsAddedTo": ["channel-3", "channel-4"] + "ActionDirectMessagePost": "Message send to new direct messages", + "ChannelsAddedTo": ["channel-3", "channel-4", "@another-bot"] } ] } diff --git a/plugin.json b/plugin.json index 2a3b43f26..1b0f3415c 100644 --- a/plugin.json +++ b/plugin.json @@ -4,7 +4,7 @@ "description": "This plugin adds a WelcomeBot that helps add new users to channels.", "homepage_url": "https://github.com/mattermost/mattermost-plugin-welcomebot", "support_url": "https://github.com/mattermost/mattermost-plugin-welcomebot/issues", - "version": "1.2.0", + "version": "1.3.0", "min_server_version": "5.12.0", "server": { "executables": { diff --git a/server/action_context.go b/server/action_context.go index 2196807ad..de1a0567f 100644 --- a/server/action_context.go +++ b/server/action_context.go @@ -5,6 +5,7 @@ type ActionContext struct { TeamID string `json:"team_id"` UserID string `json:"user_id"` Action string `json:"action"` + DirectMessagePost string `json:"direct_message_post"` } // Action type for decoding action buttons diff --git a/server/configuration.go b/server/configuration.go index edf93ca5f..d98f450b2 100644 --- a/server/configuration.go +++ b/server/configuration.go @@ -19,6 +19,9 @@ type ConfigMessageAction struct { // The message that's display after this action was successful ActionSuccessfulMessage []string + // The message that is posted to direct message channels + ActionDirectMessagePost string + // The names of the channels that a users should be added to ChannelsAddedTo []string } diff --git a/server/manifest.go b/server/manifest.go index 38e43ab9c..99642bdb0 100644 --- a/server/manifest.go +++ b/server/manifest.go @@ -7,5 +7,5 @@ var manifest = struct { Version string }{ ID: "com.mattermost.welcomebot", - Version: "1.2.0", + Version: "1.3.0", } diff --git a/server/welcomebot.go b/server/welcomebot.go index 6deb0eadd..d9703f911 100644 --- a/server/welcomebot.go +++ b/server/welcomebot.go @@ -109,6 +109,7 @@ func (p *Plugin) renderWelcomeMessage(messageTemplate MessageTemplate, configMes action.Context = &ActionContext{} action.Context.TeamID = messageTemplate.Team.Id action.Context.UserID = messageTemplate.User.Id + action.Context.DirectMessagePost = configAction.ActionDirectMessagePost action.Context.Action = "automatic" for _, channelName := range configAction.ChannelsAddedTo { @@ -124,6 +125,7 @@ func (p *Plugin) renderWelcomeMessage(messageTemplate MessageTemplate, configMes "action": configAction.ActionName, "team_id": messageTemplate.Team.Id, "user_id": messageTemplate.User.Id, + "direct_message_post": configAction.ActionDirectMessagePost, }, URL: fmt.Sprintf("%v/plugins/%v/addchannels", p.getSiteURL(), manifest.ID), }, @@ -228,12 +230,55 @@ func (p *Plugin) processActionMessage(messageTemplate MessageTemplate, action *A } func (p *Plugin) joinChannel(action *Action, channelName string) { - if channel, err := p.API.GetChannelByName(action.Context.TeamID, channelName, false); err == nil { - if _, err := p.API.AddChannelMember(channel.Id, action.Context.UserID); err != nil { - p.API.LogError("Couldn't add user to the channel, continuing to next channel", "user_id", action.Context.UserID, "channel_id", channel.Id) + // If it begins with @ create a DM channel + if strings.HasPrefix(channelName, "@") { + r := []rune(channelName) + dm_user, userErr := p.API.GetUserByUsername(string(r[1:])) + + if userErr != nil { + p.API.LogError("Couldn't find DM user, continuing to next channel", "channelName", channelName) + return + } + + if !dm_user.IsBot { + p.API.LogError("Specified DM user is not a bot, continuing to next channel", "channelName", channelName) + return + } + + dm_channel, err := p.API.GetDirectChannel(dm_user.Id, action.Context.UserID) + + if err != nil { + p.API.LogError("Couldn't create or get DM channel, continuing to next channel", "user_id", action.Context.UserID, "channelName", channelName, "channel_id", dm_channel.Id) + return + } + + dm_message := "Welcome to the team!" + if len(action.Context.DirectMessagePost) != 0 { + dm_message = action.Context.DirectMessagePost + } + + post := &model.Post{ + Message: dm_message, + ChannelId: dm_channel.Id, + UserId: dm_user.Id, + } + + if _, err := p.API.CreatePost(post); err != nil { + p.API.LogError( + "Could not create direct message post", + "user_id", post.UserId, + "err", err.Error(), + ) + } + } else { // Otherwise treat it like a normal channel + if channel, err := p.API.GetChannelByName(action.Context.TeamID, channelName, false); err == nil { + if _, err := p.API.AddChannelMember(channel.Id, action.Context.UserID); err != nil { + p.API.LogError("Couldn't add user to the channel, continuing to next channel", "channel_name", channelName, "user_id", action.Context.UserID, channel.Id) + return + } + } else { + p.API.LogError("failed to get channel, continuing to the next channel", "channel_name", channelName, "user_id", action.Context.UserID) return } - } else { - p.API.LogError("failed to get channel, continuing to the next channel", "channel_name", channelName, "user_id", action.Context.UserID) } } From adcac19bce45b9fa47231fade481e92c54686601 Mon Sep 17 00:00:00 2001 From: Paul Rothrock Date: Mon, 21 Jun 2021 14:20:12 -0400 Subject: [PATCH 2/7] Removed underscores from variable names MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 🐪 > 🐍 --- server/welcomebot.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/server/welcomebot.go b/server/welcomebot.go index d9703f911..9c2b9f7a2 100644 --- a/server/welcomebot.go +++ b/server/welcomebot.go @@ -233,34 +233,34 @@ func (p *Plugin) joinChannel(action *Action, channelName string) { // If it begins with @ create a DM channel if strings.HasPrefix(channelName, "@") { r := []rune(channelName) - dm_user, userErr := p.API.GetUserByUsername(string(r[1:])) + dmUser, userErr := p.API.GetUserByUsername(string(r[1:])) if userErr != nil { p.API.LogError("Couldn't find DM user, continuing to next channel", "channelName", channelName) return } - if !dm_user.IsBot { + if !dmUser.IsBot { p.API.LogError("Specified DM user is not a bot, continuing to next channel", "channelName", channelName) return } - dm_channel, err := p.API.GetDirectChannel(dm_user.Id, action.Context.UserID) + dmChannel, err := p.API.GetDirectChannel(dmUser.Id, action.Context.UserID) if err != nil { - p.API.LogError("Couldn't create or get DM channel, continuing to next channel", "user_id", action.Context.UserID, "channelName", channelName, "channel_id", dm_channel.Id) + p.API.LogError("Couldn't create or get DM channel, continuing to next channel", "user_id", action.Context.UserID, "channelName", channelName, "channel_id", dmChannel.Id) return } - dm_message := "Welcome to the team!" + dmMessage := "Welcome to the team!" if len(action.Context.DirectMessagePost) != 0 { - dm_message = action.Context.DirectMessagePost + dmMessage = action.Context.DirectMessagePost } post := &model.Post{ - Message: dm_message, - ChannelId: dm_channel.Id, - UserId: dm_user.Id, + Message: dmMessage, + ChannelId: dmChannel.Id, + UserId: dmUser.Id, } if _, err := p.API.CreatePost(post); err != nil { From ddff18a7b189a3eb6bc26e6a66227bed0629e2af Mon Sep 17 00:00:00 2001 From: kshitij Date: Mon, 15 Apr 2024 20:32:18 +0530 Subject: [PATCH 3/7] [PR-70]: Fixed lint errors --- server/action_context.go | 6 +++--- server/welcomebot.go | 14 +++++++------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/server/action_context.go b/server/action_context.go index 4c1dd4110..f36277687 100644 --- a/server/action_context.go +++ b/server/action_context.go @@ -2,9 +2,9 @@ package main // ActionContext passed from action buttons type ActionContext struct { - TeamID string `json:"team_id"` - UserID string `json:"user_id"` - Action string `json:"action"` + TeamID string `json:"team_id"` + UserID string `json:"user_id"` + Action string `json:"action"` DirectMessagePost string `json:"direct_message_post"` } diff --git a/server/welcomebot.go b/server/welcomebot.go index 5c72dd73e..32dfa517f 100644 --- a/server/welcomebot.go +++ b/server/welcomebot.go @@ -122,9 +122,9 @@ func (p *Plugin) renderWelcomeMessage(messageTemplate MessageTemplate, configMes Name: configAction.ActionDisplayName, Integration: &model.PostActionIntegration{ Context: map[string]interface{}{ - "action": configAction.ActionName, - "team_id": messageTemplate.Team.Id, - "user_id": messageTemplate.User.Id, + "action": configAction.ActionName, + "team_id": messageTemplate.Team.Id, + "user_id": messageTemplate.User.Id, "direct_message_post": configAction.ActionDirectMessagePost, }, URL: fmt.Sprintf("%v/plugins/%v/addchannels", p.getSiteURL(), manifest.ID), @@ -234,7 +234,7 @@ func (p *Plugin) joinChannel(action *Action, channelName string) { if strings.HasPrefix(channelName, "@") { r := []rune(channelName) dmUser, userErr := p.API.GetUserByUsername(string(r[1:])) - + if userErr != nil { p.API.LogError("Couldn't find DM user, continuing to next channel", "channelName", channelName) return @@ -244,18 +244,18 @@ func (p *Plugin) joinChannel(action *Action, channelName string) { p.API.LogError("Specified DM user is not a bot, continuing to next channel", "channelName", channelName) return } - + dmChannel, err := p.API.GetDirectChannel(dmUser.Id, action.Context.UserID) if err != nil { p.API.LogError("Couldn't create or get DM channel, continuing to next channel", "user_id", action.Context.UserID, "channelName", channelName, "channel_id", dmChannel.Id) return } - + dmMessage := "Welcome to the team!" if len(action.Context.DirectMessagePost) != 0 { dmMessage = action.Context.DirectMessagePost - } + } post := &model.Post{ Message: dmMessage, From 77baa81aedd06485902c48446c566aa9df0d424c Mon Sep 17 00:00:00 2001 From: kshitij Date: Mon, 15 Apr 2024 21:17:36 +0530 Subject: [PATCH 4/7] [PR-70]: Fixed review comments --- server/welcomebot.go | 73 ++++++++++++++++++++++---------------------- 1 file changed, 37 insertions(+), 36 deletions(-) diff --git a/server/welcomebot.go b/server/welcomebot.go index 32dfa517f..170324789 100644 --- a/server/welcomebot.go +++ b/server/welcomebot.go @@ -8,6 +8,7 @@ import ( "time" "github.com/mattermost/mattermost-server/v6/model" + "github.com/pkg/errors" ) func (p *Plugin) constructMessageTemplate(userID, teamID string) *MessageTemplate { @@ -232,44 +233,10 @@ func (p *Plugin) processActionMessage(messageTemplate MessageTemplate, action *A func (p *Plugin) joinChannel(action *Action, channelName string) { // If it begins with @ create a DM channel if strings.HasPrefix(channelName, "@") { - r := []rune(channelName) - dmUser, userErr := p.API.GetUserByUsername(string(r[1:])) - - if userErr != nil { - p.API.LogError("Couldn't find DM user, continuing to next channel", "channelName", channelName) - return - } - - if !dmUser.IsBot { - p.API.LogError("Specified DM user is not a bot, continuing to next channel", "channelName", channelName) - return - } - - dmChannel, err := p.API.GetDirectChannel(dmUser.Id, action.Context.UserID) - - if err != nil { - p.API.LogError("Couldn't create or get DM channel, continuing to next channel", "user_id", action.Context.UserID, "channelName", channelName, "channel_id", dmChannel.Id) - return - } - - dmMessage := "Welcome to the team!" - if len(action.Context.DirectMessagePost) != 0 { - dmMessage = action.Context.DirectMessagePost - } - - post := &model.Post{ - Message: dmMessage, - ChannelId: dmChannel.Id, - UserId: dmUser.Id, + if err := p.handleDMs(action,channelName); err != nil { + p.API.LogError("failed to handle DM channel, continuing to next channel. " + err.Error()) } - if _, err := p.API.CreatePost(post); err != nil { - p.API.LogError( - "Could not create direct message post", - "user_id", post.UserId, - "err", err.Error(), - ) - } } else { // Otherwise treat it like a normal channel if channel, err := p.API.GetChannelByName(action.Context.TeamID, channelName, false); err == nil { if _, err := p.API.AddChannelMember(channel.Id, action.Context.UserID); err != nil { @@ -282,3 +249,37 @@ func (p *Plugin) joinChannel(action *Action, channelName string) { } } } + +func (p *Plugin) handleDMs(action *Action, channelName string) error { + username := channelName[1:] + dmUser, userErr := p.API.GetUserByUsername(username) + if userErr != nil { + return errors.Wrapf(userErr, "couldn't find DM channel for username %s", username) + } + + if !dmUser.IsBot { + return errors.Wrapf(userErr, "Specified DM user is not a bot for username %s", username) + } + + dmChannel, err := p.API.GetDirectChannel(dmUser.Id, action.Context.UserID) + if err != nil { + return errors.Wrapf(err, "Couldn't create or get DM channel for user_id %s and channel_id %s" , action.Context.UserID, dmChannel.Id) + } + + dmMessage := "Welcome to the team!" + if len(action.Context.DirectMessagePost) != 0 { + dmMessage = action.Context.DirectMessagePost + } + + post := &model.Post{ + Message: dmMessage, + ChannelId: dmChannel.Id, + UserId: dmUser.Id, + } + + if _, err := p.API.CreatePost(post); err != nil { + return errors.Wrapf(err, "Could not create direct message for user_id %s", post.UserId) + } + + return nil +} From efbc6382e1039dd69fa2d8b7c514d4b5f7d80beb Mon Sep 17 00:00:00 2001 From: kshitij Date: Mon, 15 Apr 2024 21:21:12 +0530 Subject: [PATCH 5/7] [PR-70]: Fixed lint errors --- server/welcomebot.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server/welcomebot.go b/server/welcomebot.go index 170324789..277759576 100644 --- a/server/welcomebot.go +++ b/server/welcomebot.go @@ -233,7 +233,7 @@ func (p *Plugin) processActionMessage(messageTemplate MessageTemplate, action *A func (p *Plugin) joinChannel(action *Action, channelName string) { // If it begins with @ create a DM channel if strings.HasPrefix(channelName, "@") { - if err := p.handleDMs(action,channelName); err != nil { + if err := p.handleDMs(action, channelName); err != nil { p.API.LogError("failed to handle DM channel, continuing to next channel. " + err.Error()) } @@ -263,7 +263,7 @@ func (p *Plugin) handleDMs(action *Action, channelName string) error { dmChannel, err := p.API.GetDirectChannel(dmUser.Id, action.Context.UserID) if err != nil { - return errors.Wrapf(err, "Couldn't create or get DM channel for user_id %s and channel_id %s" , action.Context.UserID, dmChannel.Id) + return errors.Wrapf(err, "Couldn't create or get DM channel for user_id %s and channel_id %s", action.Context.UserID, dmChannel.Id) } dmMessage := "Welcome to the team!" From fe53689b188ddd03b89e4110394a907d1b2ac3be Mon Sep 17 00:00:00 2001 From: kshitij Date: Mon, 15 Apr 2024 21:25:14 +0530 Subject: [PATCH 6/7] [PR-70]: Removed trailing whitespace --- server/welcomebot.go | 1 - 1 file changed, 1 deletion(-) diff --git a/server/welcomebot.go b/server/welcomebot.go index 277759576..3f319c99f 100644 --- a/server/welcomebot.go +++ b/server/welcomebot.go @@ -236,7 +236,6 @@ func (p *Plugin) joinChannel(action *Action, channelName string) { if err := p.handleDMs(action, channelName); err != nil { p.API.LogError("failed to handle DM channel, continuing to next channel. " + err.Error()) } - } else { // Otherwise treat it like a normal channel if channel, err := p.API.GetChannelByName(action.Context.TeamID, channelName, false); err == nil { if _, err := p.API.AddChannelMember(channel.Id, action.Context.UserID); err != nil { From 90f3ba907edc38a42875c48dfc71376d3ad781ca Mon Sep 17 00:00:00 2001 From: kshitij Date: Thu, 16 May 2024 17:55:39 +0530 Subject: [PATCH 7/7] Fixed review comments --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 6625c2b48..d40a5d1d8 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ Use this plugin to improve onboarding and HR processes. It adds a Welcome Bot th To configure the Welcome Bot, edit your `config.json` file with a message you want to send to a user in the following format: -``` +```json "Plugins": { "com.mattermost.welcomebot": { "WelcomeMessages": [