forked from shomali11/slacker
/
main.go
69 lines (55 loc) · 1.73 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main
import (
"context"
"log"
"os"
"github.com/shomali11/slacker/v2"
"github.com/slack-go/slack"
)
// Show cases having one handler for all interactions
func main() {
bot := slacker.NewClient(os.Getenv("SLACK_BOT_TOKEN"), os.Getenv("SLACK_APP_TOKEN"))
bot.UnsupportedInteractionHandler(func(ctx *slacker.InteractionContext) {
callback := ctx.Callback()
if callback.Type != slack.InteractionTypeBlockActions {
return
}
if len(callback.ActionCallback.BlockActions) != 1 {
return
}
action := callback.ActionCallback.BlockActions[0]
if action.BlockID != "mood-block" {
return
}
var text string
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.Response().Reply(text, slacker.WithReplace(callback.Message.Timestamp))
})
definition := &slacker.CommandDefinition{
Command: "mood",
Handler: func(ctx *slacker.CommandContext) {
happyBtn := slack.NewButtonBlockElement("happy", "true", slack.NewTextBlockObject("plain_text", "Happy 🙂", true, false))
happyBtn.Style = slack.StylePrimary
sadBtn := slack.NewButtonBlockElement("sad", "false", slack.NewTextBlockObject("plain_text", "Sad ☹️", true, false))
sadBtn.Style = slack.StyleDanger
ctx.Response().ReplyBlocks([]slack.Block{
slack.NewSectionBlock(slack.NewTextBlockObject(slack.PlainTextType, "What is your mood today?", true, false), nil, nil),
slack.NewActionBlock("mood-block", happyBtn, sadBtn),
})
},
}
bot.AddCommand(definition)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
err := bot.Listen(ctx)
if err != nil {
log.Fatal(err)
}
}