Compare commits

..

No commits in common. "main" and "master" have entirely different histories.
main ... master

5 changed files with 122 additions and 51 deletions

View File

@ -1,7 +1,8 @@
FROM alpine:latest FROM 192.168.8.125:30016/golang/golang:1.22.2
USER root USER root
RUN apk --no-cache add tzdata RUN apk --no-cache add tzdata
ENV TZ=Asia/Shanghai ENV TZ=Asia/Shanghai
COPY ai-week /usr/local/bin COPY ai-week /usr/local/bin
RUN chmod +x /usr/local/bin/ai-week RUN chmod +x /usr/local/bin/ai-week
ENTRYPOINT ["ai-week"] ENTRYPOINT ["ai-week"]
CMD ["--livemode"]

View File

@ -1,42 +1,73 @@
package main package main
import ( import (
"flag"
"fmt"
"github.com/jasonlvhit/gocron"
"log" "log"
"time" "time"
"week/moonshot" "week/moonshot"
"week/qywechat" "week/qywechat"
"week/tools"
"github.com/jasonlvhit/gocron"
) )
var maxRetries = 3 var maxRetries = 0
var now = time.Now()
var liveMode bool
var operateTime = 1
func main() { func main() {
now := time.Now() log.Println(`
nowDate := now.Format("2006-01-02 15:04:05") + " " + now.Weekday().String() _____
log.Printf("等待执行定时任务中...当前时间是:%s", nowDate) _/ ____\______ ____ _____ _____ ____ __ _ _______ ____ ____
if maxRetries != 0 { \ __\\_ __ \/ _ \ / \ \__ \ / _ \ \ \/ \/ /\__ \ / \ / ___\
| | | | \( <_> ) Y Y \ / __ \( <_> ) \ / / __ \| | \/ /_/ >
|__| |__| \____/|__|_| / (____ /\____/ \/\_/ (____ /___| /\___ /
\/ \/ \/ \//_____/ `)
fmt.Println("-----------------------------------------------------------------------------------------")
fmt.Println("-----------------------------------------------------------------------------------------")
flag.BoolVar(&liveMode, "livemode", false, "即时模式默认关闭")
flag.Parse()
if !liveMode {
log.Println("目前是定时任务模式")
log.Printf("等待任务的第%v次执行...", operateTime)
// 定义任务,每周五的五点执行 // 定义任务,每周五的五点执行
gocron.Every(1).Friday().At("17:00").Do(job) gocron.Every(1).Friday().At("17:00").Do(job)
// 开始定时任务 // 开始定时任务
<-gocron.Start() <-gocron.Start()
} else {
log.Println("目前是即时任务模式")
job()
}
}
func job() {
//最多重试3次
if maxRetries < 3 {
nowDate := now.Format("2006-01-02 15:04:05") + " " + now.Weekday().String()
log.Printf("开始执行任务,当前时间是:%v", nowDate)
err := moonshot.CreateExcel()
if err != nil {
maxRetries++
log.Printf("执行失败,开始第%v次重试...", maxRetries)
log.Println("----------------------------------------------------------------------")
job()
}
repContent, err := moonshot.AiChat()
if err != nil {
maxRetries++
log.Printf("执行失败,开始第%v次重试...", maxRetries)
log.Println("----------------------------------------------------------------------")
job()
}
qywechat.Send(repContent)
operateTime++
log.Printf("任务执行成功,当前时间是:%v", nowDate)
log.Printf("等待%v次任务执行还剩:%v", operateTime, tools.TimeUntilFriday())
} else { } else {
log.Fatalln("最大重试次数已到达3退出") log.Fatalln("最大重试次数已到达3退出")
} }
} }
func job() {
err := moonshot.CreateExcel()
if err != nil {
job()
maxRetries--
}
repContent, err := moonshot.AiChat()
if err != nil {
job()
maxRetries--
}
qywechat.Send(repContent)
}

View File

