-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
81 lines (72 loc) · 1.75 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
package main
import (
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
"log"
"net/http"
"net/url"
"os"
)
type SlackAttachments struct {
Color string `json:"color"`
Fallback string `json:"fallback"`
Title string `json:"title"`
Text string `json:"text"`
}
type SlackParams struct {
Attachments []SlackAttachments `json:"attachments"`
Username string `json:"username"`
Mrkdwn bool `json:"mrkdwn"`
IconEmoji string `json:"icon_emoji"`
}
func color(s string) string {
if s == "Up" {
return "#00fd55"
}
return "#cc3300"
}
func text(url string) string {
return fmt.Sprintf("<%s|Link> • <https://www.statuscake.com/App/YourStatus.php|View details>", url)
}
func fallback(name, status string) string {
return fmt.Sprintf("%s is %s - https://www.statuscake.com/App/YourStatus.php", name, status)
}
func title(name, status string) string {
return fmt.Sprintf("%s is %s", name, status)
}
func handler(c *gin.Context) {
URL := c.PostForm("URL")
name := c.PostForm("Name")
status := c.PostForm("Status")
slack := SlackParams{
Attachments: []SlackAttachments{
SlackAttachments{
Color: color(status),
Fallback: fallback(name, status),
Title: title(name, status),
Text: text(URL),
},
},
Username: "StatusCake Bot",
Mrkdwn: true,
IconEmoji: ":cake:",
}
url_slack := fmt.Sprintf("https://hooks.slack.com/%s", c.Param("url"))
b, err := json.Marshal(slack)
if err != nil {
c.String(400, "fail")
return
}
http.PostForm(url_slack, url.Values{"payload": {string(b)}})
c.String(200, "ok")
}
func main() {
port := os.Getenv("PORT")
if port == "" {
log.Fatal("$PORT must be set")
}
router := gin.Default()
router.POST("/*url", handler)
router.Run(":" + port)
}