-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
52 lines (45 loc) · 1.03 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
package main
import (
"flag"
"fmt"
"github.com/nlopes/slack"
)
var token = flag.String("token", "", "bot token generated by slack")
var debug = flag.Bool("debug", false, "set to show debugging messages")
func main() {
flag.Parse()
if *token == "" {
fmt.Println("Please set -token flag")
flag.PrintDefaults()
return
}
api := slack.New(*token)
api.SetDebug(*debug)
rtm := api.NewRTM()
go rtm.ManageConnection()
Loop:
for {
select {
case msg := <-rtm.IncomingEvents:
switch ev := msg.Data.(type) {
case *slack.MessageEvent:
if len(ev.Text) > 0 && playgroundUrl.MatchString(ev.Text) {
addrs := playgroundUrl.FindAllString(ev.Text, -1)
for _, addr := range addrs {
data, err := playground(addr)
if err != nil {
fmt.Println("Playground error:", err)
continue
}
rtm.SendMessage(rtm.NewOutgoingMessage(data, ev.Channel))
}
}
case *slack.InvalidAuthEvent:
fmt.Printf("Invalid credentials")
break Loop
default:
// Ignore other events..
}
}
}
}