Skip to content

Commit

Permalink
Add ability to define a default inner event handler (#113)
Browse files Browse the repository at this point in the history
Users may define DefaultInnerEventHandler which will be called when an unhandled inner event is encountered.
  • Loading branch information
broxgit authored Aug 28, 2022
1 parent e3aedd3 commit 287b5db
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 20 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ func contains(list []string, element string) bool {

## Example 13

Adding handlers to when the bot is connected, encounters an error and a default for when none of the commands match
Adding handlers to when the bot is connected, encounters an error and a default for when none of the commands match, adding default inner event handler when event type isn't message or app_mention

```go
package main
Expand All @@ -674,6 +674,7 @@ import (
"fmt"

"github.com/shomali11/slacker"
"github.com/slack-go/slack/socketmode"
)

func main() {
Expand All @@ -694,6 +695,10 @@ func main() {
bot.DefaultEvent(func(event interface{}) {
fmt.Println(event)
})

bot.DefaultInnerEvent(func(ctx context.Context, evt interface{}, request *socketmode.Request) {
fmt.Printf("Handling inner event: %s", evt)
})

definition := &slacker.CommandDefinition{
Description: "help!",
Expand Down
5 changes: 5 additions & 0 deletions examples/13/example13.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"

"github.com/shomali11/slacker"
"github.com/slack-go/slack/socketmode"
)

func main() {
Expand All @@ -29,6 +30,10 @@ func main() {
fmt.Println(event)
})

bot.DefaultInnerEvent(func(ctx context.Context, evt interface{}, request *socketmode.Request) {
fmt.Printf("Handling inner event: %s", evt)
})

definition := &slacker.CommandDefinition{
Description: "help!",
Handler: func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
Expand Down
48 changes: 29 additions & 19 deletions slacker.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,24 +64,25 @@ func NewClient(botToken, appToken string, options ...ClientOption) *Slacker {

// Slacker contains the Slack API, botCommands, and handlers
type Slacker struct {
client *slack.Client
socketModeClient *socketmode.Client
botCommands []BotCommand
botContextConstructor func(ctx context.Context, api *slack.Client, client *socketmode.Client, evt *MessageEvent) BotContext
commandConstructor func(usage string, definition *CommandDefinition) BotCommand
requestConstructor func(botCtx BotContext, properties *proper.Properties) Request
responseConstructor func(botCtx BotContext) ResponseWriter
initHandler func()
errorHandler func(err string)
interactiveEventHandler func(*Slacker, *socketmode.Event, *slack.InteractionCallback)
helpDefinition *CommandDefinition
defaultMessageHandler func(botCtx BotContext, request Request, response ResponseWriter)
defaultEventHandler func(interface{})
errUnauthorized error
commandChannel chan *CommandEvent
appID string
botInteractionMode BotInteractionMode
cleanEventInput func(in string) string
client *slack.Client
socketModeClient *socketmode.Client
botCommands []BotCommand
botContextConstructor func(ctx context.Context, api *slack.Client, client *socketmode.Client, evt *MessageEvent) BotContext
commandConstructor func(usage string, definition *CommandDefinition) BotCommand
requestConstructor func(botCtx BotContext, properties *proper.Properties) Request
responseConstructor func(botCtx BotContext) ResponseWriter
initHandler func()
errorHandler func(err string)
interactiveEventHandler func(*Slacker, *socketmode.Event, *slack.InteractionCallback)
helpDefinition *CommandDefinition
defaultMessageHandler func(botCtx BotContext, request Request, response ResponseWriter)
defaultEventHandler func(interface{})
defaultInnerEventHandler func(ctx context.Context, evt interface{}, request *socketmode.Request)
errUnauthorized error
commandChannel chan *CommandEvent
appID string
botInteractionMode BotInteractionMode
cleanEventInput func(in string) string
}

// BotCommands returns Bot Commands
Expand Down Expand Up @@ -149,6 +150,11 @@ func (s *Slacker) DefaultEvent(defaultEventHandler func(interface{})) {
s.defaultEventHandler = defaultEventHandler
}

// DefaultInnerEvent handle events when an unknown inner event is seen
func (s *Slacker) DefaultInnerEvent(defaultInnerEventHandler func(ctx context.Context, evt interface{}, request *socketmode.Request)) {
s.defaultInnerEventHandler = defaultInnerEventHandler
}

// UnAuthorizedError error message
func (s *Slacker) UnAuthorizedError(errUnauthorized error) {
s.errUnauthorized = errUnauthorized
Expand Down Expand Up @@ -213,7 +219,11 @@ func (s *Slacker) Listen(ctx context.Context) error {
go s.handleMessageEvent(ctx, ev.InnerEvent.Data, nil)

default:
fmt.Printf("unsupported inner event: %+v\n", ev.InnerEvent.Type)
if s.defaultInnerEventHandler != nil {
s.defaultInnerEventHandler(ctx, ev.InnerEvent.Data, evt.Request)
} else {
fmt.Printf("unsupported inner event: %+v\n", ev.InnerEvent.Type)
}
}

s.socketModeClient.Ack(*evt.Request)
Expand Down

0 comments on commit 287b5db

Please sign in to comment.