-
Notifications
You must be signed in to change notification settings - Fork 61
/
main.go
87 lines (75 loc) · 2.47 KB
/
main.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
package slack
import (
"fmt"
"github.com/parnurzeal/gorequest"
)
type Field struct {
Title string `json:"title"`
Value string `json:"value"`
Short bool `json:"short"`
}
type Action struct {
Type string `json:"type"`
Text string `json:"text"`
Url string `json:"url"`
Style string `json:"style"`
}
type Attachment struct {
Fallback *string `json:"fallback"`
Color *string `json:"color"`
PreText *string `json:"pretext"`
AuthorName *string `json:"author_name"`
AuthorLink *string `json:"author_link"`
AuthorIcon *string `json:"author_icon"`
Title *string `json:"title"`
TitleLink *string `json:"title_link"`
Text *string `json:"text"`
ImageUrl *string `json:"image_url"`
Fields []*Field `json:"fields"`
Footer *string `json:"footer"`
FooterIcon *string `json:"footer_icon"`
Timestamp *int64 `json:"ts"`
MarkdownIn *[]string `json:"mrkdwn_in"`
Actions []*Action `json:"actions"`
CallbackID *string `json:"callback_id"`
ThumbnailUrl *string `json:"thumb_url"`
}
type Payload struct {
Parse string `json:"parse,omitempty"`
Username string `json:"username,omitempty"`
IconUrl string `json:"icon_url,omitempty"`
IconEmoji string `json:"icon_emoji,omitempty"`
Channel string `json:"channel,omitempty"`
Text string `json:"text,omitempty"`
LinkNames string `json:"link_names,omitempty"`
Attachments []Attachment `json:"attachments,omitempty"`
UnfurlLinks bool `json:"unfurl_links,omitempty"`
UnfurlMedia bool `json:"unfurl_media,omitempty"`
Markdown bool `json:"mrkdwn,omitempty"`
}
func (attachment *Attachment) AddField(field Field) *Attachment {
attachment.Fields = append(attachment.Fields, &field)
return attachment
}
func (attachment *Attachment) AddAction(action Action) *Attachment {
attachment.Actions = append(attachment.Actions, &action)
return attachment
}
func redirectPolicyFunc(req gorequest.Request, via []gorequest.Request) error {
return fmt.Errorf("Incorrect token (redirection)")
}
func Send(webhookUrl string, proxy string, payload Payload) []error {
request := gorequest.New().Proxy(proxy)
resp, _, err := request.
Post(webhookUrl).
RedirectPolicy(redirectPolicyFunc).
Send(payload).
End()
if err != nil {
return err
}
if resp.StatusCode >= 400 {
return []error{fmt.Errorf("Error sending msg. Status: %v", resp.Status)}
}
return nil
}