-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
70 lines (55 loc) · 1.4 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
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
)
func main() {
url := flag.String("url", "url", "The slack webhook url")
text := flag.String("text", "text", "The slack message you want to post")
channel := flag.String("channel", "channel", "Override the channel")
username := flag.String("username", "username", "Override the username")
flag.Parse()
if *url == "url" {
fmt.Println("Please provide a webhook url")
os.Exit(0)
}
if *text == "text" {
fmt.Println("Please provide text")
os.Exit(0)
}
fmt.Println("Webhook URL: ", *url)
dataMap := map[string]string{
"text": *text,
"username": "Slackr",
"icon_emoji": ":monkey:",
}
if *channel != "channel" {
dataMap["channel"] = *channel
}
if *username != "username" {
dataMap["username"] = *username
}
jsonValue, _ := json.Marshal(dataMap)
jsonString := bytes.NewBuffer(jsonValue)
fmt.Println("Sending payload", jsonString)
req, err := http.NewRequest("POST", *url, jsonString)
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Printf("Could not post %s to %s\n", jsonValue, *url)
panic(err)
}
defer resp.Body.Close()
fmt.Println("Response status:", resp.Status)
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println("Response body:", string(body))
if resp.Status != "200 OK" {
os.Exit(1)
}
}