-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathplugin.go
More file actions
211 lines (190 loc) · 4.57 KB
/
Copy pathplugin.go
File metadata and controls
211 lines (190 loc) · 4.57 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package main
import (
"errors"
"fmt"
"html"
"log"
"net/url"
"regexp"
"strings"
"github.com/gotify/plugin-api"
"github.com/nlopes/slack"
)
// GetGotifyPluginInfo returns gotify plugin info.
func GetGotifyPluginInfo() plugin.Info {
return plugin.Info{
ModulePath: "git.marceltransier.de/gotify-slack",
Author: "Marcel Transier",
Website: "https://git.marceltransier.de/gotify-slack",
Description: "Slack push notifications for gotify",
License: "MIT",
Name: "gotify-slack",
}
}
// Plugin is the gotify plugin instance.
type Plugin struct {
enabled bool
msgHandler plugin.MessageHandler
config *Config
api *slack.Client
rtm *slack.RTM
uid string
team string
}
// Config is a user plugin configuration.
type Config struct {
SlackToken string
}
// Valid checks whether the API token in the config is valid.
func (conf *Config) Valid() bool {
api := slack.New(conf.SlackToken)
_, err := api.AuthTest()
return err == nil
}
// DefaultConfig implements plugin.Configurer.
func (c *Plugin) DefaultConfig() interface{} {
return &Config{}
}
// ValidateAndSetConfig implements plugin.Configurer.
func (c *Plugin) ValidateAndSetConfig(conf interface{}) error {
config := conf.(*Config)
if config.SlackToken == "" {
return c.stopRTM()
}
if !config.Valid() {
return errors.New("the token is invalid")
}
c.config = config
if !c.enabled {
return nil
}
err := c.stopRTM()
if err != nil {
return err
}
return c.startRTM()
}
var mentionRe = regexp.MustCompile(`<@[^>]+>`)
func (c *Plugin) startRTM() error {
c.api = slack.New(c.config.SlackToken)
atr, err := c.api.AuthTest()
if err != nil {
log.Println(err)
return err
}
c.uid = atr.UserID
c.team = atr.Team
c.rtm = c.api.NewRTM()
go c.rtm.ManageConnection()
for msg := range c.rtm.IncomingEvents {
switch ev := msg.Data.(type) {
case *slack.MessageEvent:
channel, err := c.api.GetConversationInfo(ev.Msg.Channel, true)
if err != nil {
log.Println(err)
continue
}
uid := ev.Msg.User
text := ev.Msg.Text
edited := false
if ev.SubMessage != nil && ev.SubMessage.Edited != nil {
uid = ev.SubMessage.User
text = ev.PreviousMessage.Text + "\n-----\n" + ev.SubMessage.Text
edited = true
}
user, err := c.api.GetUserInfo(uid)
if err != nil {
log.Println(err)
continue
}
if user.ID == c.uid {
continue
}
title := "Slack | " + c.team + " | "
if channel.Name != "" {
title += channel.Name + " | "
}
title += user.RealName
if edited {
title += " [Edit]"
}
msgtext := mentionRe.ReplaceAllStringFunc(text, func(s string) string {
userid := strings.Trim(s, "<@>")
user, err := c.api.GetUserInfo(userid)
if err != nil {
return "@Error"
}
return fmt.Sprintf("<@%s>", user.RealName)
})
msgtext = html.UnescapeString(msgtext)
if len(ev.Msg.Attachments) != 0 {
for _, att := range ev.Msg.Attachments {
msgtext += "\n> " + att.Fallback
}
}
if len(ev.Msg.Files) != 0 {
var titles []string
for _, file := range ev.Msg.Files {
titles = append(titles, file.Title)
}
msgtext += "\nFiles: " + strings.Join(titles, ", ")
}
c.msgHandler.SendMessage(plugin.Message{
Title: title,
Message: msgtext,
Priority: 5,
})
case *slack.InvalidAuthEvent:
return errors.New("invalid credentials")
}
}
return nil
}
func (c *Plugin) stopRTM() error {
if c.rtm == nil {
c.api = nil
return nil
}
return c.rtm.Disconnect()
}
// Enable enables the plugin.
func (c *Plugin) Enable() error {
if c.config == nil {
return errors.New("please configure the slack api token first")
}
if !c.config.Valid() {
return errors.New("the slack api token is not valid anymore")
}
c.enabled = true
go c.startRTM()
return nil
}
// Disable disables the plugin.
func (c *Plugin) Disable() error {
err := c.stopRTM()
if err != nil {
return err
}
c.enabled = false
return nil
}
// GetDisplay implements plugin.Displayer.
func (c *Plugin) GetDisplay(location *url.URL) string {
return fmt.Sprintf(`
## Status
- Plugin enabled: %t
- Valid API token: %t
Tip: You can get your API token [here](https://api.slack.com/custom-integrations/legacy-tokens).
`, c.enabled, c.config != nil)
}
// SetMessageHandler implements plugin.Messenger.
func (c *Plugin) SetMessageHandler(h plugin.MessageHandler) {
c.msgHandler = h
}
// NewGotifyPluginInstance creates a plugin instance for a user context.
func NewGotifyPluginInstance(ctx plugin.UserContext) plugin.Plugin {
return &Plugin{}
}
func main() {
panic("this should be built as go plugin")
}