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

[PR-70]: Added the ability to send DMs from bot accounts #129

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Next Next commit
Added the ability to send DMs from bot accounts instead of just invit…
…ing to channels

Signed-off-by: Paul Rothrock <[email protected]>
  • Loading branch information
icelander committed May 24, 2021
commit e34d2fad467ccb3bf8e0eb2e244a742fa22c22db
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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"]
}
]
}
2 changes: 1 addition & 1 deletion plugin.json
Original file line number Diff line number Diff line change
@@ -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": {
1 change: 1 addition & 0 deletions server/action_context.go
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions server/configuration.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion server/manifest.go
Original file line number Diff line number Diff line change
@@ -7,5 +7,5 @@ var manifest = struct {
Version string
}{
ID: "com.mattermost.welcomebot",
Version: "1.2.0",
Version: "1.3.0",
}
55 changes: 50 additions & 5 deletions server/welcomebot.go
Original file line number Diff line number Diff line change
@@ -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
Copy link
Contributor

Choose a reason for hiding this comment

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

Can return here instead of having the else?

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)
}
}