要实现功能:

  1. 企微机器人提醒
  2. 机器人数量不一定
  3. 机器人提醒企微有限制 一分钟不能超过20条

准备好发送markdown消息的方法

type RobotRsp struct {
	ErrCode int    `json:"errcode"`
	ErrMsg  string `json:"errmsg"`
}

type RobotMsg struct {
	Msgtype string `json:"msgtype"`
	Text RobotContent `json:"text"`
	Markdown RobotContent `json:"markdown"`
}

type RobotContent struct {
	Content string `json:"content"`
}

func RobotMarkdownMsg(content string, url string)  {
	var rspInfo = rsp.RobotRsp{}
	var robotMsg = req.RobotMsg{
		Msgtype: "markdown",
		Markdown: req.RobotContent{
			Content:content,
		},
	}
	_ = ApiPost(url, &rspInfo, robotMsg, time.Second * 3)
	if rspInfo.ErrCode != 0 {
		fmt.Printf("RobotMsg markdown信息发送失败:%#v \n", rspInfo)
	}
}

将发送消息的Service加好

我们初始化的时候要给 *RobotMsgService类型 加一个时钟,这样没过20秒检查一下RobotTimer中有没有超过一分钟,需要发送消息的数据;

我们如果收到消息之后,就立马将消息放到以url作为key的redis hashMap中 这样需要发送消息的时候只要找到有这个redis队列缓存就行,你重启服务也不会因为服务中断导致队列中数据消失;

如果触发了发送立马去检查UrlsMap[url] 查看这个struct中已经发送的数量如果大于等于20 我们直接结束,如果小于20条,我们发送20-x = 剩余条数

如果还有好多没有发完,我们的ticket会20秒定时去检查最近发送时间有没有超过一分钟,如果超过 我们就开始发送数据,并发完之后写入发送条数和最新的发送时间(加锁);

// 只有queue才能触发这个变量 (防止循环引用)
var NewRobotMsgService = service.NewRobotMsgService()

我们在一个单独的文件中将Service初始化,之后直接使用变量就行,这样就不会重复触发时钟,和多个NewRobotMsgService实例的情况;