钉钉机器人使用webhook发送消息

package main

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

type DingRobot struct {
	RobotId string
}
type ParamCronTask struct {
	At struct {
		AtMobiles []struct {
			AtMobile string `json:"atMobile"`
		} `json:"atMobiles"` //At和Tele是一对多关系
		IsAtAll bool `json:"isAtAll"`
	} `json:"at"` //存储的是@的成员
	Text struct {
		Content string `json:"content"`
	} `json:"text"` //存储的是用户所发送的信息
	Msgtype string `json:"msgtype"`
}

func (t *DingRobot) SendMessage(p *ParamCronTask) error {
	b := []byte{}
	//我们需要在文本,链接,markdown三种其中的一个
	if p.Msgtype == "text" {
		msg := map[string]interface{}{}
		atMobileStringArr := make([]string, len(p.At.AtMobiles))
		for i, atMobile := range p.At.AtMobiles {
			atMobileStringArr[i] = atMobile.AtMobile
		}

		msg = map[string]interface{}{
			"msgtype": "text",
			"text": map[string]string{
				"content": p.Text.Content,
			},
		}
		if p.At.IsAtAll {
			msg["at"] = map[string]interface{}{
				"isAtAll": p.At.IsAtAll,
			}
		} else {
			msg["at"] = map[string]interface{}{
				"atMobiles": atMobileStringArr, //字符串切片类型

				"isAtAll": p.At.IsAtAll,
			}
		}
		b, _ = json.Marshal(msg)
	}
	var resp *http.Response
	var err error

	resp, err = http.Post(t.getURLV2(), "application/json", bytes.NewBuffer(b))

	if err != nil {
		return err
	}
	defer resp.Body.Close()
	date, err := ioutil.ReadAll(resp.Body)
	r := struct {
		Errcode int    `json:"errcode"`
		Errmsg  string `json:"errmsg"`
	}{}
	err = json.Unmarshal(date, &r)
	if err != nil {
		return err
	}
	if r.Errcode != 0 {
		fmt.Println(r.Errmsg)
		return errors.New(r.Errmsg)
	}

	return nil
}
func (t *DingRobot) getURLV2() string {
	url := "https://oapi.dingtalk.com/robot/send?access_token=" + t.RobotId //拼接token路径
	return url
}
func main() {
	param := &ParamCronTask{
		At: struct {
			AtMobiles []struct {
				AtMobile string `json:"atMobile"`
			} `json:"atMobiles"`
			IsAtAll bool `json:"isAtAll"`
		}{
			AtMobiles: []struct {
				AtMobile string `json:"atMobile"`
			}{
				{
					AtMobile: "18737480171",
				},
				{
					AtMobile: "mobile2",
				},
			},
			IsAtAll: false,
		},
		Text: struct {
			Content string `json:"content"`
		}{
			Content: "Hello, World!",
		},
		Msgtype: "text",
	}
	err := (&DingRobot{RobotId: "aba857cf3ba132581d1a99f3f5c9c5fe2754ffd57a3e7929b6781367b9325e40"}).SendMessage(param)
	if err != nil {
		fmt.Println(err)
	}
}

Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