main.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io"
  6. "log"
  7. "net/http"
  8. "os"
  9. "strings"
  10. "github.com/JamZYM/golagrange/api"
  11. "github.com/joho/godotenv"
  12. )
  13. func msgProccess(msg map[string]interface{}, connBot api.WsBot) {
  14. // fmt.Println(msg)
  15. if msg["message_type"].(string) == "private" {
  16. user_id := int(msg["user_id"].(float64))
  17. // println(user_id)
  18. // fmt.Println(msg["message"].([]interface{})[0].(map[string]interface{})["type"].(string))
  19. if msg["message"].([]interface{})[0].(map[string]interface{})["type"].(string) == "text" {
  20. if strings.HasPrefix(msg["message"].([]interface{})[0].(map[string]interface{})["data"].(map[string]interface{})["text"].(string), "/bvdl ") {
  21. bv := strings.Replace(msg["message"].([]interface{})[0].(map[string]interface{})["data"].(map[string]interface{})["text"].(string), "/bvdl ", "", 1)
  22. // println(bv)
  23. // println(os.Getenv("HOST") + ":" + os.Getenv("BVDLPORT") + "/api/info")
  24. // info, err := http.Post("http://"+os.Getenv("HOST")+":"+os.Getenv("BVDLPORT")+"/api/info", "application/json",
  25. // bytes.NewBuffer([]byte(fmt.Sprintf(`
  26. // {
  27. // "bv":%s
  28. // }
  29. // `, bv))))
  30. info, err := http.Post("http://"+os.Getenv("HOST")+":"+os.Getenv("BVDLPORT")+"/api/info?bv="+bv, "application/json", nil)
  31. if err != nil {
  32. connBot.Send_private_msg(user_id, "请求失败")
  33. fmt.Println(err)
  34. } else {
  35. defer info.Body.Close()
  36. var jsondata map[string]interface{}
  37. data, _ := io.ReadAll(info.Body)
  38. json.Unmarshal(data, &jsondata)
  39. // fmt.Println(string(data))
  40. title := jsondata["videos"].(map[string]interface{})["1"].(map[string]interface{})["title"].(string)
  41. // result, err := http.Post("http://"+os.Getenv("HOST")+":"+os.Getenv("BVDLPORT")+"/api/download", "application/json",
  42. // bytes.NewBuffer([]byte(fmt.Sprintf(`
  43. // {
  44. // "bv":%s;
  45. // "title":"%s"
  46. // }
  47. // `, bv, title))))
  48. fmt.Println("http://" + os.Getenv("HOST") + ":" + os.Getenv("BVDLPORT") + "/api/download?bv=" + bv + "&title=" + title)
  49. // result, err := http.PostForm("http://"+os.Getenv("HOST")+":"+os.Getenv("BVDLPORT")+"/api/download?bv="+bv+"&title="+title, nil)
  50. url := "http://" + os.Getenv("HOST") + ":" + os.Getenv("BVDLPORT") + "/api/download?bv=" + bv + "&title=" + title
  51. result, err := http.Get(url)
  52. // url := "http://" + os.Getenv("HOST") + ":" + os.Getenv("BVDLPORT") + "/api/download"
  53. // body := fmt.Sprintf(`{"bv":"%s","title":"%s"}`, bv, title)
  54. // result, err := http.Post(url, "application/json", strings.NewReader(body))
  55. if err != nil {
  56. connBot.Send_private_msg(user_id, "下载失败")
  57. fmt.Println(err)
  58. } else {
  59. fmt.Print(result)
  60. connBot.Send_private_msg(user_id, title)
  61. }
  62. }
  63. }
  64. }
  65. }
  66. }
  67. func main() {
  68. err := godotenv.Load()
  69. if err != nil {
  70. log.Fatal("Error loading .env file")
  71. }
  72. connBot := api.WebsocketBot(os.Getenv("HOST"), os.Getenv("PORT"), os.Getenv("PATH"))
  73. defer connBot.Wsconn.Close()
  74. for {
  75. _, messageBytes, err := connBot.Wsconn.ReadMessage()
  76. if err != nil {
  77. log.Println("read:", err)
  78. return
  79. }
  80. var message map[string]interface{}
  81. json.Unmarshal(messageBytes, &message)
  82. if message["post_type"] == "message" {
  83. go msgProccess(message, connBot)
  84. } else if message["post_type"] == "meta_event" {
  85. }
  86. }
  87. // connBot.Send_private_msg(2945340446, "Hello, world!")
  88. // err = connBot.Upload_private_file(2945340446, "/home/jam/Documents/College/大学学习/大创/基于gm_Id方法的跨阻放大器设计_林佳辉.pdf", "基于gm_Id方法的跨阻放大器设计_林佳辉.pdf")
  89. // fmt.Println(err)
  90. }