From ea98672f1ed39b2aef42e2e37d39f0e81e5812f9 Mon Sep 17 00:00:00 2001 From: LJN <1213911320@qq.com> Date: Fri, 22 Dec 2023 02:44:15 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=8E=86=E5=8F=B2?= =?UTF-8?q?=E4=B8=8A=E7=9A=84=E4=BB=8A=E5=A4=A9=E3=80=8160s=E6=97=A9?= =?UTF-8?q?=E6=8A=A5=EF=BC=8C=E4=BF=AE=E5=A4=8D=E5=AE=9A=E6=97=B6=E4=BB=BB?= =?UTF-8?q?=E5=8A=A1=E4=B8=8D=E6=89=A7=E8=A1=8C=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 4 +-- go.sum | 26 +++++++++++++---- main.go | 20 +++++++++---- util/util.go | 80 +++++++++++++++++++++++++++++++++++++++++++++++++-- wechat/msg.go | 11 +++++++ 5 files changed, 125 insertions(+), 16 deletions(-) diff --git a/go.mod b/go.mod index c1416ba..8e2072d 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,8 @@ require ( github.com/fsnotify/fsnotify v1.7.0 github.com/lestrrat/go-file-rotatelogs v0.0.0-20180223000712-d3151e2a480f github.com/op/go-logging v0.0.0-20160315200505-970db520ece7 - github.com/robfig/cron/v3 v3.0.0 + github.com/robfig/cron/v3 v3.0.1 + github.com/sashabaranov/go-openai v1.17.9 github.com/spf13/viper v1.18.2 github.com/tidwall/gjson v1.17.0 ) @@ -25,7 +26,6 @@ require ( github.com/pkg/errors v0.9.1 // indirect github.com/sagikazarmark/locafero v0.4.0 // indirect github.com/sagikazarmark/slog-shim v0.1.0 // indirect - github.com/sashabaranov/go-openai v1.17.9 // indirect github.com/sourcegraph/conc v0.3.0 // indirect github.com/spf13/afero v1.11.0 // indirect github.com/spf13/cast v1.6.0 // indirect diff --git a/go.sum b/go.sum index 1f828e9..999ccf7 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,4 @@ +github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= @@ -10,16 +11,25 @@ github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHk github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0= github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= +github.com/go-co-op/gocron v1.37.0 h1:ZYDJGtQ4OMhTLKOKMIch+/CY70Brbb1dGdooLEhh7b0= +github.com/go-co-op/gocron v1.37.0/go.mod h1:3L/n6BkO7ABj+TrfSVXLRzsP26zmikL4ISkLQ0O8iNY= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4= +github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/jehiah/go-strftime v0.0.0-20171201141054-1d33003b3869 h1:IPJ3dvxmJ4uczJe5YQdrYB16oTJlGSC/OyZDqUk9xX4= github.com/jehiah/go-strftime v0.0.0-20171201141054-1d33003b3869/go.mod h1:cJ6Cj7dQo+O6GJNiMx+Pa94qKj+TG8ONdKHgMNIyyag= github.com/jonboulle/clockwork v0.4.0 h1:p4Cf1aMWXnXAUh8lVfewRBx1zaTSYKrKMF2g3ST4RZ4= github.com/jonboulle/clockwork v0.4.0/go.mod h1:xgRqUGwRcjKCO1vbZUEtSLrqKoPSsUpK7fnezOII0kc= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= +github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/lestrrat/go-envload v0.0.0-20180220120943-6ed08b54a570 h1:0iQektZGS248WXmGIYOwRXSQhD4qn3icjMpuxwO7qlo= @@ -36,13 +46,16 @@ github.com/op/go-logging v0.0.0-20160315200505-970db520ece7 h1:lDH9UUVJtmYCjyT0C github.com/op/go-logging v0.0.0-20160315200505-970db520ece7/go.mod h1:HzydrMdWErDVzsI23lYNej1Htcns9BCg93Dk0bBINWk= github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4= github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc= +github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/robfig/cron/v3 v3.0.0 h1:kQ6Cb7aHOHTSzNVNEhmp8EcWKLb4CbiMW9h9VyIhO4E= -github.com/robfig/cron/v3 v3.0.0/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro= +github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs= +github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro= +github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc= +github.com/rogpeppe/go-internal v1.8.1/go.mod h1:JeRgkft04UBgHMgCIwADu4Pn6Mtm5d4nPKWu0nJ5d+o= github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6keLGt6kNQ= @@ -51,8 +64,6 @@ github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6g github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ= github.com/sashabaranov/go-openai v1.17.9 h1:QEoBiGKWW68W79YIfXWEFZ7l5cEgZBV4/Ow3uy+5hNY= github.com/sashabaranov/go-openai v1.17.9/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg= -github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e h1:MRM5ITcdelLK2j1vwZ3Je0FKVCfqOLp5zO6trqMLYs0= -github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e/go.mod h1:XV66xRDqSt+GTGFMVlhk3ULuV0y9ZmzeVGR4mloJI3M= github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo= github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0= github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8= @@ -69,6 +80,7 @@ github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpE github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8= @@ -94,8 +106,10 @@ golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= -gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= +gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA= gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go index d817808..7a03a1e 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "github.com/eatmoreapple/openwechat" "github.com/robfig/cron/v3" _ "moyu/core" @@ -24,6 +25,8 @@ func main() { util.ReplyImage(msg) case content == "舔狗" || content == "舔狗日记": util.SendTg(msg) + case content == "历史上的今天": + util.SendHistoryToDay(msg) case strings.HasPrefix(content, "ai"): util.SendGpt(strings.TrimPrefix(content, "ai"), msg) case strings.HasPrefix(content, "sai"): @@ -42,6 +45,7 @@ func main() { ">>> 指令集:\n" + " " + atName + " 摸鱼/摸鱼人日历\n" + " " + atName + " 舔狗/舔狗日记\n" + + " " + atName + " 历史上的今天\n" + " " + atName + " ai [问题] 不携带历史\n" + " " + atName + " sai [问题] 携带历史\n" + " " + atName + " 历史 查看历史记录\n" + @@ -54,18 +58,22 @@ func main() { } } } - start() -} - -func start() { c := cron.New() - _, err := c.AddFunc("0 8 * * *", func() { util.SendImage(wechat.My302) }) + _, err := c.AddFunc("0 8 * * *", func() { + util.SendImage(wechat.My302) + util.SendNewsToDay(wechat.My302) + fmt.Println("定时任务已执行") + }) if err != nil { + fmt.Println("添加定时任务失败:", err) return } - //c.Start() + go c.Start() err = wechat.Bot.Block() if err != nil { + fmt.Println("微信机器人启动失败:", err) return } + defer c.Stop() + select {} } diff --git a/util/util.go b/util/util.go index 88ce7c1..9099224 100644 --- a/util/util.go +++ b/util/util.go @@ -12,8 +12,10 @@ import ( "io/ioutil" "moyu/core" "net/http" + "sort" "strconv" "strings" + "time" ) const URL = "https://api.vvhan.com/api/moyu" @@ -27,8 +29,6 @@ func SendImage(myLove openwechat.Groups) { if err != nil { fmt.Println("Error while downloading", URL, ":", err) } - //format := time.Now().Format("2006年01月02日") - //err = myLove.SendText("兄弟们" + format + "早上好") if err != nil { return } @@ -43,6 +43,7 @@ func SendImage(myLove openwechat.Groups) { } }(response.Body) + fmt.Println("发送图片已执行") } func ReplyImage(myLove *openwechat.Message) { @@ -86,6 +87,81 @@ func SendTg(myLove *openwechat.Message) { }(response.Body) } +// SendHistoryToDay 历史上的今天 +func SendHistoryToDay(myLove *openwechat.Message) { + historyToDayUrl := "https://api.vvhan.com/api/hotlist?type=history" + response, err := http.Get(historyToDayUrl) + if err != nil { + return + } + defer response.Body.Close() + var result struct { + Data []struct { + Title string `json:"title"` + } `json:"data"` + } + err = json.NewDecoder(response.Body).Decode(&result) + if err != nil { + return + } + var titles []string + for _, item := range result.Data { + titles = append(titles, item.Title) + } + sort.Strings(titles) + sender, err := myLove.SenderInGroup() + reply := "@" + sender.NickName + "\n" + "====== 结果推送 ======\n" + for _, title := range titles { + reply += title + "\n" + } + _, err = myLove.ReplyText(reply) + if err != nil { + return + } +} + +// SendNewsToDay 每日60秒早报 +func SendNewsToDay(myLove openwechat.Groups) { + newsToDayUrl := "https://v2.alapi.cn/api/zaobao?token=D4tu77NBCO9sKCfO" + response, err := http.Get(newsToDayUrl) + if err != nil { + return + } + defer response.Body.Close() + var result struct { + Data struct { + Today string `json:"date"` + News []string `json:"news"` + WeiYu string `json:"weiyu"` + } `json:"data"` + } + err = json.NewDecoder(response.Body).Decode(&result) + if err != nil { + return + } + dateStr := result.Data.Today + t, err := time.Parse("2006-01-02", dateStr) + if err != nil { + fmt.Println("日期解析错误:", err) + return + } + formattedDate := t.Format("2006年01月02日") + var newses []string + for _, item := range result.Data.News { + newses = append(newses, item) + } + reply := "======== 每日推送 ========\n" + "早上好!今天是" + formattedDate + ">>>\n" + for _, news := range newses { + reply += news + "\n" + } + reply = reply + result.Data.WeiYu + err = myLove.SendText(reply) + if err != nil { + return + } + fmt.Println("发送早报已执行") +} + func SendAi(str string, myLove *openwechat.Message) { url := "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions_pro?access_token=" + GetAccessToken() payload := strings.NewReader(`{"messages":[{"role":"user","content":"` + str + `"}]}`) diff --git a/wechat/msg.go b/wechat/msg.go index ca547c2..db16382 100644 --- a/wechat/msg.go +++ b/wechat/msg.go @@ -1,6 +1,7 @@ package wechat import ( + "fmt" "github.com/eatmoreapple/openwechat" "moyu/core" ) @@ -20,12 +21,22 @@ func init() { // 获取登陆的用户 self, err := Bot.GetCurrentUser() if err != nil { + core.GetLogger().Error(err) return } NickName = self.NickName // 获取所有的群组 groups, err := self.Groups() + if err != nil { + core.GetLogger().Error(err) + return + } My302 = groups.SearchByNickName(1, "3-202") + // 检查搜索结果是否为空 + if len(My302) == 0 { + panic("Group not found") + } + fmt.Println("群组名称:", My302[0].NickName) } func ConsoleQrCode(uuid string) { url := "https://login.weixin.qq.com/qrcode/" + uuid -- Gitee From f119bc128c6354305edbeadffcdc7b2f143c424f Mon Sep 17 00:00:00 2001 From: LJN <1213911320@qq.com> Date: Fri, 22 Dec 2023 05:37:04 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=9B=B4=E6=8D=A2=E5=A4=B1=E6=95=88?= =?UTF-8?q?=E7=9A=84=E7=9A=84=E6=91=B8=E9=B1=BC=E4=BA=BA=E6=97=A5=E5=8E=86?= =?UTF-8?q?=E3=80=81=E5=8E=86=E5=8F=B2=E4=B8=8A=E7=9A=84=E4=BB=8A=E5=A4=A9?= =?UTF-8?q?=E7=9A=84API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 2 +- util/util.go | 207 +++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 179 insertions(+), 30 deletions(-) diff --git a/main.go b/main.go index 7a03a1e..b88a498 100644 --- a/main.go +++ b/main.go @@ -59,7 +59,7 @@ func main() { } } c := cron.New() - _, err := c.AddFunc("0 8 * * *", func() { + _, err := c.AddFunc("31 5 * * *", func() { util.SendImage(wechat.My302) util.SendNewsToDay(wechat.My302) fmt.Println("定时任务已执行") diff --git a/util/util.go b/util/util.go index 9099224..792c29c 100644 --- a/util/util.go +++ b/util/util.go @@ -12,7 +12,6 @@ import ( "io/ioutil" "moyu/core" "net/http" - "sort" "strconv" "strings" "time" @@ -24,15 +23,76 @@ const SECRET_KEY = "tiYeNeUgGLmBG2bT4y7YI7slTD0k96qF" var msg = make([]openai.ChatCompletionMessage, 0, 10) +// SendImage 定时任务图片发送(老) +// +// func SendImage(myLove openwechat.Groups) { +// response, err := http.Get(URL) +// if err != nil { +// fmt.Println("Error while downloading", URL, ":", err) +// } +// if err != nil { +// return +// } +// err = myLove.SendImage(response.Body) +// if err != nil { +// fmt.Println(err) +// return +// } +// defer func(Body io.ReadCloser) { +// err := Body.Close() +// if err != nil { +// +// } +// }(response.Body) +// fmt.Println("发送图片已执行") +// } +// +// SendImage 定时任务图片发送(新) func SendImage(myLove openwechat.Groups) { - response, err := http.Get(URL) + moyuUrl := "https://j4u.ink/moyuya" + response, err := http.Get(moyuUrl) if err != nil { - fmt.Println("Error while downloading", URL, ":", err) + fmt.Println("Error while downloading", moyuUrl, ":", err) + return + } + defer response.Body.Close() + body, err := ioutil.ReadAll(response.Body) + if err != nil { + fmt.Println("Error while reading response body:", err) + return + } + var resp struct { + Data struct { + MoyuURL string `json:"moyu_url"` + } `json:"data"` + } + err = json.Unmarshal(body, &resp) + if err != nil { + fmt.Println("Error while unmarshaling response:", err) + return + } + fmt.Println(resp.Data.MoyuURL) + var sjurl = "" + client := &http.Client{ + CheckRedirect: func(req *http.Request, via []*http.Request) error { + // 获取重定向的具体地址 + sjurl = req.URL.String() + return nil + }, + } + sjResponse, err := client.Get(resp.Data.MoyuURL) + if err != nil { + fmt.Println("Error while downloading image:", err) + return } + defer sjResponse.Body.Close() + imgResponse, err := http.Get(sjurl) if err != nil { + fmt.Println("Error while downloading image:", err) return } - err = myLove.SendImage(response.Body) + defer imgResponse.Body.Close() + err = myLove.SendImage(imgResponse.Body) if err != nil { fmt.Println(err) return @@ -40,18 +100,78 @@ func SendImage(myLove openwechat.Groups) { defer func(Body io.ReadCloser) { err := Body.Close() if err != nil { - + fmt.Println("Error while closing image body:", err) } - }(response.Body) + }(imgResponse.Body) fmt.Println("发送图片已执行") } +// ReplyImage 摸鱼人日历(旧API) +// +// func ReplyImage(myLove *openwechat.Message) { +// response, err := http.Get(URL) +// if err != nil { +// fmt.Println("Error while downloading", URL, ":", err) +// } +// _, err = myLove.ReplyImage(response.Body) +// if err != nil { +// fmt.Println(err) +// return +// } +// defer func(Body io.ReadCloser) { +// err := Body.Close() +// if err != nil { +// +// } +// }(response.Body) +// } +// +// ReplyImage 摸鱼人日历(新API) func ReplyImage(myLove *openwechat.Message) { - response, err := http.Get(URL) + moyuUrl := "https://j4u.ink/moyuya" + response, err := http.Get(moyuUrl) + if err != nil { + fmt.Println("Error while downloading", moyuUrl, ":", err) + return + } + defer response.Body.Close() + body, err := ioutil.ReadAll(response.Body) if err != nil { - fmt.Println("Error while downloading", URL, ":", err) + fmt.Println("Error while reading response body:", err) + return } - _, err = myLove.ReplyImage(response.Body) + var resp struct { + Data struct { + MoyuURL string `json:"moyu_url"` + } `json:"data"` + } + err = json.Unmarshal(body, &resp) + if err != nil { + fmt.Println("Error while unmarshaling response:", err) + return + } + fmt.Println(resp.Data.MoyuURL) + var sjurl = "" + client := &http.Client{ + CheckRedirect: func(req *http.Request, via []*http.Request) error { + // 获取重定向的具体地址 + sjurl = req.URL.String() + return nil + }, + } + sjResponse, err := client.Get(resp.Data.MoyuURL) + if err != nil { + fmt.Println("Error while downloading image:", err) + return + } + defer sjResponse.Body.Close() + imgResponse, err := http.Get(sjurl) + if err != nil { + fmt.Println("Error while downloading image:", err) + return + } + defer imgResponse.Body.Close() + _, err = myLove.ReplyImage(imgResponse.Body) if err != nil { fmt.Println(err) return @@ -59,9 +179,9 @@ func ReplyImage(myLove *openwechat.Message) { defer func(Body io.ReadCloser) { err := Body.Close() if err != nil { - + fmt.Println("Error while closing image body:", err) } - }(response.Body) + }(imgResponse.Body) } func SendTg(myLove *openwechat.Message) { @@ -87,39 +207,68 @@ func SendTg(myLove *openwechat.Message) { }(response.Body) } -// SendHistoryToDay 历史上的今天 +// SendHistoryToDay 历史上的今天(新API) func SendHistoryToDay(myLove *openwechat.Message) { - historyToDayUrl := "https://api.vvhan.com/api/hotlist?type=history" + historyToDayUrl := "https://www.ipip5.com/today/api.php?type=txt" response, err := http.Get(historyToDayUrl) if err != nil { return } - defer response.Body.Close() - var result struct { - Data []struct { - Title string `json:"title"` - } `json:"data"` - } - err = json.NewDecoder(response.Body).Decode(&result) + body, err := io.ReadAll(response.Body) if err != nil { return } - var titles []string - for _, item := range result.Data { - titles = append(titles, item.Title) - } - sort.Strings(titles) + lines := strings.Split(string(body), "\n") // 将文本按行分割成字符串切片 + excludedLines := lines[:len(lines)-1] // 去除最后一行 + result := strings.Join(excludedLines, "\n") // 将去除最后一行后的内容重新拼接成字符串 sender, err := myLove.SenderInGroup() - reply := "@" + sender.NickName + "\n" + "====== 结果推送 ======\n" - for _, title := range titles { - reply += title + "\n" - } + reply := "@" + sender.NickName + "\n" + "====== 结果推送 ======\n" + result _, err = myLove.ReplyText(reply) if err != nil { + fmt.Println(err) return } + defer func(Body io.ReadCloser) { + err := Body.Close() + if err != nil { + + } + }(response.Body) } +// SendHistoryToDay 历史上的今天(旧API) +//func SendHistoryToDay(myLove *openwechat.Message) { +// historyToDayUrl := "https://api.vvhan.com/api/hotlist?type=history" +// response, err := http.Get(historyToDayUrl) +// if err != nil { +// return +// } +// defer response.Body.Close() +// var result struct { +// Data []struct { +// Title string `json:"title"` +// } `json:"data"` +// } +// err = json.NewDecoder(response.Body).Decode(&result) +// if err != nil { +// return +// } +// var titles []string +// for _, item := range result.Data { +// titles = append(titles, item.Title) +// } +// sort.Strings(titles) +// sender, err := myLove.SenderInGroup() +// reply := "@" + sender.NickName + "\n" + "====== 结果推送 ======\n" +// for _, title := range titles { +// reply += title + "\n" +// } +// _, err = myLove.ReplyText(reply) +// if err != nil { +// return +// } +//} + // SendNewsToDay 每日60秒早报 func SendNewsToDay(myLove openwechat.Groups) { newsToDayUrl := "https://v2.alapi.cn/api/zaobao?token=D4tu77NBCO9sKCfO" -- Gitee From 2b945b51c4da10bc8fe0d33df8ce585b169d4707 Mon Sep 17 00:00:00 2001 From: LJN <1213911320@qq.com> Date: Fri, 22 Dec 2023 05:39:10 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=AE=9A=E6=97=B6?= =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index b88a498..7a03a1e 100644 --- a/main.go +++ b/main.go @@ -59,7 +59,7 @@ func main() { } } c := cron.New() - _, err := c.AddFunc("31 5 * * *", func() { + _, err := c.AddFunc("0 8 * * *", func() { util.SendImage(wechat.My302) util.SendNewsToDay(wechat.My302) fmt.Println("定时任务已执行") -- Gitee