Skip to content

Commit

Permalink
Merge pull request #126 from arusso/master
Browse files Browse the repository at this point in the history
Add basic and interactive slash command examples
  • Loading branch information
arusso authored Mar 29, 2023
2 parents 4163d7e + fd92eea commit 92de7d6
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 20 deletions.
18 changes: 9 additions & 9 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
type BotContext interface {
Context() context.Context
Event() *MessageEvent
ApiClient() *slack.Client
APIClient() *slack.Client
SocketModeClient() *socketmode.Client
}

Expand All @@ -37,8 +37,8 @@ func (r *botContext) Event() *MessageEvent {
return r.event
}

// ApiClient returns the slack API client
func (r *botContext) ApiClient() *slack.Client {
// APIClient returns the slack API client
func (r *botContext) APIClient() *slack.Client {
return r.apiClient
}

Expand All @@ -51,7 +51,7 @@ func (r *botContext) SocketModeClient() *socketmode.Client {
type InteractiveBotContext interface {
Context() context.Context
Event() *socketmode.Event
ApiClient() *slack.Client
APIClient() *slack.Client
SocketModeClient() *socketmode.Client
}

Expand All @@ -77,8 +77,8 @@ func (r *interactiveBotContext) Event() *socketmode.Event {
return r.event
}

// ApiClient returns the slack API client
func (r *interactiveBotContext) ApiClient() *slack.Client {
// APIClient returns the slack API client
func (r *interactiveBotContext) APIClient() *slack.Client {
return r.apiClient
}

Expand All @@ -90,7 +90,7 @@ func (r *interactiveBotContext) SocketModeClient() *socketmode.Client {
// JobContext interface is for job command contexts
type JobContext interface {
Context() context.Context
ApiClient() *slack.Client
APIClient() *slack.Client
SocketModeClient() *socketmode.Client
}

Expand All @@ -110,8 +110,8 @@ func (r *jobContext) Context() context.Context {
return r.ctx
}

// ApiClient returns the slack API client
func (r *jobContext) ApiClient() *slack.Client {
// APIClient returns the slack API client
func (r *jobContext) APIClient() *slack.Client {
return r.apiClient
}

Expand Down
4 changes: 2 additions & 2 deletions examples/10/example10.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ type MyCustomResponseWriter struct {
func (r *MyCustomResponseWriter) ReportError(err error, options ...slacker.ReportErrorOption) {
defaults := slacker.NewReportErrorDefaults(options...)

apiClient := r.botCtx.ApiClient()
apiClient := r.botCtx.APIClient()
event := r.botCtx.Event()

opts := []slack.MsgOption{
Expand Down Expand Up @@ -82,7 +82,7 @@ func (r *MyCustomResponseWriter) Reply(message string, options ...slacker.ReplyO
func (r *MyCustomResponseWriter) Post(channel string, message string, options ...slacker.ReplyOption) error {
defaults := slacker.NewReplyDefaults(options...)

apiClient := r.botCtx.ApiClient()
apiClient := r.botCtx.APIClient()
ev := r.botCtx.Event()
if ev == nil {
return fmt.Errorf("unable to get message event details")
Expand Down
4 changes: 2 additions & 2 deletions examples/12/example12.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func main() {
authorizedUserIds := []string{"<User ID>"}
authorizedUserNames := []string{"shomali11"}

authorizedDefinitionById := &slacker.CommandDefinition{
authorizedDefinitionByID := &slacker.CommandDefinition{
Description: "Very secret stuff",
Examples: []string{"secret-id"},
AuthorizationFunc: func(botCtx slacker.BotContext, request slacker.Request) bool {
Expand All @@ -36,7 +36,7 @@ func main() {
},
}

bot.Command("secret-id", authorizedDefinitionById)
bot.Command("secret-id", authorizedDefinitionByID)
bot.Command("secret-name", authorizedDefinitionByName)

ctx, cancel := context.WithCancel(context.Background())
Expand Down
2 changes: 1 addition & 1 deletion examples/15/example15.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func main() {
text = "I don't understand your mood..."
}

_, _, _ = botCtx.ApiClient().PostMessage(callback.Channel.ID, slack.MsgOptionText(text, false),
_, _, _ = botCtx.APIClient().PostMessage(callback.Channel.ID, slack.MsgOptionText(text, false),
slack.MsgOptionReplaceOriginal(callback.ResponseURL))

botCtx.SocketModeClient().Ack(*botCtx.Event().Request)
Expand Down
2 changes: 1 addition & 1 deletion examples/18/example18.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func main() {
bot.Job("0 * * * * *", &slacker.JobDefinition{
Description: "A cron job that runs every minute",
Handler: func(jobCtx slacker.JobContext) {
jobCtx.ApiClient().PostMessage("#test", slack.MsgOptionText("Hello!", false))
jobCtx.APIClient().PostMessage("#test", slack.MsgOptionText("Hello!", false))
},
})

Expand Down
2 changes: 1 addition & 1 deletion examples/6/example6.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func main() {
Description: "Upload a sentence!",
Handler: func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
sentence := request.Param("sentence")
apiClient := botCtx.ApiClient()
apiClient := botCtx.APIClient()
event := botCtx.Event()

if event.ChannelID != "" {
Expand Down
67 changes: 67 additions & 0 deletions examples/interactive/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package main

import (
"context"
"fmt"
"log"
"os"

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

// Implements a basic interactive command. This assumes that a slash command
// `/mood` is defined for your app.

func slackerCmd(actionID string) func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
return func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
happyBtn := slack.NewButtonBlockElement("happy", "true", slack.NewTextBlockObject("plain_text", "Happy 🙂", true, false))
happyBtn.Style = "primary"
sadBtn := slack.NewButtonBlockElement("sad", "false", slack.NewTextBlockObject("plain_text", "Sad ☹️", true, false))
sadBtn.Style = "danger"

err := response.Reply("", slacker.WithBlocks([]slack.Block{
slack.NewSectionBlock(slack.NewTextBlockObject(slack.PlainTextType, "What is your mood today?", true, false), nil, nil),
slack.NewActionBlock(actionID, happyBtn, sadBtn),
}))

if err != nil {
fmt.Println(err)
}
}
}

func slackerInteractive(ctx slacker.InteractiveBotContext, request *socketmode.Request, callback *slack.InteractionCallback) {
text := ""
action := callback.ActionCallback.BlockActions[0]
switch action.ActionID {
case "happy":
text = "I'm happy to hear you are happy!"
case "sad":
text = "I'm sorry to hear you are sad."
default:
text = "I don't understand your mood..."
}

_, _, _ = ctx.APIClient().PostMessage(callback.Channel.ID, slack.MsgOptionText(text, false),
slack.MsgOptionReplaceOriginal(callback.ResponseURL))
}

func main() {
bot := slacker.NewClient(os.Getenv("SLACK_BOT_TOKEN"), os.Getenv("SLACK_APP_TOKEN"))
bot.Command("mood", &slacker.CommandDefinition{
BlockID: "mood",
Handler: slackerCmd("mood"),
Interactive: slackerInteractive,
HideHelp: true,
})

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

err := bot.Listen(ctx)
if err != nil {
log.Fatal(err)
}
}
36 changes: 36 additions & 0 deletions examples/slash-cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import (
"context"
"log"
"os"

"github.com/shomali11/slacker"
)

// Implements a simple slash command. Assumes you have the slash command
// `/ping` defined for your app.

func main() {
bot := slacker.NewClient(
os.Getenv("SLACK_BOT_TOKEN"),
os.Getenv("SLACK_APP_TOKEN"),
slacker.WithDebug(true),
)

bot.Command("ping", &slacker.CommandDefinition{
Handler: func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
response.Reply("pong")
},
HideHelp: true,
})

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

err := bot.Listen(ctx)
if err != nil {
log.Fatal(err)
}

}
4 changes: 2 additions & 2 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type response struct {
func (r *response) ReportError(err error, options ...ReportErrorOption) {
defaults := NewReportErrorDefaults(options...)

apiClient := r.botCtx.ApiClient()
apiClient := r.botCtx.APIClient()
event := r.botCtx.Event()

opts := []slack.MsgOption{
Expand Down Expand Up @@ -60,7 +60,7 @@ func (r *response) Reply(message string, options ...ReplyOption) error {
func (r *response) Post(channel string, message string, options ...ReplyOption) error {
defaults := NewReplyDefaults(options...)

apiClient := r.botCtx.ApiClient()
apiClient := r.botCtx.APIClient()
event := r.botCtx.Event()
if event == nil {
return fmt.Errorf("unable to get message event details")
Expand Down
4 changes: 2 additions & 2 deletions slacker.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ func (s *Slacker) BotCommands() []Command {
return s.commands
}

// ApiClient returns the internal slack.Client of Slacker struct
func (s *Slacker) ApiClient() *slack.Client {
// APIClient returns the internal slack.Client of Slacker struct
func (s *Slacker) APIClient() *slack.Client {
return s.apiClient
}

Expand Down

0 comments on commit 92de7d6

Please sign in to comment.