File size: 2,901 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package slack

import (
	"context"
	"fmt"

	"github.com/target/goalert/config"
	"github.com/target/goalert/notification/nfydest"
	"github.com/target/goalert/validation"
)

var (
	_ nfydest.Provider      = (*ChannelSender)(nil)
	_ nfydest.FieldSearcher = (*ChannelSender)(nil)
)

func (s *ChannelSender) ID() string { return DestTypeSlackChannel }
func (s *ChannelSender) TypeInfo(ctx context.Context) (*nfydest.TypeInfo, error) {
	cfg := config.FromContext(ctx)

	// Get bot name dynamically, fallback to generic name if error
	botName := "GoAlert"
	if name, err := s.BotName(ctx); err == nil && name != "" {
		botName = name
	}

	return &nfydest.TypeInfo{
		Type:                       DestTypeSlackChannel,
		Name:                       "Slack Channel",
		Enabled:                    cfg.Slack.Enable,
		SupportsAlertNotifications: true,
		SupportsStatusUpdates:      true,
		SupportsOnCallNotify:       true,
		StatusUpdatesRequired:      true,
		SupportsSignals:            true,
		RequiredFields: []nfydest.FieldConfig{{
			FieldID:        FieldSlackChannelID,
			Label:          "Slack Channel",
			InputType:      "text",
			SupportsSearch: true,
			Hint:           fmt.Sprintf("If your channel doesn't appear in search results, invite %s (bot) to the channel and allow a minute for it to appear.", botName),
		}},
		DynamicParams: []nfydest.DynamicParamConfig{{
			ParamID: "message",
			Label:   "Message",
			Hint:    "The text of the message to send.",
		}},
	}, nil
}

func (s *ChannelSender) ValidateField(ctx context.Context, fieldID, value string) error {
	switch fieldID {
	case FieldSlackChannelID:
		return s.ValidateChannel(ctx, value)
	}

	return validation.NewGenericError("unknown field ID")
}

func (s *ChannelSender) DisplayInfo(ctx context.Context, args map[string]string) (*nfydest.DisplayInfo, error) {
	if args == nil {
		args = make(map[string]string)
	}

	ch, err := s.Channel(ctx, args[FieldSlackChannelID])
	if err != nil {
		return nil, err
	}

	team, err := s.Team(ctx, ch.TeamID)
	if err != nil {
		return nil, err
	}

	if team.IconURL == "" {
		team.IconURL = FallbackIconURL
	}
	return &nfydest.DisplayInfo{
		IconURL:     team.IconURL,
		IconAltText: team.Name,
		LinkURL:     team.ChannelLink(ch.ID),
		Text:        ch.Name,
	}, nil
}

func (s *ChannelSender) SearchField(ctx context.Context, fieldID string, options nfydest.SearchOptions) (*nfydest.SearchResult, error) {
	switch fieldID {
	case FieldSlackChannelID:
		return nfydest.SearchByListFunc(ctx, options, s.ListChannels)
	}

	return nil, validation.NewGenericError("unsupported field ID")
}

func (s *ChannelSender) FieldLabel(ctx context.Context, fieldID, value string) (string, error) {
	switch fieldID {
	case FieldSlackChannelID:
		ch, err := s.Channel(ctx, value)
		if err != nil {
			return "", err
		}

		return ch.Name, nil
	}

	return "", validation.NewGenericError("unsupported field ID")
}