-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
121 lines (95 loc) · 2.72 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
package main
import (
"context"
"strings"
"time"
"github.com/fortnoxab/bitbucket-slack-bot/config"
"github.com/fortnoxab/bitbucket-slack-bot/service"
"github.com/fortnoxab/bitbucket-slack-bot/webserver"
"github.com/fortnoxab/fnxlogrus"
"github.com/fortnoxab/ginprometheus"
"github.com/jonaz/gograce"
"github.com/koding/multiconfig"
"github.com/nlopes/slack"
"github.com/sirupsen/logrus"
)
func main() {
config := &config.Config{}
multiconfig.MustLoad(config)
fnxlogrus.Init(config.Log, logrus.StandardLogger())
s := slack.New(config.Token)
_, err := s.AuthTest()
if err != nil {
logrus.Error(err)
return
}
rtm := s.NewRTM()
bitbucket := service.NewBitbucket(config)
notifier := service.NewNotifier(s, rtm, bitbucket)
ws := webserver.New(notifier)
ws.Prometheus = ginprometheus.New("http")
srv, shutdown := gograce.NewServerWithTimeout(5 * time.Second)
srv.Handler = ws.Init()
srv.Addr = ":" + config.Port
ctx, cancel := context.WithCancel(context.Background())
go rtmWorker(ctx, rtm)
logrus.Error(srv.ListenAndServe())
cancel()
<-shutdown
}
func rtmWorker(ctx context.Context, rtm *slack.RTM) {
go rtm.ManageConnection()
for {
select {
case <-ctx.Done():
logrus.Info("stopping rtmWorker")
rtm.Disconnect()
return
case msg := <-rtm.IncomingEvents:
processEvent(rtm, msg)
}
}
}
var slackInfo *slack.Info
func mentionedMe(msg string) bool {
if slackInfo == nil || slackInfo.User == nil {
return false
}
uid := slackInfo.User.ID
if strings.Contains(msg, "<@"+uid+">") {
logrus.Info("return true")
return true
}
return false
}
func processEvent(rtm *slack.RTM, event slack.RTMEvent) {
switch msg := event.Data.(type) {
case *slack.HelloEvent:
// Ignore hello
case *slack.ConnectedEvent:
logrus.Info("Connected info:", msg.Info)
slackInfo = msg.Info
case *slack.MessageEvent:
logrus.Infof("Message: %s in/from: %s type: %s", msg.Text, msg.Channel, msg.SubType)
// if direct message to the bot. Reply with help stuff
helpMention := mentionedMe(msg.Text) && strings.Contains(msg.Text, "help")
helpPrivate := msg.Text == "help" && strings.HasPrefix(msg.Channel, "D")
if helpMention || helpPrivate {
rtm.SendMessage(rtm.NewOutgoingMessage("I'll send you important stuff about your PR's in bitbucket", msg.Channel))
}
case *slack.LatencyReport:
logrus.Infof("Current latency: %v", msg.Value)
case *slack.GroupJoinedEvent:
logrus.Infof("Bot joined: %s", msg.Channel.Name)
case *slack.GroupLeftEvent:
logrus.Infof("Bot left: %s", msg.Channel)
case *slack.RTMError:
logrus.Errorf("Error: %s\n", msg.Error())
case *slack.DisconnectedEvent:
logrus.Error("Disconnected", msg)
case *slack.InvalidAuthEvent:
logrus.Error("Invalid credentials")
return
default:
}
}