-
Notifications
You must be signed in to change notification settings - Fork 6
/
input.go
681 lines (610 loc) · 22.8 KB
/
input.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
package primitive
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"strings"
"time"
"github.com/atc0005/go-teams-notify/v2/messagecard"
"github.com/slack-go/slack"
fconstants "github.com/turbot/flowpipe/internal/constants"
"github.com/turbot/flowpipe/internal/es/db"
o "github.com/turbot/flowpipe/internal/output"
"github.com/turbot/flowpipe/internal/types"
"github.com/turbot/go-kit/helpers"
kitTypes "github.com/turbot/go-kit/types"
"github.com/turbot/pipe-fittings/constants"
"github.com/turbot/pipe-fittings/modconfig"
"github.com/turbot/pipe-fittings/perr"
"github.com/turbot/pipe-fittings/schema"
)
type Input struct {
ExecutionID string
PipelineExecutionID string
StepExecutionID string
PipelineName string
StepName string
}
func NewInputPrimitive(executionId, pipelineExecutionId, stepExecutionId, pipelineName, stepName string) *Input {
return &Input{
ExecutionID: executionId,
PipelineExecutionID: pipelineExecutionId,
StepExecutionID: stepExecutionId,
PipelineName: pipelineName,
StepName: stepName,
}
}
type InputIntegration interface {
PostMessage(ctx context.Context, inputType string, prompt string, options []InputIntegrationResponseOption) (*modconfig.Output, error)
}
type InputIntegrationBase struct {
ExecutionID string
PipelineExecutionID string
StepExecutionID string
}
func NewInputIntegrationBase(input *Input) InputIntegrationBase {
return InputIntegrationBase{
ExecutionID: input.ExecutionID,
PipelineExecutionID: input.PipelineExecutionID,
StepExecutionID: input.StepExecutionID,
}
}
type JSONPayload struct {
PipelineExecutionID string `json:"pipeline_execution_id"`
StepExecutionID string `json:"step_execution_id"`
ExecutionID string `json:"execution_id"`
}
type InputIntegrationResponseOption struct {
Label *string
Value *string
Selected *bool
Style *string
}
func (ip *Input) ValidateInput(ctx context.Context, i modconfig.Input) error {
// validate type
if i[schema.AttributeTypeType] == nil {
return perr.BadRequestWithMessage("Input must define a type")
}
inputType, inputTypeIsString := i[schema.AttributeTypeType].(string)
if !inputTypeIsString {
return perr.BadRequestWithMessage("Input type must be a string")
}
if !constants.IsValidInputType(inputType) {
return perr.BadRequestWithMessage(fmt.Sprintf("Input type '%s' is not supported", inputType))
}
// validate options
switch inputType {
case constants.InputTypeText:
// text type doesn't require options, but don't fail if we have them, just ignore
default:
// ensure has at least 1 option
options, hasOpts := i[schema.AttributeTypeOptions].([]any)
if !hasOpts || len(options) == 0 {
return perr.BadRequestWithMessage(fmt.Sprintf("Input type '%s' requires options, no options were defined", inputType))
}
// ensure all options have a value
for i, o := range options {
option := o.(map[string]any)
if helpers.IsNil(option[schema.AttributeTypeValue]) {
return perr.BadRequestWithMessage(fmt.Sprintf("option %d has no value specified", i))
}
}
}
err := ip.validateInputNotifier(i)
if err != nil {
return err // will already be perr
}
return nil
}
func (ip *Input) validateInputNotifier(i modconfig.Input) error {
notifier := i[schema.AttributeTypeNotifier].(map[string]any)
notifies := notifier[schema.AttributeTypeNotifies].([]any)
for _, n := range notifies {
notify := n.(map[string]any)
integration := notify["integration"].(map[string]any)
integrationType := integration["type"].(string)
switch integrationType {
case schema.IntegrationTypeHttp:
// no additional validations required
case schema.IntegrationTypeSlack:
// if using token, we need to specify channel, webhook_url approach has a bound channel
if integration[schema.AttributeTypeToken] != nil {
if _, stepChannel := i[schema.AttributeTypeChannel].(string); !stepChannel {
if _, notifyChannel := notify[schema.AttributeTypeChannel].(string); !notifyChannel {
if _, integrationChannel := integration[schema.AttributeTypeChannel].(string); !integrationChannel {
return perr.BadRequestWithMessage("slack notifications require a channel when using token auth, channel was not set")
}
}
}
}
case schema.IntegrationTypeEmail:
// ensure we have recipients, these can be to, cc or bcc but as optional at each layer need to ensure we have a target
var recipients []string
if to, ok := i[schema.AttributeTypeTo].([]any); ok {
for _, t := range to {
recipients = append(recipients, t.(string))
}
} else if to, ok := notify[schema.AttributeTypeTo].([]any); ok {
for _, t := range to {
recipients = append(recipients, t.(string))
}
} else if to, ok := integration[schema.AttributeTypeTo].([]any); ok {
for _, t := range to {
recipients = append(recipients, t.(string))
}
}
if cc, ok := i[schema.AttributeTypeCc].([]any); ok {
for _, c := range cc {
recipients = append(recipients, c.(string))
}
} else if cc, ok := notify[schema.AttributeTypeCc].([]any); ok {
for _, c := range cc {
recipients = append(recipients, c.(string))
}
} else if cc, ok := integration[schema.AttributeTypeCc].([]any); ok {
for _, c := range cc {
recipients = append(recipients, c.(string))
}
}
if bcc, ok := i[schema.AttributeTypeBcc].([]any); ok {
for _, b := range bcc {
recipients = append(recipients, b.(string))
}
} else if bcc, ok := notify[schema.AttributeTypeBcc].([]any); ok {
for _, b := range bcc {
recipients = append(recipients, b.(string))
}
} else if bcc, ok := integration[schema.AttributeTypeBcc].([]any); ok {
for _, b := range bcc {
recipients = append(recipients, b.(string))
}
}
if len(recipients) == 0 {
return perr.BadRequestWithMessage("email notifications require recipients; one of 'to', 'cc' or 'bcc' need to be set")
}
case schema.IntegrationTypeMsTeams:
// no additional validations required now as >4 options on button should render as select instead of error
}
}
return nil
}
func (ip *Input) execute(ctx context.Context, input modconfig.Input, mc MessageCreator) (*modconfig.Output, error) {
output := &modconfig.Output{}
var resOptions []InputIntegrationResponseOption
if options, ok := input[schema.AttributeTypeOptions].([]any); ok {
for _, o := range options {
opt := o.(map[string]any)
option := InputIntegrationResponseOption{}
if l, ok := opt[schema.AttributeTypeLabel].(string); ok {
option.Label = &l
}
if v, ok := opt[schema.AttributeTypeValue].(string); ok {
option.Value = &v
if helpers.IsNil(option.Label) {
option.Label = &v
}
}
if s, ok := opt[schema.AttributeTypeSelected].(bool); ok {
option.Selected = &s
}
if s, ok := opt[schema.AttributeTypeStyle].(string); ok {
option.Style = &s
}
resOptions = append(resOptions, option)
}
}
extNotifySent, nErrors := ip.sendNotifications(ctx, input, mc, resOptions)
switch {
case !extNotifySent && len(nErrors) == 0: // no integrations or only http integrations
return output, nil
case extNotifySent && len(nErrors) == 0: // all notifications sent
return output, nil
case extNotifySent && len(nErrors) > 0: // some external notifications sent, some failed...
// TODO: Figure out how to get this into the pipeline run output
if o.IsServerMode {
sp := types.NewServerOutputPrefixWithExecId(time.Now().UTC(), "pipeline", &ip.ExecutionID)
for _, ne := range nErrors {
o.RenderServerOutput(ctx, types.NewServerOutputError(sp, "unable to send notification", ne))
}
}
return output, nil
case !extNotifySent && len(nErrors) > 0: // all external notifications failed
var detail string
for _, ne := range nErrors {
if e, ok := ne.(perr.ErrorModel); ok {
detail += fmt.Sprintf("%s\n", e.Detail)
} else if e, ok := ne.(slack.StatusCodeError); ok {
detail += fmt.Sprintf("%s\n", e.Error())
} else {
detail += fmt.Sprintf("%s\n", ne.Error())
}
}
return nil, perr.InternalWithMessage(fmt.Sprintf("all %d notifications failed:\n%s", len(nErrors), detail))
}
return output, nil
}
func (ip *Input) sendNotifications(ctx context.Context, input modconfig.Input, mc MessageCreator, opts []InputIntegrationResponseOption) (bool, []error) {
base := NewInputIntegrationBase(ip)
externalNotificationSent := false
var notificationErrors []error
if notifier, ok := input[schema.AttributeTypeNotifier].(map[string]any); ok {
if notifies, ok := notifier[schema.AttributeTypeNotifies].([]any); ok {
for _, n := range notifies {
notify := n.(map[string]any)
integration := notify["integration"].(map[string]any)
integrationType := integration["type"].(string)
switch integrationType {
case schema.IntegrationTypeSlack:
s := NewInputIntegrationSlack(base)
// Three ways to set the channel, in order of precedence
if channel, ok := input[schema.AttributeTypeChannel].(string); ok {
s.Channel = &channel
} else if channel, ok := notify[schema.AttributeTypeChannel].(string); ok {
s.Channel = &channel
} else if channel, ok := integration[schema.AttributeTypeChannel].(string); ok {
s.Channel = &channel
}
if tkn, ok := integration[schema.AttributeTypeToken].(string); ok {
s.Token = &tkn
}
if ss, ok := integration[schema.AttributeTypeSigningSecret].(string); ok {
s.SigningSecret = &ss
}
if wu, ok := integration[schema.AttributeTypeWebhookUrl].(string); ok {
s.WebhookUrl = &wu
}
_, err := s.PostMessage(ctx, mc, opts)
if err != nil {
notificationErrors = append(notificationErrors, err)
} else {
externalNotificationSent = true
}
case schema.IntegrationTypeHttp:
// No output needs to be rendered here for HTTP step. The console output is rendered by the Event printer, it does the right thing there too.
case schema.IntegrationTypeEmail:
e := NewInputIntegrationEmail(base)
if formUrl, ok := input[fconstants.FormUrl].(string); ok {
e.FormUrl = formUrl
}
if host, ok := integration[schema.AttributeTypeSmtpHost].(string); ok {
e.Host = &host
}
if port, ok := integration[schema.AttributeTypeSmtpPort].(int64); ok {
e.Port = &port
} else if port, ok := integration[schema.AttributeTypeSmtpPort].(float64); ok {
intPort := int64(port)
e.Port = &intPort
}
if sPort, ok := integration[schema.AttributeTypeSmtpsPort].(int64); ok {
e.SecurePort = &sPort
} else if sPort, ok := integration[schema.AttributeTypeSmtpsPort].(float64); ok {
intPort := int64(sPort)
e.SecurePort = &intPort
}
if tls, ok := integration[schema.AttributeTypeSmtpTls].(string); ok {
e.Tls = &tls
}
if from, ok := integration[schema.AttributeTypeFrom].(string); ok {
e.From = from
}
if to, ok := input[schema.AttributeTypeTo].([]any); ok {
for _, t := range to {
e.To = append(e.To, t.(string))
}
} else if to, ok := notify[schema.AttributeTypeTo].([]any); ok {
for _, t := range to {
e.To = append(e.To, t.(string))
}
} else if to, ok := integration[schema.AttributeTypeTo].([]any); ok {
for _, t := range to {
e.To = append(e.To, t.(string))
}
}
if cc, ok := input[schema.AttributeTypeCc].([]any); ok {
for _, c := range cc {
e.Cc = append(e.Cc, c.(string))
}
} else if cc, ok := notify[schema.AttributeTypeCc].([]any); ok {
for _, c := range cc {
e.Cc = append(e.Cc, c.(string))
}
} else if cc, ok := integration[schema.AttributeTypeCc].([]any); ok {
for _, c := range cc {
e.Cc = append(e.Cc, c.(string))
}
}
if bcc, ok := input[schema.AttributeTypeBcc].([]any); ok {
for _, b := range bcc {
e.Bcc = append(e.Bcc, b.(string))
}
} else if bcc, ok := notify[schema.AttributeTypeBcc].([]any); ok {
for _, b := range bcc {
e.Bcc = append(e.Bcc, b.(string))
}
} else if bcc, ok := integration[schema.AttributeTypeBcc].([]any); ok {
for _, b := range bcc {
e.Bcc = append(e.Bcc, b.(string))
}
}
if sub, ok := input[schema.AttributeTypeSubject].(string); ok {
e.Subject = sub
} else if sub, ok := notify[schema.AttributeTypeSubject].(string); ok {
e.Subject = sub
} else if sub, ok := integration[schema.AttributeTypeSubject].(string); ok {
e.Subject = sub
}
if u, ok := integration[schema.AttributeTypeSmtpUsername].(string); ok {
e.User = &u
}
if p, ok := integration[schema.AttributeTypeSmtpPassword].(string); ok {
e.Pass = &p
}
_, err := e.PostMessage(ctx, mc, opts)
if err != nil {
notificationErrors = append(notificationErrors, err)
} else {
externalNotificationSent = true
}
case schema.IntegrationTypeMsTeams:
integrationName := integration["integration_name"].(string)
t := NewInputIntegrationMsTeams(base, integrationName)
if wu, ok := integration[schema.AttributeTypeWebhookUrl].(string); ok {
t.WebhookUrl = &wu
}
_, err := t.PostMessage(ctx, mc, opts)
if err != nil {
notificationErrors = append(notificationErrors, err)
} else {
externalNotificationSent = true
}
}
}
}
}
return externalNotificationSent, notificationErrors
}
func (ip *Input) Run(ctx context.Context, input modconfig.Input) (*modconfig.Output, error) {
if err := ip.ValidateInput(ctx, input); err != nil {
return nil, err
}
var inputType, prompt string
if it, ok := input[schema.AttributeTypeType].(string); ok {
inputType = it
}
if p, ok := input[schema.AttributeTypePrompt].(string); ok {
prompt = p
}
stepName := strings.Split(ip.StepName, ".")[len(strings.Split(ip.StepName, "."))-1]
return ip.execute(ctx, input, &InputStepMessageCreator{
Prompt: prompt,
InputType: inputType,
StepName: stepName,
})
}
type MessageCreator interface {
EmailMessage(*InputIntegrationEmail, []InputIntegrationResponseOption) (string, error)
SlackMessage(*InputIntegrationSlack, []InputIntegrationResponseOption) (slack.Blocks, error)
MsTeamsMessage(*InputIntegrationMsTeams, []InputIntegrationResponseOption) (*messagecard.MessageCard, error)
}
type InputStepMessageCreator struct {
Prompt string
InputType string
StepName string
}
func (icm *InputStepMessageCreator) SlackMessage(ip *InputIntegrationSlack, options []InputIntegrationResponseOption) (slack.Blocks, error) {
var blocks slack.Blocks
// payload for callback
payload := map[string]any{
"execution_id": ip.ExecutionID,
"pipeline_execution_id": ip.PipelineExecutionID,
"step_execution_id": ip.StepExecutionID,
}
jsonPayload, err := json.Marshal(payload)
if err != nil {
return blocks, err
}
// encodedPayload needs to be passed into block id of first block then we can extract it on receipt of Slacks payload
encodedPayload := base64.StdEncoding.EncodeToString(jsonPayload)
promptBlock := slack.NewTextBlockObject(slack.PlainTextType, icm.Prompt, false, false)
boldPromptBlock := slack.NewTextBlockObject(slack.MarkdownType, fmt.Sprintf("*%s*", icm.Prompt), false, false)
switch icm.InputType {
case constants.InputTypeButton:
header := slack.NewSectionBlock(boldPromptBlock, nil, nil, slack.SectionBlockOptionBlockID(encodedPayload))
var buttons []slack.BlockElement
for i, opt := range options {
button := slack.NewButtonBlockElement(fmt.Sprintf("finished_%d", i), *opt.Value, slack.NewTextBlockObject(slack.PlainTextType, *opt.Label, false, false))
if !helpers.IsNil(opt.Style) {
switch *opt.Style {
case constants.InputStyleOk:
button.Style = slack.StylePrimary
case constants.InputStyleAlert:
button.Style = slack.StyleDanger
}
}
buttons = append(buttons, slack.BlockElement(button))
}
action := slack.NewActionBlock("action_block", buttons...)
blocks.BlockSet = append(blocks.BlockSet, header, action)
case constants.InputTypeSelect:
blockOptions := make([]*slack.OptionBlockObject, len(options))
for i, opt := range options {
blockOptions[i] = slack.NewOptionBlockObject(*opt.Value, slack.NewTextBlockObject(slack.PlainTextType, *opt.Label, false, false), nil)
}
ph := slack.NewTextBlockObject(slack.PlainTextType, "Select option", false, false)
s := slack.NewOptionsSelectBlockElement(slack.OptTypeStatic, ph, "finished", blockOptions...)
input := slack.NewInputBlock(encodedPayload, promptBlock, nil, s)
blocks.BlockSet = append(blocks.BlockSet, input)
case constants.InputTypeMultiSelect:
var selectedOptions []*slack.OptionBlockObject
blockOptions := make([]*slack.OptionBlockObject, len(options))
for i, opt := range options {
blockOptions[i] = slack.NewOptionBlockObject(*opt.Value, slack.NewTextBlockObject(slack.PlainTextType, *opt.Label, false, false), nil)
if opt.Selected != nil && *opt.Selected {
selectedOptions = append(selectedOptions, blockOptions[i])
}
}
ms := slack.NewOptionsMultiSelectBlockElement(
slack.MultiOptTypeStatic,
slack.NewTextBlockObject(slack.PlainTextType, "Select options", false, false), "not_finished", blockOptions...)
if len(selectedOptions) > 0 {
ms.InitialOptions = selectedOptions
}
btn := slack.NewButtonBlockElement("finished", "submit", slack.NewTextBlockObject(slack.PlainTextType, "Submit", false, false))
input := slack.NewInputBlock(encodedPayload, promptBlock, nil, ms)
action := slack.NewActionBlock("action_block", btn)
blocks.BlockSet = append(blocks.BlockSet, input, action)
case constants.InputTypeText:
textInput := slack.NewPlainTextInputBlockElement(nil, "finished")
input := slack.NewInputBlock(encodedPayload, promptBlock, nil, textInput)
input.DispatchAction = true // required for being able to send event
blocks.BlockSet = append(blocks.BlockSet, input)
default:
return blocks, perr.InternalWithMessage(fmt.Sprintf("Type %s not yet implemented for Slack Integration", icm.InputType))
}
return blocks, nil
}
func (icm *InputStepMessageCreator) EmailMessage(iim *InputIntegrationEmail, options []InputIntegrationResponseOption) (string, error) {
header := make(map[string]string)
header["From"] = iim.From
header["To"] = strings.Join(iim.To, ", ")
if len(iim.Cc) > 0 {
header["Cc"] = strings.Join(iim.Cc, ", ")
}
header["Content-Type"] = "text/html; charset=\"UTF-8\";"
header["MIME-version"] = "1.0;"
subject := iim.Subject
if subject == "" {
subject = icm.Prompt
if len(subject) > 50 {
subject = subject[:50] + "..."
}
}
header["Subject"] = subject
var message string
for key, value := range header {
message += fmt.Sprintf("%s: %s\r\n", key, value)
}
var data any
templateFileName := "input-form-link.html"
switch icm.InputType {
case "button":
stepName := icm.StepName
templateFileName = "input-form-buttons.html"
data = struct {
Prompt string
Options []InputIntegrationResponseOption
StepName string
FormUrl string
}{
Prompt: icm.Prompt,
Options: options,
StepName: stepName,
FormUrl: iim.FormUrl,
}
default:
data = struct {
FormUrl string
Prompt string
}{
FormUrl: iim.FormUrl,
Prompt: icm.Prompt,
}
}
templateMessage, err := parseEmailInputTemplate(templateFileName, data)
if err != nil {
return "", err
}
message += templateMessage
return message, nil
}
func (icm *InputStepMessageCreator) MsTeamsMessage(ip *InputIntegrationMsTeams, options []InputIntegrationResponseOption) (*messagecard.MessageCard, error) {
msgCard := messagecard.NewMessageCard()
// get response url from cached integration
i, err := db.GetIntegration(ip.IntegrationName)
if err != nil {
return nil, err
}
responseUrl := kitTypes.SafeString(i.GetIntegrationImpl().Url)
if responseUrl == "" {
return nil, perr.InternalWithMessage("response url not found for integration " + ip.IntegrationName)
}
msgCard.Title = icm.Prompt
msgCard.Summary = icm.Prompt
pa, err := messagecard.NewPotentialAction(messagecard.PotentialActionActionCardType, ip.StepExecutionID)
if err != nil {
return nil, err
}
body, err := ip.buildReturnPayload("{{options.value}}", icm.Prompt)
if err != nil {
return nil, perr.InternalWithMessage("error building return body payload for " + ip.IntegrationName)
}
switch {
case icm.InputType == constants.InputTypeButton && len(options) <= 4:
for _, option := range options {
pa.Actions = append(pa.Actions, messagecard.PotentialActionActionCardAction{
Type: messagecard.PotentialActionHTTPPostType,
Name: *option.Label,
PotentialActionHTTPPOST: messagecard.PotentialActionHTTPPOST{
Body: strings.Replace(body, "{{options.value}}", *option.Value, 1),
Target: responseUrl,
Headers: []messagecard.PotentialActionHTTPPOSTHeader{},
},
})
}
case icm.InputType == constants.InputTypeText:
pa.Inputs = append(pa.Inputs, messagecard.PotentialActionActionCardInput{
ID: "options",
Type: messagecard.PotentialActionActionCardInputTextInputType,
IsRequired: true,
PotentialActionActionCardInputTextInput: messagecard.PotentialActionActionCardInputTextInput{
IsMultiline: false,
}})
pa.Actions = append(pa.Actions, messagecard.PotentialActionActionCardAction{
Type: messagecard.PotentialActionHTTPPostType,
Name: "Submit",
PotentialActionHTTPPOST: messagecard.PotentialActionHTTPPOST{
Body: body,
Target: responseUrl,
Headers: []messagecard.PotentialActionHTTPPOSTHeader{},
},
})
case icm.InputType == constants.InputTypeSelect,
icm.InputType == constants.InputTypeMultiSelect,
icm.InputType == constants.InputTypeButton && len(options) > 4:
isMulti := icm.InputType == constants.InputTypeMultiSelect
var choices []struct {
Display string `json:"display,omitempty" yaml:"display,omitempty"`
Value string `json:"value,omitempty" yaml:"value,omitempty"`
}
for _, option := range options {
choices = append(choices, struct {
Display string `json:"display,omitempty" yaml:"display,omitempty"`
Value string `json:"value,omitempty" yaml:"value,omitempty"`
}{
Display: *option.Label,
Value: *option.Value,
})
}
pa.Inputs = append(pa.Inputs, messagecard.PotentialActionActionCardInput{
ID: "options",
Type: messagecard.PotentialActionActionCardInputMultichoiceInputType,
IsRequired: true,
PotentialActionActionCardInputMultichoiceInput: messagecard.PotentialActionActionCardInputMultichoiceInput{
IsMultiSelect: isMulti,
Choices: choices,
Style: "expanded",
}})
pa.Actions = append(pa.Actions, messagecard.PotentialActionActionCardAction{
Type: messagecard.PotentialActionHTTPPostType,
Name: "Submit",
PotentialActionHTTPPOST: messagecard.PotentialActionHTTPPOST{
Body: body,
Target: responseUrl,
Headers: []messagecard.PotentialActionHTTPPOSTHeader{},
},
})
}
msgCard.PotentialActions = append(msgCard.PotentialActions, pa)
return msgCard, nil
}