Skip to content

Instantly share code, notes, and snippets.

@manzil-infinity180
Created April 30, 2025 14:30
Show Gist options
  • Save manzil-infinity180/3ec8682d7db5ca5ab6d38c68136113d4 to your computer and use it in GitHub Desktop.
Save manzil-infinity180/3ec8682d7db5ca5ab6d38c68136113d4 to your computer and use it in GitHub Desktop.
Slack Webhook Message Code in Golang
func makeRequest(url string) error {
//payload := map[string]string{
// "text": string(clusterJSON),
//}
//payload := map[string]interface{}{
// "blocks": []map[string]interface{}{
// {
// "type": "section",
// "text": map[string]string{
// "type": "mrkdwn",
// "text": "*Cluster1 is Unhealthy*\n*controller-manager:* ❌ Unhealthy\n*etcd:* ✅ Healthy",
// },
// },
// {
// "type": "context",
// "elements": []map[string]string{
// {
// "type": "mrkdwn",
// "text": "_Sent by KubeStellar Health Monitor_",
// },
// },
// },
// },
//}
payload := map[string]interface{}{
"blocks": []map[string]interface{}{
{
"type": "section",
"text": map[string]string{
"type": "mrkdwn",
"text": "*Cluster1 is Unhealthy*\n*controller-manager:* ❌ Unhealthy\n*etcd:* ✅ Healthy",
},
},
},
}
body, err := json.Marshal(payload)
//req, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer([]byte(requestBody)))
req, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(body))
if err != nil {
//c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return fmt.Errorf(err.Error())
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
//c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return fmt.Errorf(err.Error())
}
defer resp.Body.Close()
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment