-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
154 lines (139 loc) · 4.31 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package main
import (
"context"
"fmt"
"strings"
"github.com/hashicorp/go-plugin"
"github.com/kubeshop/botkube/pkg/api"
"github.com/kubeshop/botkube/pkg/api/executor"
)
const (
description = "Msg sends an example interactive messages."
pluginName = "msg"
)
// version is set via ldflags by GoReleaser.
var version = "dev"
// MsgExecutor implements the Botkube executor plugin interface.
type MsgExecutor struct{}
// Metadata returns details about the Msg plugin.
func (MsgExecutor) Metadata(context.Context) (api.MetadataOutput, error) {
return api.MetadataOutput{
Version: version,
Description: description,
}, nil
}
// Execute returns a given command as a response.
//
//nolint:gocritic //hugeParam: in is heavy (80 bytes); consider passing it by pointer
func (MsgExecutor) Execute(_ context.Context, in executor.ExecuteInput) (executor.ExecuteOutput, error) {
if !in.Context.IsInteractivitySupported {
return executor.ExecuteOutput{
Message: api.NewCodeBlockMessage("Interactivity for this platform is not supported", true),
}, nil
}
if strings.TrimSpace(in.Command) == pluginName {
return initialMessages(), nil
}
msg := fmt.Sprintf("Plain command: %s", in.Command)
return executor.ExecuteOutput{
Message: api.NewCodeBlockMessage(msg, true),
}, nil
}
func initialMessages() executor.ExecuteOutput {
btnBuilder := api.NewMessageButtonBuilder()
cmdPrefix := func(cmd string) string {
return fmt.Sprintf("%s %s %s", api.MessageBotNamePlaceholder, pluginName, cmd)
}
return executor.ExecuteOutput{
Message: api.Message{
BaseBody: api.Body{
Plaintext: "Showcases interactive message capabilities",
},
Sections: []api.Section{
{
Buttons: []api.Button{
btnBuilder.ForCommandWithDescCmd("Run act1", fmt.Sprintf("%s buttons act1", pluginName)),
btnBuilder.ForCommandWithDescCmd("Run act2", fmt.Sprintf("%s buttons act2", pluginName), api.ButtonStylePrimary),
btnBuilder.ForCommandWithDescCmd("Run act3", fmt.Sprintf("%s buttons act3", pluginName), api.ButtonStyleDanger),
},
},
{
Buttons: []api.Button{
btnBuilder.ForCommandWithoutDesc("Run act4", fmt.Sprintf("%s buttons act4", pluginName)),
btnBuilder.ForCommandWithoutDesc("Run act5", fmt.Sprintf("%s buttons act5", pluginName), api.ButtonStylePrimary),
btnBuilder.ForCommandWithoutDesc("Run act6", fmt.Sprintf("%s buttons act6", pluginName), api.ButtonStyleDanger),
},
},
{
Selects: api.Selects{
ID: "select-id",
Items: []api.Select{
{
Name: "first",
Command: cmdPrefix("selects first"),
OptionGroups: []api.OptionGroup{
{
Name: cmdPrefix("selects first"),
Options: []api.OptionItem{
{Name: "BAR", Value: "BAR"},
{Name: "BAZ", Value: "BAZ"},
{Name: "XYZ", Value: "XYZ"},
},
},
},
},
{
Name: "second",
Command: cmdPrefix("selects second"),
OptionGroups: []api.OptionGroup{
{
Name: cmdPrefix("selects second"),
Options: []api.OptionItem{
{Name: "BAR", Value: "BAR"},
{Name: "BAZ", Value: "BAZ"},
{Name: "XYZ", Value: "XYZ"},
},
},
{
Name: cmdPrefix("selects second/section2"),
Options: []api.OptionItem{
{Name: "123", Value: "123"},
{Name: "456", Value: "456"},
{Name: "789", Value: "789"},
},
},
},
// MUST be defined also under OptionGroups.Options slice.
InitialOption: &api.OptionItem{
Name: "789", Value: "789",
},
},
},
},
},
},
PlaintextInputs: []api.LabelInput{
{
Command: cmdPrefix("input-text"),
DispatchedAction: api.DispatchInputActionOnEnter,
Placeholder: "String pattern to filter by",
Text: "Filter output",
},
},
OnlyVisibleForYou: false,
ReplaceOriginal: false,
},
}
}
func (MsgExecutor) Help(context.Context) (api.Message, error) {
msg := description
msg += fmt.Sprintf("\nJust type `%s %s`", api.MessageBotNamePlaceholder, pluginName)
return api.NewPlaintextMessage(msg, false), nil
}
func main() {
executor.Serve(map[string]plugin.Plugin{
pluginName: &executor.Plugin{
Executor: &MsgExecutor{},
},
})
}