-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.go
146 lines (118 loc) · 2.68 KB
/
message.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
package go_msteams
import (
"bytes"
"encoding/json"
"errors"
"io"
"net/http"
)
const (
MessageCard = "MessageCard"
DefaultContext = "http://schema.org/extensions"
)
type Fact struct {
Name string `json:"name"`
Value string `json:"value"`
}
type Section struct {
Title string `json:"activityTitle"`
Subtitle string `json:"activitySubtitle"`
Image string `json:"activityImage"`
Facts []Fact `json:"facts"`
}
func (s *Section) AddFact(name string, value string) {
var fact = Fact{
Name: name,
Value: value,
}
if s.Facts == nil {
s.Facts = []Fact{fact}
return
}
s.Facts = append(s.Facts, fact)
}
type Message struct {
MessageType string `json:"@type"`
Context string `json:"@context"`
Summary string `json:"summary"`
Text string `json:"text"`
Sections []Section `json:"sections"`
Color string `json:"themeColor"`
PotentialActions []Button `json:"potentialAction"`
}
func (m *Message) SetColor(color string) {
m.Color = color
}
func (m *Message) AddButton(btn Button) {
m.PotentialActions = append(m.PotentialActions, btn)
}
func (m *Message) AddSection(title string, subtitle string, image string) *Section{
var section = Section{
Title: title,
Subtitle: subtitle,
Image: image,
}
if m.Sections == nil {
m.Sections = []Section{}
}
m.Sections = append(m.Sections, section)
var index = len(m.Sections)
return &m.Sections[index - 1]
}
type Sender struct {
WebhookUrl string
}
func GetDemoMessage() Message {
return Message{
MessageType: MessageCard,
Context: "http://schema.org/extensions",
Summary: "Larry Bryant created a new task",
Text: "Demo text",
Sections: []Section{
{
Title: "Demo section",
Subtitle: "Demo subtitle",
Image: "https://images.genius.com/4226c55edbafb61f52f4d6be22a9c65c.500x500x1.jpg",
Facts: []Fact{
{
Name: "Demo Fact",
Value: "Demo Value",
},
},
},
},
}
}
func NewMessage(summary string) Message{
return Message{
MessageType: MessageCard,
Context: DefaultContext,
Summary: summary,
Text: "",
Sections: nil,
}
}
func NewSection(title string, subtitle string, image string) Section {
return Section{
Title: title,
Subtitle: subtitle,
Image: image,
Facts: []Fact{},
}
}
func (s Sender) SendMessage(message Message) error{
var data, err = json.Marshal(message)
if err != nil {
return err
}
resp, err := http.Post(s.WebhookUrl, "application/json", bytes.NewBuffer(data))
if err != nil {
return err
}
body, _ := io.ReadAll(resp.Body)
responseString := string(body)
if responseString == "1" {
return nil
}
return errors.New(responseString)
}