opstools.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package process
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/eryajf/chatgpt-dingtalk/pkg/db"
  6. "github.com/eryajf/chatgpt-dingtalk/pkg/dingbot"
  7. "github.com/eryajf/chatgpt-dingtalk/pkg/logger"
  8. "github.com/eryajf/chatgpt-dingtalk/pkg/ops"
  9. "github.com/eryajf/chatgpt-dingtalk/public"
  10. )
  11. // 一些运维方面的工具在此
  12. // 域名信息
  13. func DomainMsg(rmsg *dingbot.ReceiveMsg) error {
  14. qObj := db.Chat{
  15. Username: rmsg.SenderNick,
  16. Source: rmsg.GetChatTitle(),
  17. ChatType: db.Q,
  18. ParentContent: 0,
  19. Content: rmsg.Text.Content,
  20. }
  21. qid, err := qObj.Add()
  22. if err != nil {
  23. logger.Error("往MySQL新增数据失败,错误信息:", err)
  24. }
  25. domain := strings.TrimSpace(strings.Split(rmsg.Text.Content, " ")[1])
  26. dm, err := ops.GetDomainMsg(domain)
  27. if err != nil {
  28. return err
  29. }
  30. // 回复@我的用户
  31. reply := fmt.Sprintf("**创建时间:** %v\n\n**到期时间:** %v\n\n**服务商:** %v", dm.CreateDate, dm.ExpiryDate, dm.Registrar)
  32. aObj := db.Chat{
  33. Username: rmsg.SenderNick,
  34. Source: rmsg.GetChatTitle(),
  35. ChatType: db.A,
  36. ParentContent: qid,
  37. Content: reply,
  38. }
  39. _, err = aObj.Add()
  40. if err != nil {
  41. logger.Error("往MySQL新增数据失败,错误信息:", err)
  42. }
  43. logger.Info(fmt.Sprintf("🤖 %s得到的答案: %#v", rmsg.SenderNick, reply))
  44. _, err = rmsg.ReplyToDingtalk(string(dingbot.MARKDOWN), reply)
  45. if err != nil {
  46. logger.Error(fmt.Errorf("send message error: %v", err))
  47. return err
  48. }
  49. return nil
  50. }
  51. // 证书信息
  52. func DomainCertMsg(rmsg *dingbot.ReceiveMsg) error {
  53. qObj := db.Chat{
  54. Username: rmsg.SenderNick,
  55. Source: rmsg.GetChatTitle(),
  56. ChatType: db.Q,
  57. ParentContent: 0,
  58. Content: rmsg.Text.Content,
  59. }
  60. qid, err := qObj.Add()
  61. if err != nil {
  62. logger.Error("往MySQL新增数据失败,错误信息:", err)
  63. }
  64. domain := strings.TrimSpace(strings.Split(rmsg.Text.Content, " ")[1])
  65. dm, err := ops.GetDomainCertMsg(domain)
  66. if err != nil {
  67. return err
  68. }
  69. cert := dm.PeerCertificates[0]
  70. // 回复@我的用户
  71. reply := fmt.Sprintf("**证书创建时间:** %v\n\n**证书到期时间:** %v\n\n**证书颁发机构:** %v\n\n", public.GetReadTime(cert.NotBefore), public.GetReadTime(cert.NotAfter), cert.Issuer.Organization)
  72. aObj := db.Chat{
  73. Username: rmsg.SenderNick,
  74. Source: rmsg.GetChatTitle(),
  75. ChatType: db.A,
  76. ParentContent: qid,
  77. Content: reply,
  78. }
  79. _, err = aObj.Add()
  80. if err != nil {
  81. logger.Error("往MySQL新增数据失败,错误信息:", err)
  82. }
  83. logger.Info(fmt.Sprintf("🤖 %s得到的答案: %#v", rmsg.SenderNick, reply))
  84. _, err = rmsg.ReplyToDingtalk(string(dingbot.MARKDOWN), reply)
  85. if err != nil {
  86. logger.Error(fmt.Errorf("send message error: %v", err))
  87. return err
  88. }
  89. return nil
  90. }