golang slack通知

メソッド作成

func sendSlack(ctx context.Context, msg string) error {
    const endPoint = "{ 通知先URL }"
    body := []byte(fmt.Sprintf(`{"text":"%s"}`, msg))
    req, err := http.NewRequest("POST", endPoint, bytes.NewBuffer([]byte(body)))
    if err != nil {
        return err
    }
    req.Header.Set("Content-Type", "application/json; charset=UTF-8")
    client := urlfetch.Client(ctx)
    resp, err := client.Do(req)
    if err != nil {
        return err
    }
    defer resp.Body.Close()
    return nil
}

通知

sendSlack(ctx, "<!channel> エラー発生!")