Browse Source

完成基本几个常用api的实现,加入.env环境,文件上传部分尚未调试成功

JamZYM 8 months ago
parent
commit
75cfeec411
7 changed files with 136 additions and 15 deletions
  1. 1 0
      .gitignore
  2. 12 3
      api/bot.go
  3. 38 0
      api/file.go
  4. 49 0
      api/send.go
  5. 4 1
      go.mod
  6. 2 0
      go.sum
  7. 30 11
      main.go

+ 1 - 0
.gitignore

@@ -0,0 +1 @@
+.env

+ 12 - 3
api/bot.go

@@ -1,17 +1,26 @@
 package api
 
 import (
+	"fmt"
 	"log"
 	"net/url"
 
 	"github.com/gorilla/websocket"
 )
 
-func websocketBot(host string, port int, path string) *websocket.Conn {
-	wsurl := url.URL{Scheme: "ws", Host: host + ":" + string(port), Path: path}
+type wsBot struct {
+	Wsconn *websocket.Conn
+}
+
+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 conn
+	return wsBot{conn}
+}
+
+func Test() {
+	fmt.Println("test")
 }

+ 38 - 0
api/file.go

@@ -0,0 +1,38 @@
+package api
+
+import (
+	"fmt"
+
+	"github.com/gorilla/websocket"
+)
+
+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(`
+		{
+			"action":"upload_group_file",
+			"params":{
+				"group_id":%d,
+				"file":"%s",
+				"name":"%s",
+				"folder":"%s"
+			}
+		}
+		`, group_id, file, name, folder)))
+	return err
+}
+
+func (conn *wsBot) Upload_private_file(user_id int, file string, name string) error {
+	err := conn.Wsconn.WriteMessage(websocket.TextMessage,
+		[]byte(fmt.Sprintf(`
+		{
+			"action":"upload_private_file",
+			"params":{
+				"user_id":%d,
+				"file":"%s",
+				"name":"%s",
+			}
+		}
+		`, user_id, file, name)))
+	return err
+}

+ 49 - 0
api/send.go

@@ -0,0 +1,49 @@
+package api
+
+import (
+	"fmt"
+
+	"github.com/gorilla/websocket"
+)
+
+func (conn *wsBot) Send_private_msg(user_id int, message string) error {
+	err := conn.Wsconn.WriteMessage(websocket.TextMessage,
+		[]byte(fmt.Sprintf(`
+		{
+			"action":"send_private_msg",
+			"params":{
+				"user_id":%d,
+				"message":"%s"
+			}
+		}
+		`, user_id, message)))
+	return err
+}
+
+func (conn *wsBot) Send_group_msg(group_id int, message string) error {
+	err := conn.Wsconn.WriteMessage(websocket.TextMessage,
+		[]byte(fmt.Sprintf(`
+		{
+			"action":"send_group_msg",
+			"params":{
+				"user_id":%d,
+				"message":"%s"
+			}
+		}
+		`, group_id, message)))
+	return err
+}
+
+func (conn *wsBot) Send_like(user_id int, times int) error {
+	err := conn.Wsconn.WriteMessage(websocket.TextMessage,
+		[]byte(fmt.Sprintf(`
+		{
+			"action":"send_like",
+			"params":{
+				"user_id":%d,
+				"times":"%d"
+			}
+		}
+		`, user_id, times)))
+	return err
+}

+ 4 - 1
go.mod

@@ -4,4 +4,7 @@ go 1.22.2
 
 require github.com/gorilla/websocket v1.5.1
 
-require golang.org/x/net v0.17.0 // indirect
+require (
+	github.com/joho/godotenv v1.5.1 // indirect
+	golang.org/x/net v0.17.0 // indirect
+)

+ 2 - 0
go.sum

@@ -1,4 +1,6 @@
 github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY=
 github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY=
+github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
+github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
 golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
 golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=

+ 30 - 11
main.go

@@ -1,20 +1,39 @@
 package main
 
 import (
+	"fmt"
 	"log"
+	"os"
+	"time"
+
+	"github.com/JamZYM/golagrange/api"
+	"github.com/joho/godotenv"
 )
 
 func main() {
-	conn := golagrange.api.websocketBot("localhost", 5101, "")
-	defer conn.Close()
-
-	for {
-		id, message, err := conn.ReadMessage()
-		if err != nil {
-			log.Println("read:", err)
-			return
-		}
-		log.Printf("recv: %s", id)
-		log.Printf("recv: %s", message)
+	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()
+
+	go func() {
+		for {
+			_, message, err := connBot.Wsconn.ReadMessage()
+			if err != nil {
+				log.Println("read:", err)
+				return
+			}
+			log.Printf("recv: %s", message)
+		}
+	}()
+
+	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)
+
 }