123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- package main
- import (
- "encoding/json"
- "fmt"
- "io"
- "log"
- "net/http"
- "net/url"
- "os"
- "strings"
- "github.com/JamZYM/golagrange/api"
- "github.com/joho/godotenv"
- )
- func msgProccess(msg map[string]interface{}, connBot api.WsBot) {
- // fmt.Println(msg)
- if msg["message_type"].(string) == "private" {
- user_id := int(msg["user_id"].(float64))
- // println(user_id)
- // fmt.Println(msg["message"].([]interface{})[0].(map[string]interface{})["type"].(string))
- if msg["message"].([]interface{})[0].(map[string]interface{})["type"].(string) == "text" {
- if strings.HasPrefix(msg["message"].([]interface{})[0].(map[string]interface{})["data"].(map[string]interface{})["text"].(string), "/bvdl ") {
- bv := strings.Replace(msg["message"].([]interface{})[0].(map[string]interface{})["data"].(map[string]interface{})["text"].(string), "/bvdl ", "", 1)
- // println(bv)
- // println(os.Getenv("HOST") + ":" + os.Getenv("BVDLPORT") + "/api/info")
- // info, err := http.Post("http://"+os.Getenv("HOST")+":"+os.Getenv("BVDLPORT")+"/api/info", "application/json",
- // bytes.NewBuffer([]byte(fmt.Sprintf(`
- // {
- // "bv":%s
- // }
- // `, bv))))
- info, err := http.Post("http://"+os.Getenv("HOST")+":"+os.Getenv("BVDLPORT")+"/api/info?bv="+bv, "application/json", nil)
- if err != nil {
- connBot.Send_private_msg(user_id, "请求失败")
- fmt.Println(err)
- } else {
- defer info.Body.Close()
- var jsondata map[string]interface{}
- data, _ := io.ReadAll(info.Body)
- json.Unmarshal(data, &jsondata)
- // fmt.Println(string(data))
- title := jsondata["videos"].(map[string]interface{})["1"].(map[string]interface{})["title"].(string)
- // result, err := http.Post("http://"+os.Getenv("HOST")+":"+os.Getenv("BVDLPORT")+"/api/download", "application/json",
- // bytes.NewBuffer([]byte(fmt.Sprintf(`
- // {
- // "bv":%s;
- // "title":"%s"
- // }
- // `, bv, title))))
- fmt.Println("http://" + os.Getenv("HOST") + ":" + os.Getenv("BVDLPORT") + "/api/download?bv=" + bv + "&title=" + title)
- // result, err := http.PostForm("http://"+os.Getenv("HOST")+":"+os.Getenv("BVDLPORT")+"/api/download?bv="+bv+"&title="+title, nil)
- qbv := url.QueryEscape(bv)
- qtitle := url.QueryEscape(title)
- geturl := "http://" + os.Getenv("HOST") + ":" + os.Getenv("BVDLPORT") + "/api/download?bv=" + qbv + "&title=" + qtitle
- result, err := http.Get(geturl)
- // url := "http://" + os.Getenv("HOST") + ":" + os.Getenv("BVDLPORT") + "/api/download"
- // body := fmt.Sprintf(`{"bv":"%s","title":"%s"}`, bv, title)
- // result, err := http.Post(url, "application/json", strings.NewReader(body))
- if err != nil {
- connBot.Send_private_msg(user_id, "下载失败")
- fmt.Println(err)
- } else {
- fmt.Print(result.Body)
- connBot.Send_private_msg(user_id, title)
- }
- }
- }
- }
- }
- }
- func main() {
- err := godotenv.Load()
- if err != nil {
- log.Fatal("Error loading .env file")
- }
- connBot := api.WebsocketBot(os.Getenv("HOST"), os.Getenv("PORT"), os.Getenv("PATH"))
- defer connBot.Wsconn.Close()
- for {
- _, messageBytes, err := connBot.Wsconn.ReadMessage()
- if err != nil {
- log.Println("read:", err)
- return
- }
- var message map[string]interface{}
- json.Unmarshal(messageBytes, &message)
- if message["post_type"] == "message" {
- go msgProccess(message, connBot)
- } else if message["post_type"] == "meta_event" {
- }
- }
- // connBot.Send_private_msg(2945340446, "Hello, world!")
- // err = connBot.Upload_private_file(2945340446, "/home/jam/Documents/College/大学学习/大创/基于gm_Id方法的跨阻放大器设计_林佳辉.pdf", "基于gm_Id方法的跨阻放大器设计_林佳辉.pdf")
- // fmt.Println(err)
- }
|