Skip to content

Commit

Permalink
V2 Foundation: Groups, Middlewares, Simplified Interfaces, ... (#134)
Browse files Browse the repository at this point in the history
feat: Slacker V2

- Simplified Interfaces
- Added support for Groups, Middleware
- Enhanced support for Interaction Callbacks
- Enhanced support for Cron Jobs
- Added support for more message actions
- Improved examples and documentation
  • Loading branch information
raed-shomali authored Jun 24, 2023
1 parent c38812c commit bc3ade5
Show file tree
Hide file tree
Showing 59 changed files with 2,070 additions and 2,432 deletions.
1,035 changes: 13 additions & 1,022 deletions README.md

Large diffs are not rendered by default.

25 changes: 0 additions & 25 deletions analytics.go

This file was deleted.

File renamed without changes.
16 changes: 8 additions & 8 deletions bots.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package slacker

// BotInteractionMode instruct the bot on how to handle incoming events that
// BotMode instruct the bot on how to handle incoming events that
// originated from a bot.
type BotInteractionMode int
type BotMode int

const (
// BotInteractionModeIgnoreAll instructs our bot to ignore any activity coming
// BotModeIgnoreAll instructs our bot to ignore any activity coming
// from other bots, including our self.
BotInteractionModeIgnoreAll BotInteractionMode = iota
BotModeIgnoreAll BotMode = iota

// BotInteractionModeIgnoreApp will ignore any events that originate from a
// BotModeIgnoreApp will ignore any events that originate from a
// bot that is associated with the same App (ie. share the same App ID) as
// this bot. OAuth scope `user:read` is required for this mode.
BotInteractionModeIgnoreApp
BotModeIgnoreApp

// BotInteractionModeIgnoreNone will not ignore any bots, including our self.
// BotModeIgnoreNone will not ignore any bots, including our self.
// This can lead to bots "talking" to each other so care must be taken when
// selecting this option.
BotInteractionModeIgnoreNone
BotModeIgnoreNone
)
45 changes: 8 additions & 37 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,55 +3,42 @@ package slacker
import (
"github.com/shomali11/commander"
"github.com/shomali11/proper"
"github.com/slack-go/slack"
"github.com/slack-go/slack/socketmode"
)

// CommandDefinition structure contains definition of the bot command
type CommandDefinition struct {
Description string
Examples []string
BlockID string
AuthorizationFunc func(BotContext, Request) bool
Handler func(BotContext, Request, ResponseWriter)
Interactive func(InteractiveBotContext, *socketmode.Request, *slack.InteractionCallback)
Command string
Description string
Examples []string
Middlewares []CommandMiddlewareHandler
Handler CommandHandler

// HideHelp will hide this command definition from appearing in the `help` results.
HideHelp bool
}

// NewCommand creates a new bot command object
func NewCommand(usage string, definition *CommandDefinition) Command {
// newCommand creates a new bot command object
func newCommand(definition *CommandDefinition) Command {
return &command{
usage: usage,
definition: definition,
cmd: commander.NewCommand(usage),
cmd: commander.NewCommand(definition.Command),
}
}

// Command interface
type Command interface {
Usage() string
Definition() *CommandDefinition

Match(string) (*proper.Properties, bool)
Tokenize() []*commander.Token
Execute(BotContext, Request, ResponseWriter)
Interactive(InteractiveBotContext, *socketmode.Request, *slack.InteractionCallback)
}

// command structure contains the bot's command, description and handler
type command struct {
usage string
definition *CommandDefinition
cmd *commander.Command
}

// Usage returns the command usage
func (c *command) Usage() string {
return c.usage
}

// Definition returns the command definition
func (c *command) Definition() *CommandDefinition {
return c.definition
Expand All @@ -66,19 +53,3 @@ func (c *command) Match(text string) (*proper.Properties, bool) {
func (c *command) Tokenize() []*commander.Token {
return c.cmd.Tokenize()
}

// Execute executes the handler logic
func (c *command) Execute(botCtx BotContext, request Request, response ResponseWriter) {
if c.definition == nil || c.definition.Handler == nil {
return
}
c.definition.Handler(botCtx, request, response)
}

// Interactive executes the interactive logic
func (c *command) Interactive(botContext InteractiveBotContext, request *socketmode.Request, callback *slack.InteractionCallback) {
if c.definition == nil || c.definition.Interactive == nil {
return
}
c.definition.Interactive(botContext, request, callback)
}
50 changes: 50 additions & 0 deletions command_group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package slacker

import (
"fmt"
"strings"
)

// newGroup creates a new CommandGroup with a prefix
func newGroup(prefix string) *CommandGroup {
return &CommandGroup{prefix: prefix}
}

// CommandGroup groups commands with a common prefix and middlewares
type CommandGroup struct {
prefix string
middlewares []CommandMiddlewareHandler
commands []Command
}

// AddMiddleware define a new middleware and append it to the list of group middlewares
func (g *CommandGroup) AddMiddleware(middleware CommandMiddlewareHandler) {
g.middlewares = append(g.middlewares, middleware)
}

// AddCommand define a new command and append it to the list of group bot commands
func (g *CommandGroup) AddCommand(definition *CommandDefinition) {
definition.Command = strings.TrimSpace(fmt.Sprintf("%s %s", g.prefix, definition.Command))
g.commands = append(g.commands, newCommand(definition))
}

// PrependCommand define a new command and prepend it to the list of group bot commands
func (g *CommandGroup) PrependCommand(definition *CommandDefinition) {
definition.Command = strings.TrimSpace(fmt.Sprintf("%s %s", g.prefix, definition.Command))
g.commands = append([]Command{newCommand(definition)}, g.commands...)
}

// GetPrefix returns the group's prefix
func (g *CommandGroup) GetPrefix() string {
return g.prefix
}

// GetCommands returns Commands
func (g *CommandGroup) GetCommands() []Command {
return g.commands
}

// GetMiddlewares returns Middlewares
func (g *CommandGroup) GetMiddlewares() []CommandMiddlewareHandler {
return g.middlewares
}
Loading

0 comments on commit bc3ade5

Please sign in to comment.