forked from slack-go/slack
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This makes it possible to select on the event type and act accordingly.
- Loading branch information
Showing
13 changed files
with
372 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/nlopes/slack" | ||
) | ||
|
||
func main() { | ||
chSender := make(chan slack.OutgoingMessage) | ||
chReceiver := make(chan slack.SlackEvent) | ||
|
||
api := slack.New("YOUR TOKEN HERE") | ||
api.SetDebug(true) | ||
wsAPI, err := api.StartRTM("", "http://example.com") | ||
if err != nil { | ||
fmt.Errorf("%s\n", err) | ||
} | ||
go wsAPI.HandleIncomingEvents(chReceiver) | ||
go wsAPI.Keepalive(20 * time.Second) | ||
go func(wsAPI *slack.SlackWS, chSender chan slack.OutgoingMessage) { | ||
for { | ||
select { | ||
case msg := <-chSender: | ||
wsAPI.SendMessage(&msg) | ||
} | ||
} | ||
}(wsAPI, chSender) | ||
for { | ||
select { | ||
case msg := <-chReceiver: | ||
fmt.Print("Event Received: ") | ||
switch msg.Data.(type) { | ||
case slack.HelloEvent: | ||
// Ignore hello | ||
case *slack.MessageEvent: | ||
a := msg.Data.(*slack.MessageEvent) | ||
fmt.Printf("%v\n", a) | ||
case *slack.PresenceChangeEvent: | ||
a := msg.Data.(*slack.PresenceChangeEvent) | ||
fmt.Printf("%v\n", a) | ||
case slack.LatencyReport: | ||
a := msg.Data.(slack.LatencyReport) | ||
fmt.Printf("Current latency: %v\n", a.Value) | ||
default: | ||
fmt.Printf("Unknown: %v\n", msg.Data) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.