A simplistic event-based framework for creating slackbots in golang
lungfish makes it easy to create a chatbot that can respond to mentions using user-defined callbacks.
See example/example.go
for a working implementation of the bot
package main
import (
"github.com/laouji/lungfish"
)
func main() {
token := "<YOUR SLACK API TOKEN>"
channel := "#channel-name"
conn := lungfish.NewConnection(token)
// register the channel you want your bot to join
conn.RegisterChannel(channel)
// bot logic goes here
err := conn.Run()
if err != nil {
// handle connection error
}
}
A reaction is a keyword and callback function pair that lets you define the way your bot responds to commands.
conn.RegisterReaction("hello", func(e *lungfish.Event) {
conn.PostMessage("o hai")
})
Here are some built-in API calls that come in handy in callbacks:
Post a message to the registered channel:
conn.PostMessage("hello world")
Get information about the user from a user ID via https://api.slack.com/methods/users.info
userInfo := conn.GetUserInfo(e.UserId)
if !userInfo.Ok {
log.Println("error: " + userInfo.Error)
} else {
log.Println("user name is " + userInfo.User.Name)
}
go get github.com/laouji/lungfish