-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathattachment.go
114 lines (96 loc) · 2.57 KB
/
attachment.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
package vslack
// AttachmentInterface is the interface for an attachment
//
//go:generate mockery --inpackage --name=AttachmentInterface
type AttachmentInterface interface {
SetText(t string) Attachment
SetColor(c string) Attachment
SetTitle(t string) Attachment
SetTitleLink(l string) Attachment
SetFields(f ...Field) Attachment
SetMarkdown(m bool) Attachment
SetMarkdownIn(opts ...MarkdownOption) Attachment
}
// Attachment is a slack attachment
type Attachment struct {
Title string `json:"title"`
TitleLink string `json:"title_link"`
Text string `json:"text"`
Fallback string `json:"fallback"`
Markdown bool `json:"mrkdwn"`
MarkdownIn []string `json:"mrkdwn_in,omitempty"`
Color string `json:"color"`
Fields []Field `json:"fields"`
}
// Field is an a slack attachment
type Field struct {
Title string `json:"title"`
Value string `json:"value"`
}
// SetText is an attachments text
func (a Attachment) SetText(t string) Attachment {
a.Text = t
return a
}
// SetColor sets the message colour
func (a Attachment) SetColor(c string) Attachment {
a.Color = c
return a
}
// SetTitle of the message
func (a Attachment) SetTitle(t string) Attachment {
a.Title = t
return a
}
// SetTitleLink of the message
func (a Attachment) SetTitleLink(l string) Attachment {
a.TitleLink = l
return a
}
// SetFields is the list of attachment fields
func (a Attachment) SetFields(f ...Field) Attachment {
a.Fields = append(a.Fields, f...)
return a
}
// SetMarkdown sets whether or not to use markdown in a message
func (a Attachment) SetMarkdown(m bool) Attachment {
a.Markdown = m
return a
}
// MarkdownConfiguration are options for message markdown
type MarkdownConfiguration struct {
options map[string]struct{}
}
// MarkdownOption is an option for markdown in a slack message
type MarkdownOption func(m *MarkdownConfiguration)
// Fields set the fields option
func Fields() MarkdownOption {
return func(m *MarkdownConfiguration) {
m.options["fields"] = struct{}{}
}
}
// Text set the text option
func Text() MarkdownOption {
return func(m *MarkdownConfiguration) {
m.options["text"] = struct{}{}
}
}
// Pretext set the pretext option
func Pretext() MarkdownOption {
return func(m *MarkdownConfiguration) {
m.options["pretext"] = struct{}{}
}
}
// SetMarkdownIn takes in supported markdown types
func (a Attachment) SetMarkdownIn(opts ...MarkdownOption) Attachment {
m := &MarkdownConfiguration{
options: map[string]struct{}{},
}
for _, o := range opts {
o(m)
}
for k := range m.options {
a.MarkdownIn = append(a.MarkdownIn, k)
}
return a
}