main.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. var TTS_id int = 0
  12. func msgProccess(msg map[string]interface{}, connBot api.WsBot) {
  13. // fmt.Println(msg)
  14. if msg["message_type"].(string) == "private" {
  15. user_id := int(msg["user_id"].(float64))
  16. msg_type := msg["message"].([]interface{})[0].(map[string]interface{})["type"].(string)
  17. if msg_type == "text" {
  18. msg_text := msg["message"].([]interface{})[0].(map[string]interface{})["data"].(map[string]interface{})["text"].(string)
  19. if strings.HasPrefix(msg_text, "/bvdl ") {
  20. function.Func_bvdl(msg_text, connBot, user_id)
  21. } else if msg_text == "/serverStatus" {
  22. function.Func_serverStatus(connBot, user_id)
  23. } else if strings.HasPrefix(msg_text, "/tts ") {
  24. TTS_id++
  25. function.Func_TTS(connBot, user_id, msg_text, false, TTS_id-1)
  26. TTS_id = TTS_id % 10
  27. }
  28. }
  29. } else if msg["message_type"].(string) == "group" {
  30. group_id := int(msg["group_id"].(float64))
  31. msg_type := msg["message"].([]interface{})[0].(map[string]interface{})["type"].(string)
  32. if msg_type == "text" {
  33. msg_text := msg["message"].([]interface{})[0].(map[string]interface{})["data"].(map[string]interface{})["text"].(string)
  34. if strings.HasPrefix(msg_text, "/tts ") {
  35. TTS_id++
  36. function.Func_TTS(connBot, group_id, msg_text, true, TTS_id-1)
  37. TTS_id = TTS_id % 10
  38. }
  39. }
  40. }
  41. }
  42. func main() {
  43. err := godotenv.Load()
  44. if err != nil {
  45. log.Fatal("Error loading .env file")
  46. }
  47. connBot := api.WebsocketBot(os.Getenv("HOST"), os.Getenv("PORT"), os.Getenv("PATH"))
  48. defer connBot.Wsconn.Close()
  49. for {
  50. _, messageBytes, err := connBot.Wsconn.ReadMessage()
  51. if err != nil {
  52. log.Println("read:", err)
  53. return
  54. }
  55. var message map[string]interface{}
  56. json.Unmarshal(messageBytes, &message)
  57. if message["post_type"] == "message" {
  58. go msgProccess(message, connBot)
  59. } else if message["post_type"] == "meta_event" {
  60. }
  61. }
  62. // connBot.Send_private_msg(2945340446, "Hello, world!")
  63. // err = connBot.Upload_private_file(2945340446, "/home/jam/Documents/College/大学学习/大创/基于gm_Id方法的跨阻放大器设计_林佳辉.pdf", "基于gm_Id方法的跨阻放大器设计_林佳辉.pdf")
  64. // fmt.Println(err)
  65. }