forked from wxkeykey/HiFiNi-Auto-Sign
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
111 lines (94 loc) · 2.02 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"time"
)
const (
SignInURL = "https://www.hifini.com/sg_sign.htm"
CookieEnvVariable = "COOKIE"
DingDingWebhook = "DINGDING_WEBHOOK"
)
type DingDingMessage struct {
MsgType string `json:"msgtype"`
Text struct {
Content string `json:"content"`
} `json:"text"`
}
func main() {
client := &http.Client{
Timeout: 10 * time.Second,
}
success := SignIn(client)
if success {
result := "签到成功"
fmt.Println(result)
dingding(result)
} else {
result := "签到失败"
fmt.Println(result)
dingding(result)
os.Exit(3)
}
}
// SignIn 签到
func SignIn(client *http.Client) bool {
cookie := os.Getenv(CookieEnvVariable)
if cookie == "" {
log.Println("COOKIE不存在,请检查是否添加")
return false
}
req, err := http.NewRequest("POST", SignInURL, nil)
if err != nil {
log.Println("创建请求失败:", err)
return false
}
req.Header.Set("Cookie", cookie)
req.Header.Set("x-requested-with", "XMLHttpRequest")
resp, err := client.Do(req)
if err != nil {
log.Println("发送请求失败:", err)
return false
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println("读取响应失败:", err)
return false
}
log.Println(string(body))
return strings.Contains(string(body), "成功")
}
func dingding(result string) {
message := DingDingMessage{
MsgType: "text",
Text: struct {
Content string `json:"content"`
}{
Content: "HiFiNi" + result,
},
}
messageJson, err := json.Marshal(message)
if err != nil {
log.Println("转换消息为JSON失败:", err)
return
}
webhook := os.Getenv(DingDingWebhook)
if webhook == "" {
log.Println("DINGDING_WEBHOOK不存在,请检查是否添加")
return
}
resp, err := http.Post(webhook, "application/json", bytes.NewBuffer(messageJson))
if err != nil {
log.Println("发送消息失败:", err)
return
}
defer resp.Body.Close()
log.Println("消息发送成功")
}