Compare commits

..

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

5 changed files with 49 additions and 120 deletions

View File

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

View File

@ -1,73 +1,42 @@
package main
import (
"flag"
"fmt"
"github.com/jasonlvhit/gocron"
"log"
"time"
"week/moonshot"
"week/qywechat"
"week/tools"
"github.com/jasonlvhit/gocron"
)
var maxRetries = 0
var now = time.Now()
var liveMode bool
var operateTime = 1
var maxRetries = 3
func main() {
log.Println(`
_____
_/ ____\______ ____ _____ _____ ____ __ _ _______ ____ ____
\ __\\_ __ \/ _ \ / \ \__ \ / _ \ \ \/ \/ /\__ \ / \ / ___\
| | | | \( <_> ) Y Y \ / __ \( <_> ) \ / / __ \| | \/ /_/ >
|__| |__| \____/|__|_| / (____ /\____/ \/\_/ (____ /___| /\___ /
\/ \/ \/ \//_____/ `)
fmt.Println("-----------------------------------------------------------------------------------------")
fmt.Println("-----------------------------------------------------------------------------------------")
flag.BoolVar(&liveMode, "livemode", false, "即时模式默认关闭")
flag.Parse()
if !liveMode {
log.Println("目前是定时任务模式")
log.Printf("等待任务的第%v次执行...", operateTime)
now := time.Now()
nowDate := now.Format("2006-01-02 15:04:05") + " " + now.Weekday().String()
log.Printf("等待执行定时任务中...当前时间是:%s", nowDate)
if maxRetries != 0 {
// 定义任务,每周五的五点执行
gocron.Every(1).Friday().At("17:00").Do(job)
// 开始定时任务
<-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 {
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,12 +1,9 @@
package qywechat
import (
"encoding/json"
"fmt"
"io"
"bytes"
"log"
"net/http"
"strings"
)
//企业微信机器人发送
@ -15,36 +12,22 @@ const webhookURL = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=7b50e0c
func Send(repContent string) {
// 替换为你的企业微信机器人Webhook URL
jsonData, err := json.Marshal(repContent)
if err != nil {
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:]
//}
// 创建消息体
requestBody := []byte(`{
"msgtype": "text",
"text": {
"content": "` + repContent + `"
}
}`)
// 发送HTTP POST请求到企业微信机器人Webhook
fmt.Println(repContent)
resp, err := http.Post(webhookURL, "application/json", strings.NewReader(repContent))
resp, err := http.Post(webhookURL, "application/json", bytes.NewBuffer(requestBody))
if err != nil {
log.Printf("发送企业微信机器人失败,错误是: %v", err)
log.Fatalf("Failed to send message: %v", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Printf("发送企业微信机器人失败,错误是: %v", err)
}
log.Printf("发送企业微信机器人完成,状态码为:%v返回体为%v", resp.StatusCode, string(body))
log.Println("Message sent successfully!")
}

View File

@ -1,40 +1,18 @@
package tools
import "time"
func TimeUntilFriday() time.Duration {
today := time.Now()
weekday := today.Weekday()
switch weekday {
case time.Monday:
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 {
return 0
}
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)
import (
"fmt"
"time"
)
// 当前时间是否为周五5点
func CheckTime() bool {
nowTime := time.Now()
if nowTime.Weekday().String() == "Friday" && nowTime.Format("15:04:05") == "17:00:00" {
fmt.Println("当前时间是周五5点")
return true
} else {
fmt.Println("当前时间不是周五5点")
return false
}
return 0
}

Binary file not shown.