Skip to content

Commit ced61ae

Browse files
[PR-70]:Added the ability to send DMs from bot accounts
2 parents 502eb34 + adcac19 commit ced61ae

File tree

4 files changed

+83
-32
lines changed

4 files changed

+83
-32
lines changed

README.md

+29-27
Original file line numberDiff line numberDiff line change
@@ -31,38 +31,39 @@ Use this plugin to improve onboarding and HR processes. It adds a Welcome Bot th
3131
To configure the Welcome Bot, edit your `config.json` file with a message you want to send to a user in the following format:
3232

3333
```
34-
"Plugins": {
35-
"com.mattermost.welcomebot": {
36-
"WelcomeMessages": [
34+
"Plugins": {
35+
"com.mattermost.welcomebot": {
36+
"WelcomeMessages": [
37+
{
38+
"TeamName": "your-team-name",
39+
"DelayInSeconds": 3,
40+
"Message": [
41+
"Your welcome message here. Each list item specifies one line in the message text."
42+
],
43+
"AttachmentMessage": [
44+
"Attachment message containing user actions"
45+
],
46+
"Actions" : [
3747
{
38-
"TeamName": "your-team-name",
39-
"DelayInSeconds": 3,
40-
"IncludeGuests": false,
41-
"Message": [
42-
"Your welcome message here. Each list item specifies one line in the message text."
48+
"ActionType": "button",
49+
"ActionDisplayName": "User Action",
50+
"ActionName": "action-name",
51+
"ActionDirectMessagePost": "Message send to new direct messages",
52+
"ActionSuccessfulMessage": [
53+
"Message posted after the user takes this action and joins channels specified by 'ChannelsAddedTo'."
4354
],
44-
"AttachmentMessage": [
45-
"Attachment message containing user actions"
46-
],
47-
"Actions" : [
48-
{
49-
"ActionType": "button",
50-
"ActionDisplayName": "User Action",
51-
"ActionName": "action-name",
52-
"ActionSuccessfulMessage": [
53-
"Message posted after the user takes this action and joins channels specified by 'ChannelsAddedTo'."
54-
],
55-
"ChannelsAddedTo": ["channel-1", "channel-2"]
56-
},
57-
{
58-
"ActionType": "automatic",
59-
"ChannelsAddedTo": ["channel-3", "channel-4"]
60-
}
61-
]
55+
"ChannelsAddedTo": ["channel-1", "channel-2", "@example-bot"]
56+
},
57+
{
58+
"ActionType": "automatic",
59+
"ActionDirectMessagePost": "Message send to new direct messages",
60+
"ChannelsAddedTo": ["channel-3", "channel-4", "@another-bot"]
6261
}
6362
]
6463
}
65-
},
64+
]
65+
}
66+
},
6667
```
6768

6869
where
@@ -78,6 +79,7 @@ where
7879
- **ActionName**: Sets the action name used by the plugin to identify which action is taken by a user.
7980
- **ActionSuccessfulMessage**: Message posted after the user takes this action and joins the specified channels.
8081
- **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
82+
- **ActionDirectMessagePost**: The post to send users when creating the bot direct message channel
8183

8284
The preview of the configured messages, as well as the creation of a channel welcome message, can be done via bot commands:
8385
* `/welcomebot help` - Displays usage information.

server/action_context.go

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ type ActionContext struct {
55
TeamID string `json:"team_id"`
66
UserID string `json:"user_id"`
77
Action string `json:"action"`
8+
DirectMessagePost string `json:"direct_message_post"`
89
}
910

1011
// Action type for decoding action buttons

server/configuration.go

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ type ConfigMessageAction struct {
1919
// The message that's display after this action was successful
2020
ActionSuccessfulMessage []string
2121

22+
// The message that is posted to direct message channels
23+
ActionDirectMessagePost string
24+
2225
// The names of the channels that a users should be added to
2326
ChannelsAddedTo []string
2427
}

server/welcomebot.go

+50-5
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ func (p *Plugin) renderWelcomeMessage(messageTemplate MessageTemplate, configMes
109109
action.Context = &ActionContext{}
110110
action.Context.TeamID = messageTemplate.Team.Id
111111
action.Context.UserID = messageTemplate.User.Id
112+
action.Context.DirectMessagePost = configAction.ActionDirectMessagePost
112113
action.Context.Action = "automatic"
113114

114115
for _, channelName := range configAction.ChannelsAddedTo {
@@ -124,6 +125,7 @@ func (p *Plugin) renderWelcomeMessage(messageTemplate MessageTemplate, configMes
124125
"action": configAction.ActionName,
125126
"team_id": messageTemplate.Team.Id,
126127
"user_id": messageTemplate.User.Id,
128+
"direct_message_post": configAction.ActionDirectMessagePost,
127129
},
128130
URL: fmt.Sprintf("%v/plugins/%v/addchannels", p.getSiteURL(), manifest.ID),
129131
},
@@ -228,12 +230,55 @@ func (p *Plugin) processActionMessage(messageTemplate MessageTemplate, action *A
228230
}
229231

230232
func (p *Plugin) joinChannel(action *Action, channelName string) {
231-
if channel, err := p.API.GetChannelByName(action.Context.TeamID, channelName, false); err == nil {
232-
if _, err := p.API.AddChannelMember(channel.Id, action.Context.UserID); err != nil {
233-
p.API.LogError("Couldn't add user to the channel, continuing to next channel", "user_id", action.Context.UserID, "channel_id", channel.Id)
233+
// If it begins with @ create a DM channel
234+
if strings.HasPrefix(channelName, "@") {
235+
r := []rune(channelName)
236+
dmUser, userErr := p.API.GetUserByUsername(string(r[1:]))
237+
238+
if userErr != nil {
239+
p.API.LogError("Couldn't find DM user, continuing to next channel", "channelName", channelName)
240+
return
241+
}
242+
243+
if !dmUser.IsBot {
244+
p.API.LogError("Specified DM user is not a bot, continuing to next channel", "channelName", channelName)
245+
return
246+
}
247+
248+
dmChannel, err := p.API.GetDirectChannel(dmUser.Id, action.Context.UserID)
249+
250+
if err != nil {
251+
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)
252+
return
253+
}
254+
255+
dmMessage := "Welcome to the team!"
256+
if len(action.Context.DirectMessagePost) != 0 {
257+
dmMessage = action.Context.DirectMessagePost
258+
}
259+
260+
post := &model.Post{
261+
Message: dmMessage,
262+
ChannelId: dmChannel.Id,
263+
UserId: dmUser.Id,
264+
}
265+
266+
if _, err := p.API.CreatePost(post); err != nil {
267+
p.API.LogError(
268+
"Could not create direct message post",
269+
"user_id", post.UserId,
270+
"err", err.Error(),
271+
)
272+
}
273+
} else { // Otherwise treat it like a normal channel
274+
if channel, err := p.API.GetChannelByName(action.Context.TeamID, channelName, false); err == nil {
275+
if _, err := p.API.AddChannelMember(channel.Id, action.Context.UserID); err != nil {
276+
p.API.LogError("Couldn't add user to the channel, continuing to next channel", "channel_name", channelName, "user_id", action.Context.UserID, channel.Id)
277+
return
278+
}
279+
} else {
280+
p.API.LogError("failed to get channel, continuing to the next channel", "channel_name", channelName, "user_id", action.Context.UserID)
234281
return
235282
}
236-
} else {
237-
p.API.LogError("failed to get channel, continuing to the next channel", "channel_name", channelName, "user_id", action.Context.UserID)
238283
}
239284
}

0 commit comments

Comments
 (0)