Compare commits
No commits in common. "main" and "master" have entirely different histories.
|
@ -1,7 +1,8 @@
|
|||
FROM alpine:latest
|
||||
FROM 192.168.8.125:30016/golang/golang:1.22.2
|
||||
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"]
|
75
cmd/main.go
75
cmd/main.go
|
@ -1,42 +1,73 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"github.com/jasonlvhit/gocron"
|
||||
"log"
|
||||
"time"
|
||||
"week/moonshot"
|
||||
"week/qywechat"
|
||||
|
||||
"github.com/jasonlvhit/gocron"
|
||||
"week/tools"
|
||||
)
|
||||
|
||||
var maxRetries = 3
|
||||
var maxRetries = 0
|
||||
var now = time.Now()
|
||||
var liveMode bool
|
||||
var operateTime = 1
|
||||
|
||||
func main() {
|
||||
now := time.Now()
|
||||
nowDate := now.Format("2006-01-02 15:04:05") + " " + now.Weekday().String()
|
||||
log.Printf("等待执行定时任务中...当前时间是:%s", nowDate)
|
||||
if maxRetries != 0 {
|
||||
log.Println(`
|
||||
_____
|
||||
_/ ____\______ ____ _____ _____ ____ __ _ _______ ____ ____
|
||||
\ __\\_ __ \/ _ \ / \ \__ \ / _ \ \ \/ \/ /\__ \ / \ / ___\
|
||||
| | | | \( <_> ) 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.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)
|
||||
|
||||
}
|
||||
|
|
|
@ -1,9 +1,12 @@
|
|||
package qywechat
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
//企业微信机器人发送
|
||||
|
@ -12,22 +15,36 @@ const webhookURL = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=7b50e0c
|
|||
|
||||
func Send(repContent string) {
|
||||
// 替换为你的企业微信机器人Webhook URL
|
||||
|
||||
// 创建消息体
|
||||
requestBody := []byte(`{
|
||||
"msgtype": "text",
|
||||
"text": {
|
||||
"content": "` + repContent + `"
|
||||
}
|
||||
}`)
|
||||
|
||||
// 发送HTTP POST请求到企业微信机器人Webhook
|
||||
resp, err := http.Post(webhookURL, "application/json", bytes.NewBuffer(requestBody))
|
||||
jsonData, err := json.Marshal(repContent)
|
||||
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()
|
||||
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))
|
||||
|
||||
}
|
||||
|
|
|
@ -1,18 +1,40 @@
|
|||
package tools
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
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)
|
||||
|
||||
// 当前时间是否为周五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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue