Skip to content

Commit

Permalink
Introduce new {word} vs <sentence> parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
raed-shomali committed Jul 16, 2022
1 parent 65dc447 commit b07a424
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 52 deletions.
31 changes: 19 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func main() {

## Example 3

Defining a command with a parameter
Defining a command with a parameter. Parameters surrounded with {} will be satisfied with a word. Parameters surrounded with <> are "greedy" and will take as much input as fed.

```go
package main
Expand All @@ -163,16 +163,23 @@ import (
func main() {
bot := slacker.NewClient(os.Getenv("SLACK_BOT_TOKEN"), os.Getenv("SLACK_APP_TOKEN"))

definition := &slacker.CommandDefinition{
bot.Command("echo {word}", &slacker.CommandDefinition{
Description: "Echo a word!",
Example: "echo hello",
Handler: func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
word := request.Param("word")
response.Reply(word)
},
}
})

bot.Command("echo <word>", definition)
bot.Command("say <sentence>", &slacker.CommandDefinition{
Description: "Say a sentence!",
Example: "say hello there everyone!",
Handler: func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
sentence := request.Param("sentence")
response.Reply(sentence)
},
})

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand Down Expand Up @@ -215,7 +222,7 @@ func main() {
},
}

bot.Command("repeat <word> <number>", definition)
bot.Command("repeat {word} {number}", definition)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand Down Expand Up @@ -294,23 +301,23 @@ func main() {
bot := slacker.NewClient(os.Getenv("SLACK_BOT_TOKEN"), os.Getenv("SLACK_APP_TOKEN"))

definition := &slacker.CommandDefinition{
Description: "Upload a word!",
Description: "Upload a sentence!",
Handler: func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
word := request.Param("word")
sentence := request.Param("sentence")
client := botCtx.Client()
ev := botCtx.Event()

if ev.Channel != "" {
client.PostMessage(ev.Channel, slack.MsgOptionText("Uploading file ...", false))
_, err := client.UploadFile(slack.FileUploadParameters{Content: word, Channels: []string{ev.Channel}})
_, err := client.UploadFile(slack.FileUploadParameters{Content: sentence, Channels: []string{ev.Channel}})
if err != nil {
fmt.Printf("Error encountered when uploading file: %+v\n", err)
}
}
},
}

bot.Command("upload <word>", definition)
bot.Command("upload <sentence>", definition)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand Down Expand Up @@ -405,7 +412,7 @@ func main() {
},
}

bot.Command("echo <word>", definition)
bot.Command("echo {word}", definition)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand Down Expand Up @@ -450,7 +457,7 @@ func main() {
},
}

bot.Command("echo <word>", definition)
bot.Command("echo {word}", definition)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand Down Expand Up @@ -746,7 +753,7 @@ func main() {
},
})

bot.Command("echo <word>", &slacker.CommandDefinition{
bot.Command("echo {word}", &slacker.CommandDefinition{
Description: "Echo a word!",
Example: "echo hello",
Handler: func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
Expand Down
2 changes: 1 addition & 1 deletion examples/14/example14.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func main() {
},
})

bot.Command("echo <word>", &slacker.CommandDefinition{
bot.Command("echo {word}", &slacker.CommandDefinition{
Description: "Echo a word!",
Example: "echo hello",
Handler: func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
Expand Down
40 changes: 20 additions & 20 deletions examples/16/example16.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
package main

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

"github.com/shomali11/slacker"
"github.com/shomali11/slacker"
)

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

bot.Command("hello", &slacker.CommandDefinition{
Handler: func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
response.Reply("hai!")
},
})
bot.Command("hello", &slacker.CommandDefinition{
Handler: func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
response.Reply("hai!")
},
})

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

err := bot.Listen(ctx)
if err != nil {
log.Fatal(err)
}
err := bot.Listen(ctx)
if err != nil {
log.Fatal(err)
}
}
13 changes: 10 additions & 3 deletions examples/3/example3.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,23 @@ import (
func main() {
bot := slacker.NewClient(os.Getenv("SLACK_BOT_TOKEN"), os.Getenv("SLACK_APP_TOKEN"))

definition := &slacker.CommandDefinition{
bot.Command("echo {word}", &slacker.CommandDefinition{
Description: "Echo a word!",
Example: "echo hello",
Handler: func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
word := request.Param("word")
response.Reply(word)
},
}
})

bot.Command("echo <word>", definition)
bot.Command("say <sentence>", &slacker.CommandDefinition{
Description: "Say a sentence!",
Example: "say hello there everyone!",
Handler: func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
sentence := request.Param("sentence")
response.Reply(sentence)
},
})

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand Down
2 changes: 1 addition & 1 deletion examples/4/example4.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func main() {
},
}

bot.Command("repeat <word> <number>", definition)
bot.Command("repeat {word} {number}", definition)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand Down
8 changes: 4 additions & 4 deletions examples/6/example6.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,23 @@ func main() {
bot := slacker.NewClient(os.Getenv("SLACK_BOT_TOKEN"), os.Getenv("SLACK_APP_TOKEN"))

definition := &slacker.CommandDefinition{
Description: "Upload a word!",
Description: "Upload a sentence!",
Handler: func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
word := request.Param("word")
sentence := request.Param("sentence")
client := botCtx.Client()
ev := botCtx.Event()

if ev.Channel != "" {
client.PostMessage(ev.Channel, slack.MsgOptionText("Uploading file ...", false))
_, err := client.UploadFile(slack.FileUploadParameters{Content: word, Channels: []string{ev.Channel}})
_, err := client.UploadFile(slack.FileUploadParameters{Content: sentence, Channels: []string{ev.Channel}})
if err != nil {
fmt.Printf("Error encountered when uploading file: %+v\n", err)
}
}
},
}

bot.Command("upload <word>", definition)
bot.Command("upload <sentence>", definition)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand Down
2 changes: 1 addition & 1 deletion examples/8/example8.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func main() {
},
}

bot.Command("echo <word>", definition)
bot.Command("echo {word}", definition)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand Down
2 changes: 1 addition & 1 deletion examples/9/example9.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func main() {
},
}

bot.Command("echo <word>", definition)
bot.Command("echo {word}", definition)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ module github.com/shomali11/slacker
go 1.14

require (
github.com/pkg/errors v0.8.1 // indirect
github.com/shomali11/commander v0.0.0-20191122162317-51bc574c29ba
github.com/shomali11/commander v0.0.0-20220716022157-b5248c76541a
github.com/shomali11/proper v0.0.0-20180607004733-233a9a872c30
github.com/slack-go/slack v0.11.0
github.com/stretchr/testify v1.3.0 // indirect
Expand Down
11 changes: 4 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,22 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-test/deep v1.0.4 h1:u2CU3YKy9I2pmu9pX0eq50wCgjfGIt539SqR7FbHiho=
github.com/go-test/deep v1.0.4/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/shomali11/commander v0.0.0-20191122162317-51bc574c29ba h1:EDb+FfzJD5OTWxKE5LQaM6oiScfzNVmzjgCfWziLDkA=
github.com/shomali11/commander v0.0.0-20191122162317-51bc574c29ba/go.mod h1:bYyJw/Aj9fK+qoFmRbPJeWsDgq7WGO8f/Qof95qPug4=
github.com/shomali11/commander v0.0.0-20220716022157-b5248c76541a h1:NCmAZOmyqKwf+0KzhY6I6CPndU3qkLRp47RwTyLdMW8=
github.com/shomali11/commander v0.0.0-20220716022157-b5248c76541a/go.mod h1:bYyJw/Aj9fK+qoFmRbPJeWsDgq7WGO8f/Qof95qPug4=
github.com/shomali11/proper v0.0.0-20180607004733-233a9a872c30 h1:56awf1OXG6Jc2Pk1saojpCzpzkoBvlqecCyNLY+wwkc=
github.com/shomali11/proper v0.0.0-20180607004733-233a9a872c30/go.mod h1:O723XwIZBX3FR45rBic/Eyp/DKo/YtchYFURzpUWY2c=
github.com/slack-go/slack v0.9.1 h1:pekQBs0RmrdAgoqzcMCzUCWSyIkhzUU3F83ExAdZrKo=
github.com/slack-go/slack v0.9.1/go.mod h1:wWL//kk0ho+FcQXcBTmEafUI5dz4qz5f4mMk8oIkioQ=
github.com/slack-go/slack v0.11.0 h1:sBBjQz8LY++6eeWhGJNZpRm5jvLRNnWBFZ/cAq58a6k=
github.com/slack-go/slack v0.11.0/go.mod h1:hlGi5oXA+Gt+yWTPP0plCdRKmjsDxecdHxYQdlMQKOw=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.1/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

0 comments on commit b07a424

Please sign in to comment.