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

Added the ability to send DMs from bot accounts #70

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
55 changes: 29 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,37 +28,39 @@ 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:

```
"Plugins": {
"com.mattermost.welcomebot": {
"WelcomeMessages": [
"Plugins": {
"com.mattermost.welcomebot": {
"WelcomeMessages": [
{
"TeamName": "your-team-name",
"DelayInSeconds": 3,
"Message": [
"Your welcome message here. Each list item specifies one line in the message text."
],
"AttachmentMessage": [
"Attachment message containing user actions"
],
"Actions" : [
{
"TeamName": "your-team-name",
"DelayInSeconds": 3,
"Message": [
"Your welcome message here. Each list item specifies one line in the message text."
"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'."
],
"AttachmentMessage": [
"Attachment message containing user actions"
],
"Actions" : [
{
"ActionType": "button",
"ActionDisplayName": "User Action",
"ActionName": "action-name",
"ActionSuccessfulMessage": [
"Message posted after the user takes this action and joins channels specified by 'ChannelsAddedTo'."
],
"ChannelsAddedTo": ["channel-1", "channel-2"]
},
{
"ActionType": "automatic",
"ChannelsAddedTo": ["channel-3", "channel-4"]
}
]
"ChannelsAddedTo": ["channel-1", "channel-2", "@example-bot"]
},
{
"ActionType": "automatic",
"ActionDirectMessagePost": "Message send to new direct messages",
"ChannelsAddedTo": ["channel-3", "channel-4", "@another-bot"]
}
]
}
},
]
}
},
```

where
Expand All @@ -73,6 +75,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
- **ActionDirectMessagePost**: The post to send users when creating the bot direct message channel

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
2 changes: 1 addition & 1 deletion plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"homepage_url": "https://github.com/mattermost/mattermost-plugin-welcomebot",
"support_url": "https://github.com/mattermost/mattermost-plugin-welcomebot/issues",
"release_notes_url": "https://github.com/mattermost/mattermost-plugin-welcomebot/releases/tag/v1.2.0",
"version": "1.2.0",
"version": "1.3.0",
"min_server_version": "5.12.0",
"server": {
"executables": {
Expand Down
1 change: 1 addition & 0 deletions server/action_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions server/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion server/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ var manifest = struct {
Version string
}{
ID: "com.mattermost.welcomebot",
Version: "1.2.0",
Kshitij-Katiyar marked this conversation as resolved.
Show resolved Hide resolved
Version: "1.3.0",
}
55 changes: 50 additions & 5 deletions server/welcomebot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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),
},
Expand Down Expand Up @@ -228,12 +230,55 @@ func (p *Plugin) processActionMessage(messageTemplate MessageTemplate, action *A
}

func (p *Plugin) joinChannel(action *Action, channelName string) {
Kshitij-Katiyar marked this conversation as resolved.
Show resolved Hide resolved
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)
dmUser, userErr := p.API.GetUserByUsername(string(r[1:]))
Kshitij-Katiyar marked this conversation as resolved.
Show resolved Hide resolved

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
}
Kshitij-Katiyar marked this conversation as resolved.
Show resolved Hide resolved

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