Skip to content

Latest commit

 

History

History
69 lines (55 loc) · 2.19 KB

README.md

File metadata and controls

69 lines (55 loc) · 2.19 KB

GitHub Workflow Status Go Report Card GitHub tag (latest SemVer) PkgGoDev

Slackbot

A simple callback-based framework for quickly building Slack Bots in Go built on https://github.com/slack-go/slack.

It supports:

Install

go get github.com/bushelpowered/slackbot

Basic Example

package main

import (
	"github.com/bushelpowered/slackbot"
	"github.com/slack-go/slack"
	"log"
	"os"
	"os/signal"
	"syscall"
	"time"
)

// Boot a bot with a slash command that posts "Hello World!" and message listener.
func main() {
	bot := slackbot.NewBot(os.Getenv("SLACK_TOKEN"), os.Getenv("SLACK_SIGNING_SECRET"))

	// register a slash command
	bot.RegisterCommand("myslashcommand", func(bot *slackbot.Bot, command slack.SlashCommand) *slack.Msg {
		log.Println(command.Text)
		return &slack.Msg{Text: "Hello World!"} // return nil for no reply
	})

	// register a message event
	bot.RegisterMessageEvent(func(bot *slackbot.Bot, c slackbot.MessageEventContainer) {
		log.Println(c.Event.Text)
	})

	// boot the bot
	err := bot.Boot(":8000")
	if err != nil {
		log.Println(err)
		return
	}
	defer bot.Shutdown(time.Second * 10)

	// wait for exit
	quit := make(chan os.Signal)
	signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
	<-quit

	log.Print("Shutting down...")
}

See more examples in examples.