-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
143 lines (125 loc) · 4.25 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
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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"bytes"
"os"
)
// Payload structure to match the incoming JSON
type Payload struct {
CreatedAt string `json:"created_at"`
TransactionID string `json:"transaction_id"`
Type string `json:"type"`
SupporterName string `json:"supporter_name"`
SupporterAvatar string `json:"supporter_avatar"`
SupporterMessage string `json:"supporter_message"`
Media *string `json:"media"`
Unit string `json:"unit"`
UnitIcon string `json:"unit_icon"`
Quantity int `json:"quantity"`
Price int `json:"price"`
NetAmount int `json:"net_amount"`
}
// DiscordWebhook structure for the output JSON to Discord
type DiscordWebhook struct {
Content string `json:"content"`
Embeds []Embed `json:"embeds"`
Attachments []string `json:"attachments"`
}
type Embed struct {
Description string `json:"description"`
Color int `json:"color"`
Author Author `json:"author"`
Footer Footer `json:"footer"`
Thumbnail Thumbnail `json:"thumbnail"`
}
type Author struct {
Name string `json:"name"`
IconURL string `json:"icon_url"`
}
type Footer struct {
Text string `json:"text"`
}
type Thumbnail struct {
URL string `json:"url"`
}
var discordWebhookURL string
var validToken string
func init() {
discordWebhookURL = os.Getenv("DISCORD_WEBHOOK_URL")
validToken = os.Getenv("VALID_TOKEN")
}
// Function to handle incoming webhook requests
func webhookHandler(w http.ResponseWriter, r *http.Request) {
// Extract token from X-Webhook-Token header
token := r.Header.Get("X-Webhook-Token")
if token == "" {
http.Error(w, "Missing X-Webhook-Token token", http.StatusUnauthorized)
return
}
// Validate the token
if token != validToken {
http.Error(w, "Invalid X-Webhook-Token token", http.StatusUnauthorized)
return
}
// Read the body of the request
body, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, "Unable to read request body", http.StatusBadRequest)
return
}
// Parse the incoming payload
var payload Payload
if err := json.Unmarshal(body, &payload); err != nil {
http.Error(w, "Invalid JSON format", http.StatusBadRequest)
return
}
// Create the Discord webhook message
discordMessage := DiscordWebhook{
Embeds: []Embed{
{
Description: fmt.Sprintf("**%s** mentraktir **%d %s** senilai **Rp %d** dengan pesan _**\"%s\"**_",
payload.SupporterName, payload.Quantity, payload.Unit, payload.Price, payload.SupporterMessage),
Color: 261932, // Custom color for the embed
Author: Author{
Name: payload.SupporterName,
IconURL: payload.SupporterAvatar,
},
Footer: Footer{
Text: "Dukung Datenshi Community di https://trakteer.id/datenshi",
},
Thumbnail: Thumbnail{
URL: payload.UnitIcon,
},
},
},
Attachments: []string{},
}
// Convert the message to JSON
discordPayload, err := json.Marshal(discordMessage)
if err != nil {
http.Error(w, "Error creating Discord message", http.StatusInternalServerError)
return
}
// Send the message to the Discord webhook
// Todo
// This payload successfully send to discord but return internal server error
resp, err := http.Post(discordWebhookURL, "application/json", bytes.NewBuffer(discordPayload))
// Menghapus pemeriksaan status respons
if err != nil {
http.Error(w, "Failed to send message to Discord", http.StatusInternalServerError)
return
}
// Menambahkan log untuk mencatat respons
log.Printf("Discord response status: %s", resp.Status)
w.WriteHeader(http.StatusOK)
w.Write([]byte("Payload sent to Discord successfully"))
}
func main() {
http.HandleFunc("/webhook", webhookHandler)
log.Println("Server started on :8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}