Giter Club home page Giter Club logo

go-slackbot's Introduction

Description

This is a simple hubot like slack bot. It will listen to slack MessageEvents prefixed with a given BOT_NAME and parse the message to run your custom scripts.

Interactive shell for testing message events

asciicast

Setup

  1. Create a slack app
  2. Set app to socket mode
  3. Get/Generate your App Token and Bot Token
  4. Enable Events and Subscribe to message.channels events
package main

import (
	"fmt"
	"strings"

	"github.com/chrispruitt/go-slackbot/lib/bot"
	c "github.com/chrispruitt/go-slackbot/lib/config"
)

func main() {
	// Simple script
	bot.RegisterScript(bot.Script{
		Name:        "lulz",
		Matcher:     "lulz",
		Description: "lulz",
		Function: func(context *bot.ScriptContext) {
			bot.PostMessage(context.SlackEvent.Channel, "lol")
		},
	})

	// Script with parameters
	bot.RegisterScript(bot.Script{
		Name:        "echo",
		Matcher:     "echo <message>",
		Description: "Echo a message",
		Function: func(context *bot.ScriptContext) {
			message := context.Arguments["message"]
			bot.PostMessage(context.SlackEvent.Channel, fmt.Sprintf("You said, \"%s\"", message))
		},
	})

	// Script with some custom arguments syntax
	// "<app>+" allows spaces in argument to match on or more words in the single arg
	bot.RegisterScript(bot.Script{
		Name:        "ship",
		Matcher:     `ship <app>+ to <env>`,
		Description: "Usage: 'ship [email protected] to dev' or 'ship [email protected] [email protected] to dev",
		Function: func(context *bot.ScriptContext) {

			env := context.Arguments["env"]
			apps := strings.Split(context.Arguments["app"], " ")

			for _, a := range apps {
				app := strings.Split(a, "@")
				bot.PostMessage(context.SlackEvent.Channel, fmt.Sprintf("Shipping App: %s Version: %s to %s", app[0], app[1], env))
			}
		},
	})

	// Register a scheduled task
	bot.RegisterPeriodicScript(bot.PeriodicScript{
		Name:     "hello-world",
		CronSpec: "*/1 * * * *",
		Function: func() {
			fmt.Print("hello world every minute")
		},
	})

	botConfig := &bot.Config{
		SlackAppToken:    c.SlackAppToken,
		SlackBotToken:    c.SlackBotToken,
		BotName:          c.BotName,

		 // Set to true to enable shell mode
		ShellMode:        c.ShellMode,
		
		// If set, when using shell mode, scripts will bot.PostMessage will post message in given slack channel
		ShellModeChannel: c.ShellModeChannel,
	}

	bot.Start(botConfig)
}

In Slack

Add your slack bot to a channel.

Then, execute a script via slack by typing the given BOT_NAME followed by a command that will match a script matcher.

${BOT_NAME} help <filter> is a built in script that will list all your commands using the Description and Matcher fields.

Build in commands

${BOT_NAME} help ?<filter> - filter arg is optional - list all available commands ${BOT_NAME} status - check status of bot

Roadmap

  • Provide terraform module for quick setup in fargate
  • Update readme with a "how to" to set up slack bot or publish one
  • Add native script authorization via roles
  • Give go-slackbot a brain via dynamodb or s3 json file
  • Catch fatal script errors and prevent exit
  • Dockerize

go-slackbot's People

Contributors

chrispruitt avatar

Watchers

James Cloos avatar  avatar

Forkers

justmiles

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.