go实现企业微信机器人消息(原生库)

func weworkAlarm(text string, mentionAll bool) {
  mentionAllStr := ""
  if mentionAll {
    mentionAllStr = "@all"
  }
  dataJsonStr := fmt.Sprintf(`{"msgtype": "text", "text": {"content": "%s", "mentioned_list": [%s]}}`, text, mentionAllStr)
  fmt.Println(dataJsonStr)

  resp, err := http.Post(
    "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxx",
    "application/json",
    bytes.NewBuffer([]byte(dataJsonStr)))

  if err != nil {
    fmt.Println("weworkAlarm request error")
  }
  defer resp.Body.Close()
}

顺道演示了go http post json的用法

Leave a Reply

Your email address will not be published. Required fields are marked *