Built on top of the Slack API github.com/nlopes/slack with the idea to simplify the Real-Time Messaging feature to easily create Slack Bots, assign commands to them and extract parameters.
- Easy definitions of commands and their input
- Available bot initialization, errors and default handlers
- Simple parsing of String, Integer, Float and Boolean parameters
- Contains support for
context.Context
- Built-in
help
command - Bot responds to mentions and direct messages
- Handlers run concurrently via goroutines
- Full access to the Slack API github.com/nlopes/slack
Using govendor
github.com/kardianos/govendor:
govendor fetch github.com/shomali11/slacker
commander
github.com/shomali11/commanderslack
github.com/nlopes/slack
Defining a command using slacker
package main
import (
"github.com/shomali11/slacker"
"log"
)
func main() {
bot := slacker.NewClient("<YOUR SLACK BOT TOKEN>")
bot.Command("ping", "Ping!", func(request *slacker.Request, response slacker.ResponseWriter) {
response.Reply("pong")
})
err := bot.Listen()
if err != nil {
log.Fatal(err)
}
}
Adding handlers to when the bot is connected, encounters an error and a default for when none of the commands match
package main
import (
"github.com/shomali11/slacker"
"log"
)
func main() {
bot := slacker.NewClient("<YOUR SLACK BOT TOKEN>")
bot.Init(func() {
log.Println("Connected!")
})
bot.Err(func(err string) {
log.Println(err)
})
bot.Default(func(request *slacker.Request, response slacker.ResponseWriter) {
response.Reply("Say what?")
})
err := bot.Listen()
if err != nil {
log.Fatal(err)
}
}
Defining a command with a parameter
package main
import (
"github.com/shomali11/slacker"
"log"
)
func main() {
bot := slacker.NewClient("<YOUR SLACK BOT TOKEN>")
bot.Command("echo <word>", "Echo a word!", func(request *slacker.Request, response slacker.ResponseWriter) {
word := request.Param("word")
response.Reply(word)
})
err := bot.Listen()
if err != nil {
log.Fatal(err)
}
}
Defining a command with two parameters. Parsing one as a string and the other as an integer. (The second parameter is the default value in case no parameter was passed or could not parse the value)
package main
import (
"github.com/shomali11/slacker"
"log"
)
func main() {
bot := slacker.NewClient("<YOUR SLACK BOT TOKEN>")
bot.Command("repeat <word> <number>", "Repeat a word a number of times!", func(request *slacker.Request, response slacker.ResponseWriter) {
word := request.StringParam("word", "Hello!")
number := request.IntegerParam("number", 1)
for i := 0; i < number; i++ {
response.Reply(word)
}
})
err := bot.Listen()
if err != nil {
log.Fatal(err)
}
}
Send an error message to the Slack channel
package main
import (
"errors"
"github.com/shomali11/slacker"
"log"
)
func main() {
bot := slacker.NewClient("<YOUR SLACK BOT TOKEN>")
bot.Command("test", "Tests something", func(request *slacker.Request, response slacker.ResponseWriter) {
response.ReportError(errors.New("Oops!"))
})
err := bot.Listen()
if err != nil {
log.Fatal(err)
}
}
Send a "Typing" indicator
package main
import (
"github.com/shomali11/slacker"
"log"
"time"
)
func main() {
bot := slacker.NewClient("<YOUR SLACK BOT TOKEN>")
bot.Command("time", "Server time!", func(request *slacker.Request, response slacker.ResponseWriter) {
response.Typing()
time.Sleep(time.Second)
response.Reply(time.Now().Format(time.RFC1123))
})
err := bot.Listen()
if err != nil {
log.Fatal(err)
}
}
Showcasing the ability to access the github.com/nlopes/slack API. In this example, we upload a file using the Slack API.
package main
import (
"github.com/nlopes/slack"
"github.com/shomali11/slacker"
"log"
)
func main() {
bot := slacker.NewClient("<YOUR SLACK BOT TOKEN>")
bot.Command("upload <word>", "Upload a word!", func(request *slacker.Request, response slacker.ResponseWriter) {
word := request.Param("word")
channel := request.Event.Channel
bot.Client.UploadFile(slack.FileUploadParameters{Content: word, Channels: []string{channel}})
})
err := bot.Listen()
if err != nil {
log.Fatal(err)
}
}
Showcasing the ability to leverage context.Context
to add a timeout
package main
import (
"context"
"errors"
"github.com/shomali11/slacker"
"log"
"time"
)
func main() {
bot := slacker.NewClient("<YOUR SLACK BOT TOKEN>")
bot.Command("process", "Process!", func(request *slacker.Request, response slacker.ResponseWriter) {
timedContext, cancel := context.WithTimeout(request.Context, time.Second)
defer cancel()
select {
case <-timedContext.Done():
response.ReportError(errors.New("Timed out"))
case <-time.After(time.Minute):
response.Reply("Processing done!")
}
})
err := bot.Listen()
if err != nil {
log.Fatal(err)
}
}