-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
155 lines (138 loc) · 3.31 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package main
import (
"bytes"
"encoding/csv"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"runtime"
"strings"
"time"
)
const filename = "data.csv"
var cstZone = time.FixedZone("UTC", 8*3600) // 东八
var token string
type dingMsg struct {
MsgType string `json:"msgtype"`
Text struct {
Content string `json:"content"`
} `json:"text"`
At struct {
IsAtAll int `json:"isAtAll"`
} `json:"at"`
}
func log(message interface{}) {
pc, file, line, _ := runtime.Caller(1)
f := runtime.FuncForPC(pc).Name()
now := time.Now().In(cstZone).Format("15:04:05")
t := time.Now().In(cstZone)
str := fmt.Sprintf("%s %s:%d [%s]: %v", now, file, line, f, message)
fmt.Println(str)
fname := fmt.Sprintf("ding_eat_%d-%02d-%02d.log", t.Year(), t.Month(), t.Day())
logfile, err := os.OpenFile(fname, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
defer logfile.Close()
if err != nil {
fmt.Printf("%s %s:%d [%s]: [日志创建错误] %v\r\n", now, file, line, f, err)
}
logfile.WriteString(str + "\r\n")
}
func get_weekday(name string) string {
var weekday string
switch name {
case "Monday":
weekday = "周一"
case "Tuesday":
weekday = "周二"
case "Wednesday":
weekday = "周三"
case "Thursday":
weekday = "周四"
case "Friday":
weekday = "周五"
case "Saturday":
weekday = "周六"
case "Sunday":
weekday = "周日"
}
return weekday
}
func get_message(name string) (string, string) {
file, err := ioutil.ReadFile(filename)
if err != nil {
log(fmt.Sprintf("文件解析失败:%s", err))
}
text := csv.NewReader(strings.NewReader(string(file)))
lines, _ := text.ReadAll()
var weekday, msg string
for i := 0; i < len(lines); i++ {
if lines[i][0] == name {
if len(lines[i]) == 2 {
weekday, msg = lines[i][0], lines[i][1]
} else {
log(fmt.Sprintf("配置文件有误"))
os.Exit(1)
}
}
}
if weekday == "" || msg == "" {
log(fmt.Sprintf("当日无需发送"))
os.Exit(1)
}
return weekday, msg
}
func make_msg(weekday, msg string) string {
var message = ""
hour, min, _ := time.Now().In(cstZone).Clock()
now_time := "上午"
now_min_sec := fmt.Sprintf("%d:%d", hour, min)
if hour > 12 {
now_time = "下午"
}
message = fmt.Sprintf("现在是 %s %s %s啦, %s", weekday, now_time, now_min_sec, msg)
log(fmt.Sprintf("消息已生成:%s", message))
return message
}
func send_msg(msg string) {
var dingmsg dingMsg
dingmsg.At.IsAtAll = 1
dingmsg.MsgType = "text"
dingmsg.Text.Content = msg
JsonMsg, err := json.Marshal(dingmsg)
log(fmt.Sprintf("json已生成:%s", JsonMsg))
if err != nil {
log(err)
os.Exit(1)
}
body := bytes.NewBuffer([]byte(JsonMsg))
url := fmt.Sprintf("https://oapi.dingtalk.com/robot/send?access_token=%s", token)
res, err := http.Post(url, "application/json;charset=utf-8", body)
if err != nil {
log(err)
os.Exit(1)
return
}
result, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
log(err)
os.Exit(1)
return
}
log(fmt.Sprintf("接口返回信息:%s", result))
return
}
func init() {
flag.StringVar(&token, "t", "", "token when u create a robot in dingtalk")
flag.Parse()
if token == "" {
log("无token输入")
os.Exit(1)
}
log(fmt.Sprintf("初始化完成,获取token为:%s", token))
}
func main() {
send_msg(make_msg(get_message(get_weekday(time.Now().In(cstZone).Weekday().String()))))
}