go-sarah: github.com/oklahomer/go-sarah/examples/simple/plugins/guess Index | Files

package guess

import "github.com/oklahomer/go-sarah/examples/simple/plugins/guess"

Package guess provides example code to setup stateful command.

This command returns sarah.UserContext as part of sarah.CommandResponse until user inputs correct number. As long as sarah.UserContext is returned, the next input from the same user is fed to the function defined in sarah.UserContext. When user guesses right number or input .abort, the context is removed and user is free to input next desired command.

This example uses in-memory storage to store user context. See https://github.com/oklahomer/go-sarah-rediscontext to use external storage.

Index

Package Files

props.go

Variables

var SlackProps = sarah.NewCommandPropsBuilder().
    BotType(slack.SLACK).
    Identifier("guess").
    Instruction("Input .guess to start a game.").
    MatchFunc(func(input sarah.Input) bool {
        return strings.HasPrefix(strings.TrimSpace(input.Message()), ".guess")
    }).
    Func(func(ctx context.Context, input sarah.Input) (*sarah.CommandResponse, error) {

        rand.Seed(time.Now().UnixNano())
        answer := rand.Intn(10)

        return slack.NewResponse(
            input,
            "Input number.",
            slack.RespWithNext(func(c context.Context, i sarah.Input) (*sarah.CommandResponse, error) {
                return guessFunc(c, i, answer)
            }),
            slack.RespAsThreadReply(true),
        )
    }).
    MustBuild()

SlackProps is a pre-built guess command properties for Slack.

Package guess imports 7 packages (graph). Updated 2020-09-27. Refresh now. Tools for package owners.