slackbot

package module
v0.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 13, 2023 License: MIT Imports: 9 Imported by: 1

README

slackbot

Test GoDoc

Basic slackbot implementation

Authors

  • Christophe Lambin

License

This project is licensed under the MIT License - see the LICENSE.md file for details.

Documentation

Overview

Package slackbot provides a basic slackbot implementation. Using this package typically involves creating a bot as follows:

bot := slackbot.New("some-token", slackbot.WithCommands(...)
go bot.Run(context.Background())

Once running, the bot will listen for any commands specified and execute them. Slackbot itself implements two commands: "version" (which responds with the bot's name; see WithName option) and "help" (which shows all supported commands).

Applications can send messages as follows:

bot.Send(channel, []slack.Attachment{{Text: "Hello world"}})
Example
package main

import (
	"context"
	"github.com/clambin/go-common/slackbot"
	"github.com/slack-go/slack"
)

func main() {
	b := slackbot.New("my-slack-token", slackbot.WithCommands(map[string]slackbot.CommandFunc{
		"hello": func(_ context.Context, _ ...string) []slack.Attachment {
			return []slack.Attachment{{
				Color: "good",
				Text:  "General Kenobi!",
			}}
		},
	}))

	b.Run(context.Background())
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CommandFunc

type CommandFunc func(ctx context.Context, args ...string) []slack.Attachment

CommandFunc signature for command callback functions

type Option added in v0.4.0

type Option func(*SlackBot)

func WithCommands added in v0.4.0

func WithCommands(commands map[string]CommandFunc) Option

func WithLogger added in v0.4.0

func WithLogger(logger *slog.Logger) Option

func WithName added in v0.4.0

func WithName(name string) Option

type SlackBot

type SlackBot struct {
	// contains filtered or unexported fields
}

SlackBot connects to Slack through Slack's Bot integration.

func New

func New(slackToken string, options ...Option) *SlackBot

New creates a new slackbot

func (*SlackBot) Register

func (b *SlackBot) Register(name string, command CommandFunc)

Register adds a new command

func (*SlackBot) Run

func (b *SlackBot) Run(ctx context.Context) error

Run the slackbot

func (*SlackBot) Send

func (b *SlackBot) Send(channel string, attachments []slack.Attachment) error

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL