-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathplugin.go
64 lines (52 loc) · 1.77 KB
/
plugin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package main
import (
"sync"
"github.com/mattermost/mattermost-server/v5/model"
"github.com/mattermost/mattermost-server/v5/plugin"
"github.com/pkg/errors"
)
const (
botUsername = "community"
botDisplayName = "Community Bot"
botDescription = "Created by the Community plugin."
)
// Plugin is the object to run the plugin
type Plugin struct {
plugin.MattermostPlugin
// configurationLock synchronizes access to the configuration.
configurationLock sync.RWMutex
// configuration is the active plugin configuration. Consult getConfiguration and
// setConfiguration for usage.
configuration *configuration
// botUserID is the ID of the community bot user
botUserID string
}
var _ = manifest // Fix unused linter error
// OnActivate creates a github client with the access token from the configurations,
// creates a bot account, if it doesn't exist and registers the /community slash command
func (p *Plugin) OnActivate() error {
bot := &model.Bot{
Username: botUsername,
DisplayName: botDisplayName,
Description: botDescription,
}
botUserID, appErr := p.Helpers.EnsureBot(bot)
if appErr != nil {
return errors.Wrap(appErr, "failed to ensure bot user")
}
p.botUserID = botUserID
err := p.API.RegisterCommand(getCommand())
if err != nil {
return errors.Wrap(err, "failed to register new command")
}
return nil
}
// SendEphemeralPost sends a ephemeral message in a given channel to a given user
func (p *Plugin) SendEphemeralPost(channelID, userID, message string) {
// This is mostly taken from https://github.com/mattermost/mattermost-server/blob/master/app/command.go#L304
ephemeralPost := &model.Post{}
ephemeralPost.ChannelId = channelID
ephemeralPost.UserId = p.botUserID
ephemeralPost.Message = message
_ = p.API.SendEphemeralPost(userID, ephemeralPost)
}