diff --git a/README.md b/README.md index 3171caa..b8da77e 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 ", definition) + bot.Command("say ", &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() @@ -215,7 +222,7 @@ func main() { }, } - bot.Command("repeat ", definition) + bot.Command("repeat {word} {number}", definition) ctx, cancel := context.WithCancel(context.Background()) defer cancel() @@ -294,15 +301,15 @@ 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) } @@ -310,7 +317,7 @@ func main() { }, } - bot.Command("upload ", definition) + bot.Command("upload ", definition) ctx, cancel := context.WithCancel(context.Background()) defer cancel() @@ -405,7 +412,7 @@ func main() { }, } - bot.Command("echo ", definition) + bot.Command("echo {word}", definition) ctx, cancel := context.WithCancel(context.Background()) defer cancel() @@ -450,7 +457,7 @@ func main() { }, } - bot.Command("echo ", definition) + bot.Command("echo {word}", definition) ctx, cancel := context.WithCancel(context.Background()) defer cancel() @@ -746,7 +753,7 @@ func main() { }, }) - bot.Command("echo ", &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) { diff --git a/examples/14/example14.go b/examples/14/example14.go index 3d584b2..68d410f 100644 --- a/examples/14/example14.go +++ b/examples/14/example14.go @@ -32,7 +32,7 @@ func main() { }, }) - bot.Command("echo ", &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) { diff --git a/examples/16/example16.go b/examples/16/example16.go index 4d72065..12d1b33 100644 --- a/examples/16/example16.go +++ b/examples/16/example16.go @@ -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) + } } diff --git a/examples/3/example3.go b/examples/3/example3.go index bc7ab56..607b3a4 100644 --- a/examples/3/example3.go +++ b/examples/3/example3.go @@ -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 ", definition) + bot.Command("say ", &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() diff --git a/examples/4/example4.go b/examples/4/example4.go index 69a6e48..9104ab2 100644 --- a/examples/4/example4.go +++ b/examples/4/example4.go @@ -23,7 +23,7 @@ func main() { }, } - bot.Command("repeat ", definition) + bot.Command("repeat {word} {number}", definition) ctx, cancel := context.WithCancel(context.Background()) defer cancel() diff --git a/examples/6/example6.go b/examples/6/example6.go index b28b00d..bf3a28c 100644 --- a/examples/6/example6.go +++ b/examples/6/example6.go @@ -14,15 +14,15 @@ 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) } @@ -30,7 +30,7 @@ func main() { }, } - bot.Command("upload ", definition) + bot.Command("upload ", definition) ctx, cancel := context.WithCancel(context.Background()) defer cancel() diff --git a/examples/8/example8.go b/examples/8/example8.go index bccfcd8..1282b85 100644 --- a/examples/8/example8.go +++ b/examples/8/example8.go @@ -29,7 +29,7 @@ func main() { }, } - bot.Command("echo ", definition) + bot.Command("echo {word}", definition) ctx, cancel := context.WithCancel(context.Background()) defer cancel() diff --git a/examples/9/example9.go b/examples/9/example9.go index ce0196b..0795b3f 100644 --- a/examples/9/example9.go +++ b/examples/9/example9.go @@ -28,7 +28,7 @@ func main() { }, } - bot.Command("echo ", definition) + bot.Command("echo {word}", definition) ctx, cancel := context.WithCancel(context.Background()) defer cancel() diff --git a/go.mod b/go.mod index faa952e..f22b44f 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index cfcbbac..fbab869 100644 --- a/go.sum +++ b/go.sum @@ -3,20 +3,16 @@ 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= @@ -24,4 +20,5 @@ github.com/stretchr/testify v1.2.1/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf 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=