https://github.com/willis7/slack
A Slack client for the Go programming language
https://github.com/willis7/slack
api golang slack
Last synced: about 1 month ago
JSON representation
A Slack client for the Go programming language
- Host: GitHub
- URL: https://github.com/willis7/slack
- Owner: willis7
- Created: 2017-04-26T23:15:30.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2017-05-03T20:07:33.000Z (about 9 years ago)
- Last Synced: 2025-04-20T18:38:58.104Z (about 1 year ago)
- Topics: api, golang, slack
- Language: Go
- Homepage:
- Size: 4.88 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Slack
This project started as an embedded package in my Golossary project. After a positive code review from Brian Ketelsen I decided to open source this code as a standalone package.
## Design
This package was heavily influenced by the `net/http` package which allows users to define a `Handler` for a given pattern. The same principle is used here for events:
``` go
mux := slack.NewEventMux()
mux.Handle("message", slack.HandlerFunc(RTMMessage))
```
In the example above we use the user defined `RTMMessage` function, which satisfies the `Handler` interface with the `message` event. Similarly, we could apply our `RTMHello` function to the `hello` event. This might look like:
``` go
...
mux.Handle("hello", slack.HandlerFunc(RTMHello))
```
## Client Lifecycle
### Connecting to Slack
To create a new client we first need to run:
`client := slack.NewClient(token, mux)`
You're required to provide an `EventMux` here because it would be a useless client if it didn't know how to handle events. No default Event mux is provided.
To open a connection with Slack you will need to call:
``` go
client.Connect()
defer client.Close()
```
Remeber to close the connection!
Next we need to tell the client to start dispatching messages with:
`go client.Dispatch()`
At this point its really up to the user to shutdown the connection gracefully. Here's an example of how I do it:
```go
sigterm := make(chan os.Signal)
signal.Notify(sigterm, os.Interrupt)
for {
select {
case <-sigterm:
log.Println("terminate signal recvd")
err := client.Shutdown()
if err != nil {
log.Println("write close:", err)
return
}
select {
case <-time.After(time.Second):
}
client.Close()
return
}
}
```