-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathslack.go
173 lines (131 loc) · 3.75 KB
/
slack.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package variant
import (
"bytes"
"context"
"fmt"
"log"
"os"
"strconv"
"strings"
"github.com/nlopes/slack"
"github.com/zclconf/go-cty/cty"
"golang.org/x/xerrors"
"github.com/mumoshu/variant2/pkg/app"
variantslack "github.com/mumoshu/variant2/pkg/slack"
)
func (r *Runner) StartSlackbot(name string) error {
// (This is written by calling isatty beforehand and this just overwrite that
r.Interactive = true
bot := variantslack.New(name, os.Getenv("SLACK_BOT_TOKEN"), os.Getenv("SLACK_VERIFICATION_TOKEN"), func(bot *variantslack.Connection, cmd string, message slack.SlashCommand) string {
var b bytes.Buffer
err := r.Run(strings.Split(cmd, " "), RunOptions{
Stdout: &b,
Stderr: &b,
SetOpts: func(opts map[string]cty.Value, pendingOptions []app.PendingInput) error {
var elems []slack.DialogElement
for _, o := range pendingOptions {
k := o.Name
var desc string
if o.Description != nil {
desc = *o.Description
}
var elem slack.DialogElement
switch o.Type {
case cty.String, cty.Bool, cty.Number:
elem = slack.DialogInput{
Label: k,
Placeholder: desc,
Type: slack.InputTypeText,
Name: k,
// Optional: true,
}
default:
elem = slack.DialogInput{
Label: k,
Placeholder: desc,
Type: slack.InputTypeTextArea,
Name: k,
// Optional: true,
}
}
elems = append(elems, elem)
}
callbackID := message.UserID + "_" + message.TriggerID
dialog := slack.Dialog{
Title: cmd,
SubmitLabel: "Run",
CallbackID: callbackID,
Elements: elems,
NotifyOnCancel: true,
}
done := make(chan error, 1)
bot.RegisterInteractionCallbackHandler(callbackID, func(callback slack.InteractionCallback) (interface{}, error) {
if callback.Type == slack.InteractionTypeDialogCancellation {
done <- nil
return nil, nil
}
if callback.Type != slack.InteractionTypeDialogSubmission {
return nil, fmt.Errorf("unexpected type of interaction callback: want %s, got %s", slack.InteractionTypeDialogSubmission, callback.Type)
}
_, transformers, err := app.MakeQuestions(pendingOptions)
if err != nil {
return nil, xerrors.Errorf("making questions: %w", err)
}
vals := make(map[string]interface{})
errs := map[string]error{}
for _, o := range pendingOptions {
k := o.Name
v := callback.Submission[k]
if v == "" {
errs[k] = fmt.Errorf("%s is required", k)
continue
}
if o.Type == cty.Bool {
b, err := strconv.ParseBool(v)
if err != nil {
errs[k] = err
continue
}
vals[k] = b
} else {
vals[k] = v
}
}
if len(errs) > 0 {
type validateError struct {
Name string `json:"name"`
Error string `json:"error"`
}
type validateErrorResponse struct {
Errors []validateError `json:"errors"`
}
var validateErrs []validateError
for k, e := range errs {
validateErrs = append(validateErrs, validateError{k, e.Error()})
}
errResponse := &validateErrorResponse{
validateErrs,
}
return errResponse, nil
}
if err := app.SetOptsFromMap(transformers, opts, vals); err != nil {
return nil, xerrors.Errorf("setting options: %w", err)
}
done <- nil
return nil, nil
})
if err := bot.Client.OpenDialogContext(context.TODO(), message.TriggerID, dialog); err != nil {
log.Print("open dialog failed: ", err)
return nil
}
return <-done
},
})
out := b.String()
if err != nil {
return err.Error()
}
return out
})
return bot.Run()
}