-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.go
150 lines (128 loc) · 3.48 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"io/ioutil"
"path/filepath"
"github.com/limejuny/mattermost-chatbot/command"
"github.com/limejuny/mattermost-chatbot/config"
"github.com/limejuny/mattermost-chatbot/util"
"github.com/mattermost/mattermost-server/v5/model"
"github.com/mattermost/mattermost-server/v5/plugin"
"github.com/pkg/errors"
"gopkg.in/yaml.v3"
)
const (
botUserName = "chatbot"
botDisplayName = "Chatbot"
botDescription = "Chatbot"
)
type Plugin struct {
plugin.MattermostPlugin
}
func (p *Plugin) OnActivate() error {
config.Mattermost = p.API
if err := p.setUpBotUser(); err != nil {
config.Mattermost.LogError("Failed to create a bot user", "Error", err.Error())
return err
}
if err := p.OnConfigurationChange(); err != nil {
return err
}
cmds := command.GetCommands(p.API)
for _, cmd := range cmds {
p.API.RegisterCommand(cmd)
}
return nil
}
func (p *Plugin) OnConfigurationChange() error {
if config.Mattermost == nil {
return nil
}
bundlePath, err := p.API.GetBundlePath()
if err != nil {
return err
}
serviceFile, err := ioutil.ReadFile(filepath.Join(bundlePath, "site", "msa.yaml"))
if err != nil {
return err
}
yaml.Unmarshal(serviceFile, &config.Service)
swaggerFile, err := ioutil.ReadFile(filepath.Join(bundlePath, "site", "swagger.yaml"))
if err != nil {
return err
}
yaml.Unmarshal(swaggerFile, &config.Swagger)
// 운영
appPFile, err := ioutil.ReadFile(filepath.Join(bundlePath, "site", "app-p.yaml"))
if err != nil {
return err
}
yaml.Unmarshal(appPFile, &config.AppP)
// SI개발
appSDFile, err := ioutil.ReadFile(filepath.Join(bundlePath, "site", "app-sd.yaml"))
if err != nil {
return err
}
yaml.Unmarshal(appSDFile, &config.AppSD)
// SI통시
appSTFile, err := ioutil.ReadFile(filepath.Join(bundlePath, "site", "app-st.yaml"))
if err != nil {
return err
}
yaml.Unmarshal(appSTFile, &config.AppST)
// SM개발
appDFile, err := ioutil.ReadFile(filepath.Join(bundlePath, "site", "app-d.yaml"))
if err != nil {
return err
}
yaml.Unmarshal(appDFile, &config.AppD)
// SM통시
appTFile, err := ioutil.ReadFile(filepath.Join(bundlePath, "site", "app-t.yaml"))
if err != nil {
return err
}
yaml.Unmarshal(appTFile, &config.AppT)
// 교육
appEFile, err := ioutil.ReadFile(filepath.Join(bundlePath, "site", "app-e.yaml"))
if err != nil {
return err
}
yaml.Unmarshal(appEFile, &config.AppE)
return nil
}
func (p *Plugin) setUpBotUser() error {
botUserID, err := p.Helpers.EnsureBot(&model.Bot{
Username: botUserName,
DisplayName: botDisplayName,
Description: botDescription,
})
if err != nil {
config.Mattermost.LogError("Error in setting up bot user", "Error", err.Error())
return err
}
bundlePath, err := p.API.GetBundlePath()
if err != nil {
return err
}
profileImage, err := ioutil.ReadFile(filepath.Join(bundlePath, "assets", "icon.png"))
if err != nil {
return err
}
if appErr := p.API.SetProfileImage(botUserID, profileImage); appErr != nil {
return errors.Wrap(appErr, "couldn't set profile image")
}
config.BotUserID = botUserID
return nil
}
func (p *Plugin) ExecuteCommand(context *plugin.Context, commandArgs *model.CommandArgs) (*model.CommandResponse, *model.AppError) {
args, argErr := util.SplitArgs(commandArgs.Command)
if argErr != nil {
return &model.CommandResponse{
ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL,
Text: argErr.Error(),
}, nil
}
return command.CommandHandler.Handle(commandArgs, args...), nil
}
func main() {
plugin.ClientMain(&Plugin{})
}