-
Notifications
You must be signed in to change notification settings - Fork 0
/
slack-topic-changes.go
83 lines (70 loc) · 2.14 KB
/
slack-topic-changes.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
package main
import (
"flag"
"fmt"
"github.com/nlopes/slack"
"net/http"
"os"
"regexp"
"strconv"
"strings"
"time"
)
var count int
var debug bool
var msgRegex = regexp.MustCompile("set the channel's topic: (.+)$")
func init() {
flag.IntVar(&count, "count", 100, "Number of results to return from Slack")
flag.BoolVar(&debug, "debug", false, "Use debugging in Slack requests?")
}
func handleMsg(msg slack.SearchMessage, api *slack.Client, nameCache map[string]string) {
if _, ok := nameCache[msg.User]; !ok {
if debug {
fmt.Printf("No entry for name %s in user cache, asking Slack\n", msg.User)
}
user, err := api.GetUserInfo(msg.User)
if err != nil {
fmt.Printf("Error looking up user %s: %s\n", msg.User, err)
return
}
if debug {
fmt.Printf("Adding name cache mapping for %s to user name %s\n", msg.User, user.Name)
}
nameCache[msg.User] = user.Name
}
userName := nameCache[msg.User]
timeStrings := strings.Split(msg.Timestamp, ".")
sec, err := strconv.ParseInt(timeStrings[0], 10, 64)
if err != nil {
fmt.Printf("Error converting timestamp %s (specifically %s) to int64: %s\n", msg.Timestamp, timeStrings[0], err)
return
}
// For sake of convenience we outright ignore nsec here
ts := time.Unix(sec, 0)
regexMatches := msgRegex.FindSubmatch([]byte(msg.Text))
if regexMatches != nil {
fmt.Printf("%s: %s set topic to '%s'\n", ts, userName, regexMatches[1])
} else {
fmt.Printf("%s: %s emptied topic\n", ts, userName)
}
}
func main() {
flag.Parse()
nameCache := make(map[string]string)
// Originally I was creating a new HTTP client 'cuz I thought we'd have to do a manual OAuth process
// in order to get a token... now I just do it for fun
httpclient := &http.Client{}
api := slack.New(os.Getenv("SLACK_TOKEN"), slack.OptionHTTPClient(httpclient))
slack.OptionDebug(debug)(api)
params := slack.NewSearchParameters()
params.Count = count
queryString := fmt.Sprintf("\"set the channel's topic\" in:%s", os.Getenv("SLACK_CHANNEL"))
msgs, err := api.SearchMessages(queryString, params)
if err != nil {
fmt.Printf("%s\n", err)
return
}
for _, msg := range msgs.Matches {
handleMsg(msg, api, nameCache)
}
}