Last active
December 3, 2021 09:18
-
-
Save techno-tanoC/9555324ca5d5faf81e80b427086e3878 to your computer and use it in GitHub Desktop.
slack incoming webhook snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"encoding/json" | |
"io/ioutil" | |
"net/http" | |
) | |
func notifySlack(url, msg string) (string, error) { | |
payload := struct { | |
Text string `json:"text"` | |
}{ | |
msg, | |
} | |
data, err := json.Marshal(payload) | |
if err != nil { | |
return "", err | |
} | |
resp, err := http.Post(url, "application/json", bytes.NewReader(data)) | |
if err != nil { | |
return "", err | |
} | |
defer resp.Body.Close() | |
bs, err := ioutil.ReadAll(resp.Body) | |
if err != nil { | |
return "", err | |
} | |
return string(bs), nil | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"encoding/json" | |
"log" | |
"net/http" | |
) | |
func main() { | |
} | |
func notifySlack(url string, message string) error { | |
values := map[string]string{ | |
"text": message, | |
} | |
data, err := json.Marshal(values) | |
if err != nil { | |
log.Fatal(err) | |
} | |
resp, err := http.Post(url, "application/json", bytes.NewReader(data)) | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer resp.Body.Close() | |
return err | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use anyhow::Result; | |
use reqwest::Client; | |
use serde::Serialize; | |
#[derive(Debug, Clone, Serialize)] | |
struct Text { | |
text: String, | |
} | |
async fn notify_slack(url: &str, message: &str) -> Result<()> { | |
let payload = Text { text: message.to_string() }; | |
Client::new() | |
.post(url) | |
.json(&payload) | |
.send() | |
.await?; | |
Ok(()) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use anyhow::Result; | |
use reqwest::Client; | |
use serde_json::json; | |
pub async fn notify_slack(url: &str, text: &str) -> Result<()> { | |
let payload = json!({ | |
"text": text | |
}); | |
Client::new().post(url).json(&payload).send().await?; | |
Ok(()) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
notify_slack() { | |
BODY=$(cat <<EOF | |
{ | |
"blocks": [ | |
{ | |
"type": "section", | |
"text": { | |
"type": "mrkdwn", | |
"text": "$2" | |
} | |
} | |
] | |
} | |
EOF | |
) | |
curl -X POST -d "$BODY" "$1" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment