-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #126 from arusso/master
Add basic and interactive slash command examples
- Loading branch information
Showing
10 changed files
with
123 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters