forked from go-chat-bot/bot
/
slack.go
140 lines (123 loc) · 2.87 KB
/
slack.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
// Package slack implements Slack handlers for github.com/go-chat-bot/bot
package slack
import (
"fmt"
"github.com/go-chat-bot/bot"
"github.com/nlopes/slack"
)
var (
rtm *slack.RTM
api *slack.Client
teaminfo *slack.TeamInfo
channelList = map[string]slack.Channel{}
params = slack.PostMessageParameters{AsUser: true}
botUserID = ""
)
func responseHandler(target string, message string, sender *bot.User) {
api.PostMessage(target, message, params)
}
// Extracts user information from slack API
func extractUser(event *slack.MessageEvent) *bot.User {
var isBot bool
var userID string
if len(event.User) == 0 {
userID = event.BotID
isBot = true
} else {
userID = event.User
isBot = false
}
slackUser, err := api.GetUserInfo(userID)
if err != nil {
fmt.Printf("Error retrieving slack user: %s\n", err)
return &bot.User{
ID: userID,
IsBot: isBot}
}
return &bot.User{
ID: userID,
Nick: slackUser.Name,
RealName: slackUser.Profile.RealName,
IsBot: isBot}
}
func extractText(event *slack.MessageEvent) string {
text := ""
if len(event.Text) != 0 {
text = event.Text
} else {
attachments := event.Attachments
if len(attachments) > 0 {
text = attachments[0].Fallback
}
}
return text
}
func readBotInfo(api *slack.Client) {
info, err := api.AuthTest()
if err != nil {
fmt.Printf("Error calling AuthTest: %s\n", err)
return
}
botUserID = info.UserID
}
func readChannelData(api *slack.Client) {
channels, err := api.GetChannels(true)
if err != nil {
fmt.Printf("Error getting Channels: %s\n", err)
return
}
for _, channel := range channels {
channelList[channel.ID] = channel
}
}
func ownMessage(UserID string) bool {
return botUserID == UserID
}
// Run connects to slack RTM API using the provided token
func Run(token string) {
api = slack.New(token)
rtm = api.NewRTM()
teaminfo, _ = api.GetTeamInfo()
b := bot.New(&bot.Handlers{
Response: responseHandler,
})
b.Disable([]string{"url"})
go rtm.ManageConnection()
Loop:
for {
select {
case msg := <-rtm.IncomingEvents:
switch ev := msg.Data.(type) {
case *slack.HelloEvent:
readBotInfo(api)
readChannelData(api)
case *slack.ChannelCreatedEvent:
readChannelData(api)
case *slack.ChannelRenameEvent:
readChannelData(api)
case *slack.MessageEvent:
if !ev.Hidden && !ownMessage(ev.User) {
C := channelList[ev.Channel]
var channel = ev.Channel
if C.IsChannel {
channel = fmt.Sprintf("#%s", C.Name)
}
b.MessageReceived(
&bot.ChannelData{
Protocol: "slack",
Server: teaminfo.Domain,
Channel: channel,
IsPrivate: !C.IsChannel,
},
extractText(ev),
extractUser(ev))
}
case *slack.RTMError:
fmt.Printf("Error: %s\n", ev.Error())
case *slack.InvalidAuthEvent:
fmt.Printf("Invalid credentials")
break Loop
}
}
}
}