-
Notifications
You must be signed in to change notification settings - Fork 1
/
listen.go
147 lines (124 loc) · 3.66 KB
/
listen.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
package bot
import (
"context"
"fmt"
"os"
"strings"
"github.com/matthieudolci/hatcher/common"
"github.com/matthieudolci/hatcher/help"
"github.com/matthieudolci/hatcher/setup"
"github.com/matthieudolci/hatcher/standup"
log "github.com/Sirupsen/logrus"
"github.com/slack-go/slack"
)
// New returns a new instance of the Slack struct, primary for our slackbot
func New() (*common.Slack, error) {
token := os.Getenv("SLACK_TOKEN")
if len(token) == 0 {
return nil, fmt.Errorf("Could not discover API token")
}
return &common.Slack{Client: slack.New(token), Token: token, Name: "hatcher"}, nil
}
// Run is the primary service to generate and kick off the slackbot listener
// This portion receives all incoming Real Time Messages notices from the workspace
// as registered by the API token
func Run(ctx context.Context, s *common.Slack) error {
authTest, err := s.Client.AuthTest()
if err != nil {
return fmt.Errorf("Did not authenticate: %+v", err)
}
s.User = authTest.User
s.UserID = authTest.UserID
log.WithFields(log.Fields{
"username": s.User,
"userid": s.UserID,
}).Info("Bot is now registered")
go run(ctx, s)
return nil
}
func run(ctx context.Context, s *common.Slack) {
// slack.SetLogger()
// s.Client.SetDebug(true)
rtm := s.Client.NewRTM()
go rtm.ManageConnection()
log.Info("Now listening for incoming messages...")
for msg := range rtm.IncomingEvents {
switch x := msg.Data.(type) {
case *slack.MessageEvent:
if x.SubType == "message_changed" {
err := standup.CheckIfYesterdayMessageEdited(s, x)
if err != nil {
log.WithFields(log.Fields{
"userid": x.User,
}).WithError(err).Error("Checking if yesterday standup was edited")
}
err = standup.CheckIfTodayMessageEdited(s, x)
if err != nil {
log.WithFields(log.Fields{
"userid": x.User,
}).WithError(err).Error("Checking if yesterday standup was edited")
}
err = standup.CheckIfBlockerMessageEdited(s, x)
if err != nil {
log.WithFields(log.Fields{
"userid": x.User,
}).WithError(err).Error("Checking if yesterday standup was edited")
}
}
}
switch ev := msg.Data.(type) {
case *slack.MessageEvent:
if len(ev.User) == 0 {
continue
}
// check if we have a DM, or standard channel post
direct := strings.HasPrefix(ev.Msg.Channel, "D")
inchannel := strings.Contains(ev.Msg.Text, "@"+s.UserID)
if !direct && !inchannel {
// msg not for us!
continue
}
user, err := s.Client.GetUserInfo(ev.User)
if err != nil {
log.WithFields(log.Fields{
"userid": ev.User,
}).Printf("Could not grab user information.")
continue
}
log.WithFields(log.Fields{
"username": user.Profile.RealName,
"userid": ev.User,
}).Info("Received message")
err = setup.AskSetup(s, ev)
if err != nil {
log.WithFields(log.Fields{
"username": user.Profile.RealName,
"userid": ev.User,
}).WithError(err).Error("Posting setup reply to user")
}
err = setup.AskRemove(s, ev)
if err != nil {
log.WithFields(log.Fields{
"username": user.Profile.RealName,
"userid": ev.User,
}).WithError(err).Error("Posting remove reply to user")
}
err = standup.AskStandupYesterday(s, ev)
if err != nil {
log.WithFields(log.Fields{
"username": user.Profile.RealName,
"userid": ev.User,
}).WithError(err).Error("Posting yesterday standup note question to user")
}
err = help.AskHelp(s, ev)
if err != nil {
log.WithFields(log.Fields{
"username": user.Profile.RealName,
"userid": ev.User,
}).WithError(err).Error("Posting help message")
}
case *slack.RTMError:
log.Errorf("%s", ev.Error())
}
}
}