![WX20200326-165904@2x.png](https://static.studygolang.com/200326/5845fe0f1784757017c0ec5e9d6d8acb.png)
1.先看目录,一个入口文件,一个html,很简单。
```
bogon:dingding-notice admin$ ll
total 16
-rw-r--r--@ 1 admin staff 1.9K 3 26 16:35 main.go
-rw-r--r--@ 1 admin staff 1.9K 3 26 15:55 index.html
```
2.index.html页面,一个简单的textarea,和一个button。
```
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.4.1/dist/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<title>钉钉</title>
</head>
<body>
<div class="container-md">
<div class="row">
<div class="col-md-6 offset-md-3">
<h1 class="text-center">钉钉消息通知</h1>
<form action="send" method="post" >
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text">消息</span>
</div>
<textarea class="form-control" aria-label="With textarea" name="content"></textarea>
</div>
</br>
<button type="submit" class="btn btn-success" style="float:right" >发送</button>
</form>
</div>
</div>
</div>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://cdn.jsdelivr.net/npm/jquery@3.4.1/dist/jquery.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.4.1/dist/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
</body>
</html>
```
3.看入口文件main.go
```
package main
import (
"bytes"
"encoding/json"
"html/template"
"io/ioutil"
"log"
"net/http"
"unsafe"
)
//钉钉要求的json消息格式,复制粘贴属性。
type Contentinfo struct {
Content string `json:"content"`
}
type IsAtAllinfo struct {
IsAtAll bool `json:"isAtAll"`
}
type Contents struct {
Msgtype string `json:"msgtype"`
Text Contentinfo `json:"text"`
At IsAtAllinfo `json:"at"`
}
func checkErr(err error) {
if err != nil {
panic(err)
}
}
func index(w http.ResponseWriter, r *http.Request) {
log.Println(r.RemoteAddr, r.Host, r.Method, r.RequestURI)
t, _ := template.ParseFiles("index.html")
t.Execute(w, nil)
}
func sms_send(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
c := &Contents{} //实例化一个对象
c.Msgtype = "text"
c.Text.Content = r.FormValue("content")
c.At.IsAtAll = true
data, _ := json.Marshal(c)
log.Println(string(data))
reader := bytes.NewReader(data)
url := "https://oapi.dingtalk.com/robot/send?access_token=xxxx" //拼接参数,xxxx为自己钉钉群里的生成的机器人access_token
request, err := http.NewRequest("POST", url, reader)
checkErr(err)
request.Header.Set("Content-Type", "application/json")
client := http.Client{}
resp, err := client.Do(request) //发个请求
checkErr(err)
respBytes, err := ioutil.ReadAll(resp.Body)
checkErr(err)
str := (*string)(unsafe.Pointer(&respBytes))
log.Println(*str) //看看请求后返回啥,正确情况下是这样婶儿的:{"errcode":0,"errmsg":"ok"}
//碰到个错误 {"errcode":310000,"errmsg":"ip xx.xx.xx.xx not in whitelist"} ,已经说明的很明白了,ip不在白名单,钉钉机器人里配置。
log.Println(r.RemoteAddr, r.Host, r.Method, r.RequestURI, r.FormValue("content"))
t, _ := template.ParseFiles("index.html")
t.Execute(w, nil)
}
func main() {
http.HandleFunc("/", index) //打开页面
http.HandleFunc("/send", sms_send) //点发送button后,post数据。
err := http.ListenAndServe(":80", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
```
4.群里是这个样子滴
![WX20200326-165656@2x.png](https://static.studygolang.com/200326/c8a2caf55bad0a6e659bb018b4321b87.png)
新手入门,在项目中学习。。。????
有疑问加站长微信联系(非本文作者))