-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
144 lines (114 loc) · 3.38 KB
/
main.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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"strings"
"time"
"github.com/nlopes/slack"
)
const waitBetweenChecks = time.Minute * 5
func main() {
slackApiToken := os.Getenv("SLACK_API_TOKEN")
if "" == slackApiToken {
log.Fatalln("The environment variable \"SLACK_API_TOKEN\" is not set.")
}
tagToChannel := os.Getenv("TAG_TO_CHANNEL")
if "" == tagToChannel {
log.Fatalln("The environment variable \"TAG_TO_CHANNEL\" is not set.")
}
var tagToChannelName map[string]string
err := json.Unmarshal([]byte(tagToChannel), &tagToChannelName)
if err != nil {
log.Panicln("Unable to parse JSON data provided in \"TAG_TO_CHANNEL\".")
log.Fatal(err)
}
stackSite := os.Getenv("STACK_SITE")
if stackSite == "" {
stackSite = "stackoverflow"
}
runSlackClient(slackApiToken, stackSite, tagToChannelName, os.Getenv("DEBUG") == "1")
}
func runSlackClient(slackApiToken string, stackSite string, tagToChannelName map[string]string, debug bool) {
api := slack.New(slackApiToken)
logger := log.New(os.Stdout, "slack-bot: ", log.Lshortfile|log.LstdFlags)
slack.SetLogger(logger)
api.SetDebug(debug)
rtm := api.NewRTM()
go rtm.ManageConnection()
for msg := range rtm.IncomingEvents {
switch ev := msg.Data.(type) {
case *slack.ConnectedEvent:
tagToChannelId := make(map[string]string, len(tagToChannelName))
OUTER:
for tagName, channelName := range tagToChannelName {
for _, channel := range ev.Info.Channels {
if channelName == channel.Name {
tagToChannelId[tagName] = channel.ID
continue OUTER
}
}
log.Fatalf("The channel \"%s\" doesn't exist.", channelName)
}
watchStack(rtm, tagToChannelId, stackSite)
}
}
}
func watchStack(rtm *slack.RTM, tagToChannelId map[string]string, stackSite string) {
lastCreationDate := 0
tags := make([]string, len(tagToChannelId))
i := 0
for tag := range tagToChannelId {
tags[i] = tag
i++
}
baseUrl := fmt.Sprintf("https://api.stackexchange.com/2.2/search?order=desc&sort=creation&tagged=%s&site=%s", strings.Join(tags, ";"), stackSite)
for {
var url string
if lastCreationDate > 0 {
url = fmt.Sprintf("%s&min=%d", baseUrl, lastCreationDate)
} else {
// During the first iteration, the last question is posted to be sure that the bot works
url = fmt.Sprintf("%s&pagesize=1", baseUrl)
}
resp, err := http.Get(url)
if err != nil {
log.Print(err)
time.Sleep(waitBetweenChecks)
continue
}
type Owner struct {
DisplayName string `json:"display_name"`
}
type Item struct {
Tags []string `json:"tags"`
Owner Owner `json:"owner"`
CreationDate int `json:"creation_date"`
Link string `json:"link"`
}
type Response struct {
Items []Item `json:"items"`
}
var stackResponse = new(Response)
err = json.NewDecoder(resp.Body).Decode(&stackResponse)
if err != nil {
log.Print(err)
time.Sleep(waitBetweenChecks)
continue
}
for _, item := range stackResponse.Items {
for _, tag := range item.Tags {
if channelId, ok := tagToChannelId[tag]; ok {
fmt.Printf("%v", channelId)
rtm.SendMessage(rtm.NewOutgoingMessage(fmt.Sprintf("Question by %s, tags: %s %s\n", item.Owner.DisplayName, strings.Join(item.Tags, ", "), item.Link), channelId))
}
}
if item.CreationDate > lastCreationDate {
lastCreationDate = item.CreationDate
}
}
time.Sleep(waitBetweenChecks)
}
}