https://github.com/eko/slackbot
This is a Slack Robot written in Go.
https://github.com/eko/slackbot
bot go golang robot slack
Last synced: 10 months ago
JSON representation
This is a Slack Robot written in Go.
- Host: GitHub
- URL: https://github.com/eko/slackbot
- Owner: eko
- Created: 2017-01-10T20:10:18.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2018-03-11T16:15:10.000Z (over 8 years ago)
- Last Synced: 2025-03-19T05:07:08.338Z (over 1 year ago)
- Topics: bot, go, golang, robot, slack
- Language: Go
- Size: 14.6 KB
- Stars: 15
- Watchers: 2
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Slackbot
This is a Slack Robot written in Go.
[](https://godoc.org/github.com/eko/slackbot)
[](https://travis-ci.org/eko/slackbot)
## Robot creation
1. Go on the following uri to declare your new bot: https://{team}.slack.com/services/new/bot
2. Retrieve the given token.
## Installation
```bash
$ go get -u github.com/eko/slackbot
```
## Run the robot
```bash
$ go run app.go
Bot is ready, hit ^C to exit.
-> Command: hello dude
```
## A robot example application
This example application answers to the following command:
* @yourbotname hello : Renders "hello !",
```go
package main
import (
"github.com/eko/slackbot"
"fmt"
)
func main() {
slackbot.Token = ""
slackbot.Init()
slackbot.AddCommand("^hello (.*)", "hello", "Responds hello {string}",
func(command slackbot.Command, message slackbot.Message) {
name := command.Pattern.FindStringSubmatch(message.Text)[1]
message.Text = string(fmt.Sprintf("hello, %s!", name))
slackbot.Respond(message)
})
slackbot.Stream()
}
```
## A periodic (using robfig/cron) tasks for your bot
For this demo, we are using `github.com/robfig/cron` library to run a task every minute.
```go
package main
import (
"github.com/eko/slackbot"
"github.com/robfig/cron"
"./task"
)
const (
channel = "general"
)
var (
channelIdentifier string
)
func main() {
slackbot.Token = "your-amazing-token"
channelsResponse, _ := slackbot.ListChannels()
for i := 0; i < len(channelsResponse.Channels); i++ {
if channel == channelsResponse.Channels[i].Name {
channelIdentifier = channelsResponse.Channels[i].ID
}
}
c := cron.New()
c.AddFunc("1 * * * * *", func() {
message := slackbot.Message{
AsUser: true,
Channel: channelIdentifier,
Text: "Hello you!",
}
slackbot.PostMessage(message)
})
c.Start()
select {}
}
```