main.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package main
  2. import (
  3. "encoding/json"
  4. "log"
  5. "os"
  6. "strings"
  7. "github.com/JamZYM/golagrange/api"
  8. "github.com/JamZYM/golagrange/function"
  9. "github.com/joho/godotenv"
  10. )
  11. func msgProccess(msg map[string]interface{}, connBot api.WsBot) {
  12. // fmt.Println(msg)
  13. if msg["message_type"].(string) == "private" {
  14. user_id := int(msg["user_id"].(float64))
  15. msg_type := msg["message"].([]interface{})[0].(map[string]interface{})["type"].(string)
  16. if msg_type == "text" {
  17. msg_text := msg["message"].([]interface{})[0].(map[string]interface{})["data"].(map[string]interface{})["text"].(string)
  18. if strings.HasPrefix(msg_text, "/bvdl ") {
  19. function.Func_bvdl(msg_text, connBot, user_id)
  20. } else if msg_text == "/serverStatus" {
  21. function.Func_serverStatus(connBot, user_id)
  22. }
  23. }
  24. }
  25. }
  26. func main() {
  27. err := godotenv.Load()
  28. if err != nil {
  29. log.Fatal("Error loading .env file")
  30. }
  31. connBot := api.WebsocketBot(os.Getenv("HOST"), os.Getenv("PORT"), os.Getenv("PATH"))
  32. defer connBot.Wsconn.Close()
  33. for {
  34. _, messageBytes, err := connBot.Wsconn.ReadMessage()
  35. if err != nil {
  36. log.Println("read:", err)
  37. return
  38. }
  39. var message map[string]interface{}
  40. json.Unmarshal(messageBytes, &message)
  41. if message["post_type"] == "message" {
  42. go msgProccess(message, connBot)
  43. } else if message["post_type"] == "meta_event" {
  44. }
  45. }
  46. // connBot.Send_private_msg(2945340446, "Hello, world!")
  47. // err = connBot.Upload_private_file(2945340446, "/home/jam/Documents/College/大学学习/大创/基于gm_Id方法的跨阻放大器设计_林佳辉.pdf", "基于gm_Id方法的跨阻放大器设计_林佳辉.pdf")
  48. // fmt.Println(err)
  49. }