-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslackinviter.go
36 lines (31 loc) · 1.24 KB
/
slackinviter.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
package main
import (
"fmt"
"github.com/nlopes/slack"
"os"
)
func main() {
apiKey := os.Getenv("SLACK_API_KEY")
slackChannel := os.Getenv("SLACK_CHANNEL")
api := slack.New(apiKey)
api.SetDebug(false)
rtm := api.NewRTM()
go rtm.ManageConnection()
mainEventLoop:
for {
select {
case msg := <-rtm.IncomingEvents:
switch slackEvent := msg.Data.(type) {
case *slack.MessageEvent:
if slackEvent.SubType == "channel_leave" && slackEvent.Channel == slackChannel {
api.InviteUserToChannel(slackEvent.Channel, slackEvent.User)
userData, _ := api.GetUserInfo(slackEvent.User)
fmt.Printf("User %s attempted to leave!\n", userData.Name)
}
case *slack.InvalidAuthEvent:
fmt.Printf("Invalid Authentication: %+v\n", slackEvent)
break mainEventLoop // Exit main event loop, The API key is invalid
}
}
}
}