main.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. }
  21. }
  22. }
  23. }
  24. func main() {
  25. err := godotenv.Load()
  26. if err != nil {
  27. log.Fatal("Error loading .env file")
  28. }
  29. connBot := api.WebsocketBot(os.Getenv("HOST"), os.Getenv("PORT"), os.Getenv("PATH"))
  30. defer connBot.Wsconn.Close()
  31. for {
  32. _, messageBytes, err := connBot.Wsconn.ReadMessage()
  33. if err != nil {
  34. log.Println("read:", err)
  35. return
  36. }
  37. var message map[string]interface{}
  38. json.Unmarshal(messageBytes, &message)
  39. if message["post_type"] == "message" {
  40. go msgProccess(message, connBot)
  41. } else if message["post_type"] == "meta_event" {
  42. }
  43. }
  44. // connBot.Send_private_msg(2945340446, "Hello, world!")
  45. // err = connBot.Upload_private_file(2945340446, "/home/jam/Documents/College/大学学习/大创/基于gm_Id方法的跨阻放大器设计_林佳辉.pdf", "基于gm_Id方法的跨阻放大器设计_林佳辉.pdf")
  46. // fmt.Println(err)
  47. }