Bladeren bron

接入bilimusic,未完成

JamZYM 8 maanden geleden
bovenliggende
commit
ea812c5b3c
4 gewijzigde bestanden met toevoegingen van 69 en 22 verwijderingen
  1. 3 3
      api/bot.go
  2. 2 2
      api/file.go
  3. 3 3
      api/send.go
  4. 61 14
      main.go

+ 3 - 3
api/bot.go

@@ -8,17 +8,17 @@ import (
 	"github.com/gorilla/websocket"
 )
 
-type wsBot struct {
+type WsBot struct {
 	Wsconn *websocket.Conn
 }
 
-func WebsocketBot(host string, port string, path string) wsBot {
+func WebsocketBot(host string, port string, path string) WsBot {
 	wsurl := url.URL{Scheme: "ws", Host: host + ":" + port, Path: path}
 	conn, _, err := websocket.DefaultDialer.Dial(wsurl.String(), nil)
 	if err != nil {
 		log.Fatal("Failed to connect to WebSocket server:", err)
 	}
-	return wsBot{conn}
+	return WsBot{conn}
 }
 
 func Test() {

+ 2 - 2
api/file.go

@@ -6,7 +6,7 @@ import (
 	"github.com/gorilla/websocket"
 )
 
-func (conn *wsBot) Upload_group_file(group_id int, file string, name string, folder string) error {
+func (conn *WsBot) Upload_group_file(group_id int, file string, name string, folder string) error {
 	err := conn.Wsconn.WriteMessage(websocket.TextMessage,
 		[]byte(fmt.Sprintf(`
 		{
@@ -22,7 +22,7 @@ func (conn *wsBot) Upload_group_file(group_id int, file string, name string, fol
 	return err
 }
 
-func (conn *wsBot) Upload_private_file(user_id int, file string, name string) error {
+func (conn *WsBot) Upload_private_file(user_id int, file string, name string) error {
 	err := conn.Wsconn.WriteMessage(websocket.TextMessage,
 		[]byte(fmt.Sprintf(`
 		{

+ 3 - 3
api/send.go

@@ -6,7 +6,7 @@ import (
 	"github.com/gorilla/websocket"
 )
 
-func (conn *wsBot) Send_private_msg(user_id int, message string) error {
+func (conn *WsBot) Send_private_msg(user_id int, message string) error {
 	err := conn.Wsconn.WriteMessage(websocket.TextMessage,
 		[]byte(fmt.Sprintf(`
 		{
@@ -20,7 +20,7 @@ func (conn *wsBot) Send_private_msg(user_id int, message string) error {
 	return err
 }
 
-func (conn *wsBot) Send_group_msg(group_id int, message string) error {
+func (conn *WsBot) Send_group_msg(group_id int, message string) error {
 	err := conn.Wsconn.WriteMessage(websocket.TextMessage,
 		[]byte(fmt.Sprintf(`
 		{
@@ -34,7 +34,7 @@ func (conn *wsBot) Send_group_msg(group_id int, message string) error {
 	return err
 }
 
-func (conn *wsBot) Send_like(user_id int, times int) error {
+func (conn *WsBot) Send_like(user_id int, times int) error {
 	err := conn.Wsconn.WriteMessage(websocket.TextMessage,
 		[]byte(fmt.Sprintf(`
 		{

+ 61 - 14
main.go

@@ -1,15 +1,60 @@
 package main
 
 import (
+	"bytes"
+	"encoding/json"
 	"fmt"
+	"io"
 	"log"
+	"net/http"
 	"os"
-	"time"
+	"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))))
+				if err != nil {
+					connBot.Send_private_msg(user_id, "请求失败")
+					fmt.Println(err)
+				} else {
+					// defer info.Body.Close()
+					data, _ := io.ReadAll(info.Body)
+					fmt.Println(string(data))
+					result, err := http.Post(os.Getenv("HOST")+":"+os.Getenv("BVDLPORT")+"/api/download", "application/json",
+						bytes.NewBuffer([]byte(fmt.Sprintf(`
+					{
+						"bv":%s;
+						"title":"%s"
+					}
+					`, bv, "title"))))
+					if err != nil {
+						connBot.Send_private_msg(user_id, "下载失败")
+					}
+					fmt.Print(result)
+				}
+			}
+		}
+	}
+}
+
 func main() {
 	err := godotenv.Load()
 	if err != nil {
@@ -19,21 +64,23 @@ func main() {
 	connBot := api.WebsocketBot(os.Getenv("HOST"), os.Getenv("PORT"), os.Getenv("PATH"))
 	defer connBot.Wsconn.Close()
 
-	go func() {
-		for {
-			_, message, err := connBot.Wsconn.ReadMessage()
-			if err != nil {
-				log.Println("read:", err)
-				return
-			}
-			log.Printf("recv: %s", message)
+	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)
+		}
+	}
 
-	time.Sleep(10 * time.Second)
+	// 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)
 
 }