slackbot

package module
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2020 License: MIT Imports: 17 Imported by: 0

README

slackbot-go

Test Status Coverage Status MIT License

Description

Chatbot for slack of golang.

Setup

Token, ID

You need to set up Token and ID in one of the following ways.

  • Setup() function
  • Environment Variables
Setup() function

Can be set by calling the Setup function.

import (
    slackbot "github.com/peto-tn/slackbot-go"
)

const (
    SLACK_ACCESS_TOKEN       string = "xoxb-0123456789-012345678901-ABCDEFGHIJKLMOPQRSTUVWXY"
    SLACK_BOT_USER_ID        string = "U01234567"
    SLACK_VERIFICATION_TOKEN string = "abcdefghijklmopqrstuvwxy"
)

func init() {
    slackbot.Setup(SLACK_ACCESS_TOKEN, SLACK_BOT_USER_ID, SLACK_VERIFICATION_TOKEN)
}
Environment Variables

Automatically used if the following environment variables are set.

  • SLACK_ACCESS_TOKEN
  • SLACK_BOT_USER_ID
  • SLACK_VERIFICATION_TOKEN
Entry point

Create an entry point for slackbot-go.

AWS Lambda
import (
    slackbot "github.com/peto-tn/slackbot-go"
)

func main() {
    slackbot.AWSLambdaStart()
}
GCP Cloud Functions
import (
    "net/http"

    slackbot "github.com/peto-tn/slackbot-go"
)

func OnCall(w http.ResponseWriter, r *http.Request) {
    slackbot.OnCall(w, r)
}
Listen
import (
    slackbot "github.com/peto-tn/slackbot-go"
)

func main() {
    slackbot.ListenAndServe("/", ":8000", nil)
}

Add ChatOps Command

This is a sample command to repeat a message.
You can optionally specify the number of repetitions and the font.

package example

import (
	"strconv"

	slackbot "github.com/peto-tn/slackbot-go"
)

func init() {
	slackbot.AddCommand(&slackbot.Command{
		Name:        "repeat",
		HelpMessage: "Repeat input message.",
		Execute:     repeat,
		Option:      RepeatOption{},
	})
}

type RepeatOption struct {
	Message string
	Count   string `default:"1"`
	Font    string `default:"thin" choice:"thin,bold,italic"`
}

func repeat(e slackbot.Event, opt interface{}) {
	option := opt.(RepeatOption)

	message := ""

	// Add messages as many as Count
	count, err := strconv.Atoi(option.Count)
	if err != nil {
		slackbot.ReplyMessage(e, "error: Invalid format for 'Count' option.")
		return
	}
	for i := 0; i < count; i++ {
		message += option.Message
	}

	// Font
	switch option.Font {
	case "bold":
		message = "*" + message + "*"
	case "italic":
		message = "_" + message + "_"
	}

	slackbot.ReplyMessage(e, message)
}

Author

peto-tn

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AWSLambdaHandler

AWSLambdaHandler is handler when a slack event is received via aws lambda.

func AWSLambdaStart

func AWSLambdaStart()

AWSLambdaStart is start execution of aws lambda.

func AddCommand

func AddCommand(c *Command)

AddCommand for slackbot.

func ClearCommand

func ClearCommand()

ClearCommand all for slackbot.

func Help

func Help(c *Command, desc bool) string

Help message command.

func ListenAndServe

func ListenAndServe(pattern, addr string, handler http.Handler)

ListenAndServe is start the http server. use net/http

func OnCall

func OnCall(w http.ResponseWriter, r *http.Request)

OnCall is receive slack events handler.

func ParseOption

func ParseOption(c *Command, options []string) (interface{}, error)

ParseOption of the command.

func PostEphemeral

func PostEphemeral(e Event, message string)

PostEphemeral message to Slack.

func PostMessage

func PostMessage(e Event, message string)

PostMessage to Slack.

func ReplyMessage

func ReplyMessage(e Event, message string)

ReplyMessage to Slack.

func SetDefaultHelpDescription

func SetDefaultHelpDescription(description bool)

SetDefaultHelpDescription display.

func SetMessageHandler

func SetMessageHandler(handler MessageHandler)

SetMessageHandler for slackbot

func Setup

func Setup(argBotUserID, argVerificationToken, argAccessToken string)

Setup slackbot.

func SetupCommand

func SetupCommand(custom []*Command)

SetupCommand for slackbot. help and ping command are added automativally.

Types

type Command

type Command struct {
	Name        string
	HelpMessage string
	Execute     func(e Event, opt interface{})
	Option      interface{}
}

Command for Slack ChatOps.

type Event

type Event map[string]interface{}

Event of slack.

func (Event) Channel

func (e Event) Channel() string

Channel of Event.

func (Event) ModifyText added in v0.0.3

func (e Event) ModifyText()

ModifyText correctly.

func (Event) String

func (e Event) String(key string) string

String data in Event.

func (Event) Text

func (e Event) Text() string

Text of Event.

func (Event) ThreadTimestamp

func (e Event) ThreadTimestamp() string

ThreadTimestamp of Event. If not thread, get event timestamp.

func (Event) Type

func (e Event) Type() string

Type of Event.

func (Event) User

func (e Event) User() string

User of eVent.

type HelpCommandOption

type HelpCommandOption interface {
	IsDescription() string
}

HelpCommandOption interface.

type HelpCommandOptionDesc

type HelpCommandOptionDesc struct {
	Description string `default:"true" choice:"false,true"`
}

HelpCommandOptionDesc is Help Command Option with default description enabled.

func (HelpCommandOptionDesc) IsDescription

func (o HelpCommandOptionDesc) IsDescription() string

IsDescription check.

type HelpCommandOptionSimple

type HelpCommandOptionSimple struct {
	Description string `default:"false" choice:"false,true"`
}

HelpCommandOptionSimple is Help Command Option with default description enabled.

func (HelpCommandOptionSimple) IsDescription

func (o HelpCommandOptionSimple) IsDescription() string

IsDescription check.

type MessageHandler

type MessageHandler interface {
	OnMessage(e Event, texts []string)
	OnMentionMessage(e Event, texts []string)
}

MessageHandler for Slack

type Payload

type Payload map[string]interface{}

Payload for Slack Event

func DecodeJSON

func DecodeJSON(r io.Reader) (Payload, error)

DecodeJSON data.

func (Payload) Event

func (p Payload) Event() Event

Event in Payload.

func (Payload) String

func (p Payload) String(key string) string

String data in Payload.

func (Payload) Token

func (p Payload) Token() string

Token in Payload.

func (Payload) Type

func (p Payload) Type() string

Type of Payload.

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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