This repository has been archived by the owner on Aug 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
incoming_webhook.go
72 lines (61 loc) · 1.79 KB
/
incoming_webhook.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
package golang_slacktools
import (
"encoding/json"
"strings"
"net/http"
"fmt"
)
type IncomingWebhook struct {
Text string `json:"text"`
Attachments []Attachment `json:"attachments"`
}
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"`
Fields []Field `json:"fields"`
ImageURL string `json:"image_url"`
ThumbURL string `json:"thumb_url"`
}
type Field struct {
Title string `json:"title"`
Value string `json:"value"`
Short bool `json:"short"`
}
func GetSimpleWebhook(text string) IncomingWebhook {
return IncomingWebhook{Text:text}
}
func GetSimpleAttachment(text string) Attachment{
return Attachment{Text:text}
}
func (hook *IncomingWebhook) AddAttachment (add Attachment){
hook.Attachments=append(hook.Attachments,add)
}
func SendWebhook(hook IncomingWebhook, url string) error{
if(url=="" || !strings.HasPrefix(url, "https://hooks.slack.com/services/")){
return fmt.Errorf("WebhookUrl not configured. The program is unable to send a webhook")
}
marshaled, err :=json.Marshal(hook)
if(err != nil) {
return fmt.Errorf("Marshalling webhook failed: %q", err)
}
req, err :=http.NewRequest("POST",url,strings.NewReader(string(marshaled[:])))
if(err!=nil) {
return fmt.Errorf("Creating new HTTP-request for a webhook failed: %q", err)
}
req.Header.Set("Content-Type", "application/json")
resp , err :=http.DefaultClient.Do(req)
if(err!=nil){
return fmt.Errorf("Sending webhook failed: %q", err)
}
if(resp.StatusCode!=http.StatusOK){
return fmt.Errorf("Response is not status ok")
}
return nil
}