/
gomic.go
382 lines (318 loc) · 8.67 KB
/
gomic.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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
package main
import (
"bufio"
"encoding/json"
"fmt"
"github.com/SlyMarbo/rss"
"github.com/asaskevich/govalidator"
"github.com/boltdb/bolt"
"golang.org/x/net/websocket"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"time"
)
var feeds map[string][]FeedBundle
var sentId uint64
var db *bolt.DB
var world = []byte("world")
var FEEDSKEY = []byte("FEEDSKEY")
var BOTAPIKEY = []byte("BOTAPIKEY")
func main() {
botAPIValue := connectToDb()
defer db.Close()
ws := connectToSlack(botAPIValue)
sentId = uint64(0)
go checkFeeds(ws)
go backup()
readMessages(ws)
}
func connectToDb() string {
var err error
db, err = bolt.Open("gomic.db", 0600, nil)
if err != nil {
log.Fatal(err)
}
return initializeDb(db)
}
func initializeDb(db *bolt.DB) string {
var botAPIValue string
err := db.Update(func(tx *bolt.Tx) error {
bucket, err := tx.CreateBucketIfNotExists(world)
if err != nil {
return err
}
if botAPIByteValue := bucket.Get(BOTAPIKEY); botAPIByteValue == nil {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter bot API key: ")
botAPIValue, err = reader.ReadString('\n')
if err != nil {
return err
}
err = bucket.Put(BOTAPIKEY, []byte(botAPIValue))
if err != nil {
return err
}
} else {
botAPIValue = string(botAPIByteValue)
}
feeds = make(map[string][]FeedBundle)
if feedsBytes := bucket.Get(FEEDSKEY); feedsBytes != nil {
json.Unmarshal(feedsBytes, &feeds)
}
return nil
})
if err != nil {
log.Fatal(err)
}
fmt.Println("botapivalue: " + botAPIValue)
return botAPIValue
}
func connectToSlack(botAPIValue string) *websocket.Conn {
fmt.Println("https://slack.com/api/rtm.start?token=" + botAPIValue)
resp, err := http.Get("https://slack.com/api/rtm.start?token=" + strings.TrimSpace(botAPIValue))
if err != nil {
fmt.Println("resp is kil")
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("body is kil")
}
var respObj ResponseRtmStart
err = json.Unmarshal(body, &respObj)
ws, err := websocket.Dial(respObj.Url, "", "https://api.slack.com/")
if err != nil {
fmt.Println("ws is kil")
}
return ws
}
func checkFeeds(ws *websocket.Conn) {
fmt.Println("Calling a check on the feedses")
for channel, feedSlice := range feeds {
go checkFeed(channel, feedSlice, ws)
}
}
func checkFeed(channel string, feedSlice []FeedBundle, ws *websocket.Conn) {
for {
fmt.Println("Checking channel " + channel)
feedSlice = feeds[channel]
if len(feedSlice) > 0 {
for feedIteration := range feedSlice {
feed := feedSlice[feedIteration]
fmt.Println("Checking for updates for " + feed.Url)
feed.ClearItems()
err := feed.Update()
if err != nil {
fmt.Println(err)
}
items := feed.GetItems()
for itemIteration := range items {
item := items[itemIteration]
fmt.Println(item.Title)
postLink(channel, item, ws)
}
feed.RssHandler.Refresh = time.Now()
}
} else {
return
}
time.Sleep(time.Minute * 10)
}
}
func readMessages(ws *websocket.Conn) {
for {
var m Message
err := websocket.JSON.Receive(ws, &m)
if err == nil {
handleMessage(m, ws)
} else {
fmt.Println(err)
}
}
}
func handleMessage(m Message, ws *websocket.Conn) {
fmt.Println(m)
if m.Type == "message" {
text := strings.ToLower(m.Text)
if strings.HasPrefix(text, "gomic add rss") {
addRss(m, ws)
} else if strings.HasPrefix(text, "gomic ls -a") {
listAllRss(m.Channel, ws)
} else if strings.HasPrefix(text, "gomic ls") {
listRssInChannel(m.Channel, ws)
} else if strings.HasPrefix(text, "gomic rm") {
removeRss(m, ws)
} else if strings.HasPrefix(text, "gomic help") {
printHelp(m.Channel, ws)
} else if strings.HasPrefix(text, "gomic") {
sayHi(m.Channel, ws)
}
}
}
func addRss(m Message, ws *websocket.Conn) {
feedUrl := getArgument(m, 3, ws)
if feedUrl != "" {
feedUrl = feedUrl[1 : len(feedUrl)-1]
if govalidator.IsURL(feedUrl) {
channelFeeds := feeds[m.Channel]
for _, f := range channelFeeds {
if f.Url == feedUrl {
sendMessage(m.Channel, "Already tracking that url here, no worries homie :snowboarder:", ws)
return
}
}
var newFeed FeedBundle
var err error
newFeed.Url = feedUrl
newFeed.RssHandler, err = rss.Fetch(feedUrl)
if err != nil {
sendMessage(m.Channel, "Incorrect url :OoOoOoOo", ws)
} else {
createNewFeedChecker := false
if _, exists := feeds[m.Channel]; !exists {
createNewFeedChecker = true
}
feeds[m.Channel] = append(feeds[m.Channel], newFeed)
sendMessage(m.Channel, "Yes, sir. "+feedUrl+" added.", ws)
if createNewFeedChecker {
go checkFeed(m.Channel, feeds[m.Channel], ws)
}
}
} else {
sendMessage(m.Channel, "Incorrect url :OoOoOoOo", ws)
}
}
}
func getArgument(m Message, position int, ws *websocket.Conn) string {
tokens := strings.Split(m.Text, " ")
if len(tokens) <= position {
sendMessage(m.Channel, "You need more arguments :(", ws)
return ""
}
return tokens[position]
}
func listRssInChannel(channel string, ws *websocket.Conn) {
text := getRssInChannel(channel)
if text == "" {
text = "No RSS feeds in this channel :neutral_face:"
}
sendMessage(channel, text, ws)
}
func getRssInChannel(channel string) string {
text := "Listing all feeds in channel <#" + channel + ">"
channelFeeds := feeds[channel]
for _, feed := range channelFeeds {
text = text + "\n" + feed.Url
}
if text == "Listing all feeds in channel <#"+channel+">" {
return ""
} else {
return text
}
}
func listAllRss(channel string, ws *websocket.Conn) {
text := "Listing all RSS feeds\n\n"
for c := range feeds {
channelText := getRssInChannel(c)
if channelText != "" {
text = text + getRssInChannel(c) + "\n"
}
}
if text == "Listing all RSS feeds\n\n" {
fmt.Println(text)
text = "Looks like your team doesn't have any RSS feeds at the moment. You should add some! :wink:"
}
sendMessage(channel, text, ws)
}
func removeRss(m Message, ws *websocket.Conn) {
urlToRemove := getArgument(m, 2, ws)
urlToRemove = urlToRemove[1 : len(urlToRemove)-1]
for c, channelFeeds := range feeds {
for i, f := range channelFeeds {
if f.Url == urlToRemove {
feeds[c] = append(channelFeeds[:i], channelFeeds[i+1:]...)
sendMessage(m.Channel, urlToRemove+" found and annihilated :fire: :sunglasses: :fire: ", ws)
return
}
}
}
sendMessage(m.Channel, "I couldn't find that one... try again :ambulance:", ws)
}
func backup() {
var err error
for {
if err = saveFeeds(); err != nil {
log.Fatal(err)
}
time.Sleep(time.Second * 10)
}
}
func saveFeeds() error {
err := db.Update(func(tx *bolt.Tx) error {
bucket := tx.Bucket(world)
feedsJson, err := json.Marshal(feeds)
if err != nil {
return err
}
return bucket.Put(FEEDSKEY, feedsJson)
})
return err
}
func printHelp(channel string, ws *websocket.Conn) {
sendMessage(channel,
`Hi there! I'm gomic! I'm a general purpose rss slack bot (originally created for posting webcomic feeds). I'm also made in Go! Sometimes I wonder where my name comes from.
OH! You probably want some help. Here's what I can do:
- "gomic add rss {url}" : adds an rss feed to my database for me to automatically check and post updates in the channel it was added from
- "gomic ls [-a]" : Lists all the RSS feeds in the channel you're in. If -a flag is given, lists all RSS feeds in slack team"
- "gomic rm {url}" : removes an rss feed from the database
- "gomic help" : what you did just now! Prints the help!
- "gomic" : In case you just wanted to say hi :)`, ws)
}
func sayHi(channel string, ws *websocket.Conn) {
sendMessage(channel, "Hi, that's my name :simple_smile:", ws)
}
func postLink(channel string, item *rss.Item, ws *websocket.Conn) {
sendMessage(channel, "Hey, have you checked this out "+item.Link+" ?", ws)
}
func sendMessage(channel string, text string, ws *websocket.Conn) {
var botMsg Message
botMsg.Id = sentId
sentId++
botMsg.Type = "message"
botMsg.Channel = channel
botMsg.Text = text
websocket.JSON.Send(ws, botMsg)
}
type FeedBundle struct {
Url string `json:"url"`
RssHandler *rss.Feed `json:"rssHandler"`
}
func (f FeedBundle) Update() error {
return f.RssHandler.Update()
}
func (f FeedBundle) GetItemMap() map[string]struct{} {
return f.RssHandler.ItemMap
}
func (f FeedBundle) GetItems() []*rss.Item {
return f.RssHandler.Items
}
func (f FeedBundle) ClearItems() {
f.RssHandler.Items = make([]*rss.Item, 0)
}
type ResponseRtmStart struct {
Ok bool `json:"ok"`
Error string `json:"error"`
Url string `json:"url"`
Self ResponseSelf `json:"self"`
}
type ResponseSelf struct {
Id string `json:"id"`
}
type Message struct {
Id uint64 `json:"id"`
Type string `json:"type"`
Channel string `json:"channel"`
Text string `json:"text"`
}