@ -1,9 +1,12 @@
package qywechat package qywechat
import ( import (
"bytes" "encoding/json"
"fmt"
"io"
"log" "log"
"net/http" "net/http"
"strings"
) )
//企业微信机器人发送 //企业微信机器人发送
@ -12,22 +15,36 @@ const webhookURL = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=7b50e0c
func Send(repContent string) { func Send(repContent string) {
// 替换为你的企业微信机器人Webhook URL // 替换为你的企业微信机器人Webhook URL
jsonData, err := json.Marshal(repContent)
// 创建消息体
requestBody := []byte(`{
"msgtype": "text",
"text": {
"content": "` + repContent + `"
}
}`)
// 发送HTTP POST请求到企业微信机器人Webhook
resp, err := http.Post(webhookURL, "application/json", bytes.NewBuffer(requestBody))
if err != nil { if err != nil {
log.Fatalf("Failed to send message: %v", err) fmt.Println("转换为JSON时出错:", err)
return
}
repContent = string(jsonData)
// 去掉最后三个字符和第一个引号
repContent = repContent[:len(repContent)-3]
repContent = repContent[1:]
fmt.Println(repContent)
// 创建消息体
repContent = fmt.Sprintf(`{"msgtype": "text", "text": {"content": "%s"}}`, repContent)
//repContent = strings.Replace(repContent, "\\", "", 9)
//lastIndex := strings.LastIndex(repContent, "\\")
//if lastIndex != -1 {
// repContent = repContent[:lastIndex] + repContent[lastIndex+1:]
//}
// 发送HTTP POST请求到企业微信机器人Webhook
fmt.Println(repContent)
resp, err := http.Post(webhookURL, "application/json", strings.NewReader(repContent))
if err != nil {
log.Printf("发送企业微信机器人失败,错误是: %v", err)
} }
defer resp.Body.Close() defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Printf("发送企业微信机器人失败,错误是: %v", err)
}
log.Println("Message sent successfully!") log.Printf("发送企业微信机器人完成,状态码为:%v返回体为%v", resp.StatusCode, string(body))
} }

View File

@ -1,18 +1,40 @@
package tools package tools
import ( import "time"
"fmt"
"time"
)
// 当前时间是否为周五5点 func TimeUntilFriday() time.Duration {
func CheckTime() bool { today := time.Now()
nowTime := time.Now() weekday := today.Weekday()
if nowTime.Weekday().String() == "Friday" && nowTime.Format("15:04:05") == "17:00:00" { switch weekday {
fmt.Println("当前时间是周五5点") case time.Monday:
return true friday := time.Date(today.Year(), today.Month(), today.Day()+4, 17, 0, 0, 0, today.Location())
return friday.Sub(today)
case time.Tuesday:
friday := time.Date(today.Year(), today.Month(), today.Day()+3, 17, 0, 0, 0, today.Location())
return friday.Sub(today)
case time.Wednesday:
friday := time.Date(today.Year(), today.Month(), today.Day()+2, 17, 0, 0, 0, today.Location())
return friday.Sub(today)
case time.Thursday:
friday := time.Date(today.Year(), today.Month(), today.Day()+1, 17, 0, 0, 0, today.Location())
return friday.Sub(today)
case time.Friday:
friday := time.Date(today.Year(), today.Month(), today.Day(), 17, 0, 0, 0, today.Location())
if friday.Sub(today) > 0 {
return friday.Sub(today)
} else if friday.Sub(today) < 0 {
friday = time.Date(today.Year(), today.Month(), today.Day()+7, 17, 0, 0, 0, today.Location())
return friday.Sub(today)
} else { } else {
fmt.Println("当前时间不是周五5点") return 0
return false
} }
case time.Saturday:
friday := time.Date(today.Year(), today.Month(), today.Day()+6, 17, 0, 0, 0, today.Location())
return friday.Sub(today)
case time.Sunday:
friday := time.Date(today.Year(), today.Month(), today.Day()+5, 17, 0, 0, 0, today.Location())
return friday.Sub(today)
}
return 0
} }

Binary file not shown.