|
@@ -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)
|
|
|
|
|
|
}
|