-
-
Notifications
You must be signed in to change notification settings - Fork 388
/
opstools.go
95 lines (90 loc) · 2.71 KB
/
opstools.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
package process
import (
"fmt"
"strings"
"github.com/eryajf/chatgpt-dingtalk/pkg/db"
"github.com/eryajf/chatgpt-dingtalk/pkg/dingbot"
"github.com/eryajf/chatgpt-dingtalk/pkg/logger"
"github.com/eryajf/chatgpt-dingtalk/pkg/ops"
"github.com/eryajf/chatgpt-dingtalk/public"
)
// 一些运维方面的工具在此
// 域名信息
func DomainMsg(rmsg *dingbot.ReceiveMsg) error {
qObj := db.Chat{
Username: rmsg.SenderNick,
Source: rmsg.GetChatTitle(),
ChatType: db.Q,
ParentContent: 0,
Content: rmsg.Text.Content,
}
qid, err := qObj.Add()
if err != nil {
logger.Error("往MySQL新增数据失败,错误信息:", err)
}
domain := strings.TrimSpace(strings.Split(rmsg.Text.Content, " ")[1])
dm, err := ops.GetDomainMsg(domain)
if err != nil {
return err
}
// 回复@我的用户
reply := fmt.Sprintf("**创建时间:** %v\n\n**到期时间:** %v\n\n**服务商:** %v", dm.CreateDate, dm.ExpiryDate, dm.Registrar)
aObj := db.Chat{
Username: rmsg.SenderNick,
Source: rmsg.GetChatTitle(),
ChatType: db.A,
ParentContent: qid,
Content: reply,
}
_, err = aObj.Add()
if err != nil {
logger.Error("往MySQL新增数据失败,错误信息:", err)
}
logger.Info(fmt.Sprintf("🤖 %s得到的答案: %#v", rmsg.SenderNick, reply))
_, err = rmsg.ReplyToDingtalk(string(dingbot.MARKDOWN), reply)
if err != nil {
logger.Error(fmt.Errorf("send message error: %v", err))
return err
}
return nil
}
// 证书信息
func DomainCertMsg(rmsg *dingbot.ReceiveMsg) error {
qObj := db.Chat{
Username: rmsg.SenderNick,
Source: rmsg.GetChatTitle(),
ChatType: db.Q,
ParentContent: 0,
Content: rmsg.Text.Content,
}
qid, err := qObj.Add()
if err != nil {
logger.Error("往MySQL新增数据失败,错误信息:", err)
}
domain := strings.TrimSpace(strings.Split(rmsg.Text.Content, " ")[1])
dm, err := ops.GetDomainCertMsg(domain)
if err != nil {
return err
}
cert := dm.PeerCertificates[0]
// 回复@我的用户
reply := fmt.Sprintf("**证书创建时间:** %v\n\n**证书到期时间:** %v\n\n**证书颁发机构:** %v\n\n", public.GetReadTime(cert.NotBefore), public.GetReadTime(cert.NotAfter), cert.Issuer.Organization)
aObj := db.Chat{
Username: rmsg.SenderNick,
Source: rmsg.GetChatTitle(),
ChatType: db.A,
ParentContent: qid,
Content: reply,
}
_, err = aObj.Add()
if err != nil {
logger.Error("往MySQL新增数据失败,错误信息:", err)
}
logger.Info(fmt.Sprintf("🤖 %s得到的答案: %#v", rmsg.SenderNick, reply))
_, err = rmsg.ReplyToDingtalk(string(dingbot.MARKDOWN), reply)
if err != nil {
logger.Error(fmt.Errorf("send message error: %v", err))
return err
}
return nil
}