devops/feishu/client/h.go
2025-04-02 10:26:57 +08:00

82 lines
2.1 KiB
Go

package client
import (
"bytes"
"encoding/json"
"feishu/data"
"fmt"
"net/http"
"time"
)
func SendMsg(data []*data.Operation) error {
retainStr := ""
for _, v := range data {
retainStr += fmt.Sprintf("| %s | %d | %d | %.2f | %.2f%% | %.2f%% | %d | %d |\n", v.Date.Format("2006-01-02"), v.Register, v.Login, v.Recharge, float64(v.SecondRemain)/float64(v.Recharge), float64(v.ThirdRemain)/float64(v.Recharge), v.SeventhRemain, v.ThirtiethRemain)
}
str := fmt.Sprintf(`
# 日期 %s 😁
-----------------------------
## 昨日数据
- 注册:%d
- 充值:%.2f
- 登录:%d
----------------------
## 留存数据
| Date | Reg | Login | Pay | Retain2 | Retain3 | Retain7 | Retain30 |
| -------- | -------- | -------- | -------- | -------- | -------- | -------- | -------- |
%s
## 玩家数据
| Uid | Level | Exp | Diamond | Star | Energy |
| -------- | -------- | -------- | -------- | -------- | -------- |
| 1 | 2 | 3 | 4 | 5 | 6 |
| 1 | 2 | 3 | 4 | 5 | 6 |
`, time.Now().In(time.UTC).Format("2006-01-02"), data[0].Register, data[0].Recharge, data[0].Login, retainStr)
// 创建请求体
payload := map[string]interface{}{
"msg_type": "interactive",
"card": map[string]interface{}{
"type": "template",
"data": map[string]interface{}{
"template_id": "AAqBcfmUwQya1",
"template_version_name": "1.0.1",
"template_variable": map[string]interface{}{
"msg": str,
"appName": "merge_pet_london",
},
},
},
}
payloadBytes, err := json.Marshal(payload)
if err != nil {
return err
}
// 创建HTTP请求
req, err := http.NewRequest("POST", "https://open.feishu.cn/open-apis/bot/v2/hook/64bad1f3-3a41-4dca-9037-399067ffb252", bytes.NewBuffer(payloadBytes))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
// 发送请求
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
// 检查响应状态码
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("failed to send message, status code: %d", resp.StatusCode)
}
return nil
}