forked from yqchilde/wxbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
75 lines (66 loc) · 1.66 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package moyuban
import (
"net/http"
"github.com/imroc/req/v3"
"github.com/yqchilde/wxbot/engine/control"
"github.com/yqchilde/wxbot/engine/robot"
)
func init() {
engine := control.Register("moyu", &control.Options{
Alias: "摸鱼日历",
Help: "输入 {摸鱼日历|摸鱼} => 获取摸鱼办日历",
})
engine.OnFullMatchGroup([]string{"摸鱼日历", "摸鱼"}).SetBlock(true).Handle(func(ctx *robot.Ctx) {
if imageUrl := getMoYuBan(); imageUrl == "" {
ctx.ReplyTextAndAt("获取摸鱼办日历失败")
} else {
ctx.ReplyImage(imageUrl)
}
})
}
type MoYu1Resp struct {
Success bool `json:"success"`
Url string `json:"url"`
}
func getMoYuBan() string {
resp := req.C().Get("https://api.vvhan.com/api/moyu?type=json").Do()
if resp.StatusCode != 200 {
return getMoYuBan2()
}
var respData MoYu1Resp
if err := resp.Into(&respData); err != nil {
return getMoYuBan2()
}
if respData.Url == "" {
return getMoYuBan2()
}
return respData.Url
}
type MoYu2Resp struct {
Code int `json:"code"`
Message string `json:"message"`
Data struct {
MoyuUrl string `json:"moyu_url"`
} `json:"data"`
}
func getMoYuBan2() string {
client := req.C()
resp := client.Get("https://api.j4u.ink/v1/store/other/proxy/remote/moyu.json").Do()
if resp.StatusCode != 200 {
return ""
}
var respData MoYu2Resp
if err := resp.Into(&respData); err != nil {
return ""
}
var imageUrl string
resp = client.SetRedirectPolicy(func(req *http.Request, via []*http.Request) error {
location, err := req.Response.Location()
if err != nil {
return err
}
imageUrl = location.String()
return nil
}).Get(respData.Data.MoyuUrl).Do()
return imageUrl
}