Package list golang-github-nlopes-slack / f28d4fc
simplify websocket example I'm not sure if I missed something but I think the example could be simplified to this. I changed it to directly loop over the channel and I replaced the "break with label" with a "return". jorin authored 4 years ago Jorin Vogel committed 4 years ago
1 changed file(s) with 24 addition(s) and 28 deletion(s). Raw diff Collapse all Expand all
1616 rtm := api.NewRTM()
1717 go rtm.ManageConnection()
1818
19 Loop:
20 for {
21 select {
22 case msg := <-rtm.IncomingEvents:
23 fmt.Print("Event Received: ")
24 switch ev := msg.Data.(type) {
25 case *slack.HelloEvent:
26 // Ignore hello
19 for msg := range rtm.IncomingEvents {
20 fmt.Print("Event Received: ")
21 switch ev := msg.Data.(type) {
22 case *slack.HelloEvent:
23 // Ignore hello
2724
28 case *slack.ConnectedEvent:
29 fmt.Println("Infos:", ev.Info)
30 fmt.Println("Connection counter:", ev.ConnectionCount)
31 // Replace #general with your Channel ID
32 rtm.SendMessage(rtm.NewOutgoingMessage("Hello world", "#general"))
25 case *slack.ConnectedEvent:
26 fmt.Println("Infos:", ev.Info)
27 fmt.Println("Connection counter:", ev.ConnectionCount)
28 // Replace #general with your Channel ID
29 rtm.SendMessage(rtm.NewOutgoingMessage("Hello world", "#general"))
3330
34 case *slack.MessageEvent:
35 fmt.Printf("Message: %v\n", ev)
31 case *slack.MessageEvent:
32 fmt.Printf("Message: %v\n", ev)
3633
37 case *slack.PresenceChangeEvent:
38 fmt.Printf("Presence Change: %v\n", ev)
34 case *slack.PresenceChangeEvent:
35 fmt.Printf("Presence Change: %v\n", ev)
3936
40 case *slack.LatencyReport:
41 fmt.Printf("Current latency: %v\n", ev.Value)
37 case *slack.LatencyReport:
38 fmt.Printf("Current latency: %v\n", ev.Value)
4239
43 case *slack.RTMError:
44 fmt.Printf("Error: %s\n", ev.Error())
40 case *slack.RTMError:
41 fmt.Printf("Error: %s\n", ev.Error())
4542
46 case *slack.InvalidAuthEvent:
47 fmt.Printf("Invalid credentials")
48 break Loop
43 case *slack.InvalidAuthEvent:
44 fmt.Printf("Invalid credentials")
45 return
4946
50 default:
47 default:
5148
52 // Ignore other events..
53 // fmt.Printf("Unexpected: %v\n", msg.Data)
54 }
49 // Ignore other events..
50 // fmt.Printf("Unexpected: %v\n", msg.Data)
5551 }
5652 }
5753 }