package main import ( "log" "sync" "time" "github.com/levigross/grequests" ) // AlertOver 报警相关 type AlertOver struct { Collection sync.Map // k 是title v是时间戳 用于统计上次的发送时间, 发送题目不能重复 Setting map[string]string } // SetDefaultSetting 设置默认Setting func (ao *AlertOver) SetDefaultSetting() { ao.Setting = map[string]string{ "source": "s-577f047d-763a-4f45-a652-475595dc", "receiver": "g-5b4ce1ea-7f6b-422a-9127-a9049c65", "priority": "1", } } func (ao *AlertOver) send(data map[string]string) { // 合并初始化设置 for k, v := range ao.Setting { data[k] = v } resp, err := grequests.Post("https://api.alertover.com/v1/alert", &grequests.RequestOptions{Data: data}) now := time.Now().Unix() ao.Collection.Store(data["title"], now) ErrorLog(err) log.Println(resp.String()) } // Alert 报警API func (ao *AlertOver) Alert(title, content string) { data := map[string]string{ "title": title, "content": content, } now := time.Now().Unix() if _t, ok := ao.Collection.Load(title); ok { lasttime := _t.(int64) if now-lasttime >= 600 { ao.send(data) } } else { ao.Collection.Store(title, now) ao.send(data) } }