go 钉钉报警

代码

package main

import (
    "bytes"
    "encoding/json"
    "fmt"
    "io/ioutil"
    "net/http"
)

const webhook_url = "https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

func dingToInfo(s string) bool {
    content, data := make(map[string]string), make(map[string]interface{})
    content["content"] = s
    data["msgtype"] = "text"
    data["text"] = content
    b, _ := json.Marshal(data)

    resp, err := http.Post(webhook_url,
        "application/json",
        bytes.NewBuffer(b))
    if err != nil {
        fmt.Println(err)
    }
    defer resp.Body.Close()
    body, _ := ioutil.ReadAll(resp.Body)
    fmt.Println(string(body))
    return true
}

func main()  {
    dingToInfo("报警")
}

猜你喜欢

转载自blog.51cto.com/hequan/2374161