telegram_groups.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package admin
  2. import (
  3. "app/commons/core"
  4. "app/telegram"
  5. "fmt"
  6. "github.com/gin-gonic/gin"
  7. tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
  8. "time"
  9. )
  10. // GetTelegramGroups 获取 Bot 加入的群组列表
  11. func (s *Server) GetTelegramGroups(ctx *gin.Context) {
  12. c := s.FromContext(ctx)
  13. // 检查 Bot 是否启用
  14. if !telegram.IsEnabled() {
  15. c.Fail("Telegram Bot 未启用")
  16. return
  17. }
  18. bot := telegram.GetBot()
  19. if bot == nil {
  20. c.Fail("无法获取 Bot 实例")
  21. return
  22. }
  23. // 获取群组列表
  24. // 注意: Telegram Bot API 没有直接获取所有群组的方法
  25. // 这里我们需要从 getUpdates 中提取群组信息
  26. // 或者从数据库中查询已记录的群组信息
  27. type GroupInfo struct {
  28. ChatId int64 `json:"chatId"`
  29. ChatType string `json:"chatType"`
  30. Title string `json:"title"`
  31. Username string `json:"username"`
  32. MemberCount int `json:"memberCount"`
  33. BotJoinedAt int64 `json:"botJoinedAt"`
  34. }
  35. // 这里简化处理,返回一个提示
  36. // 实际应该从数据库查询已记录的群组
  37. c.Resp(gin.H{
  38. "message": "请通过 Bot 添加到群组后,群组信息会自动记录",
  39. "groups": []GroupInfo{},
  40. })
  41. }
  42. // SyncTelegramGroups 同步 Bot 加入的群组到数据库
  43. // 从数据库获取已自动记录的群组信息
  44. func (s *Server) SyncTelegramGroups(ctx *gin.Context) {
  45. c := s.FromContext(ctx)
  46. // 检查 Bot 是否启用
  47. if !telegram.IsEnabled() {
  48. c.Fail("Telegram Bot 未启用")
  49. return
  50. }
  51. // 从数据库获取已自动记录的群组
  52. dbGroups, err := telegram.GetAllGroupsFromDB()
  53. if err != nil {
  54. core.Log.Errorf("从数据库获取群组失败: %v", err)
  55. c.Fail(fmt.Sprintf("获取群组失败: %v", err))
  56. return
  57. }
  58. // 转换为 GroupData 格式
  59. groups := make([]GroupData, 0, len(dbGroups))
  60. for _, g := range dbGroups {
  61. groups = append(groups, GroupData{
  62. ChatId: g.ChatId,
  63. ChatType: g.ChatType,
  64. Title: g.Title,
  65. Username: g.Username,
  66. Description: g.Description,
  67. MemberCount: g.MemberCount,
  68. BotJoinedAt: g.BotJoinedAt,
  69. })
  70. }
  71. core.Log.Infof("从数据库获取了 %d 个群组", len(groups))
  72. c.Resp(gin.H{
  73. "message": fmt.Sprintf("成功同步 %d 个群组", len(groups)),
  74. "groups": groups,
  75. "count": len(groups),
  76. })
  77. }
  78. // GetChatInfo 获取指定群组的详细信息
  79. func (s *Server) GetChatInfo(ctx *gin.Context) {
  80. c := s.FromContext(ctx)
  81. type request struct {
  82. ChatId int64 `json:"chatId" form:"chatId" binding:"required"`
  83. }
  84. req := new(request)
  85. if err := c.ShouldBindQuery(req); err != nil {
  86. c.Fail("参数错误")
  87. return
  88. }
  89. // 检查 Bot 是否启用
  90. if !telegram.IsEnabled() {
  91. c.Fail("Telegram Bot 未启用")
  92. return
  93. }
  94. bot := telegram.GetBot()
  95. if bot == nil {
  96. c.Fail("无法获取 Bot 实例")
  97. return
  98. }
  99. // 获取群组信息
  100. chat, err := bot.GetChat(tgbotapi.ChatInfoConfig{
  101. ChatConfig: tgbotapi.ChatConfig{
  102. ChatID: req.ChatId,
  103. },
  104. })
  105. if err != nil {
  106. core.Log.Errorf("获取群组信息失败: %v", err)
  107. c.Fail(fmt.Sprintf("获取群组信息失败: %v", err))
  108. return
  109. }
  110. // 获取成员数量
  111. memberCount, err := bot.GetChatMembersCount(tgbotapi.ChatMemberCountConfig{
  112. ChatConfig: tgbotapi.ChatConfig{
  113. ChatID: req.ChatId,
  114. },
  115. })
  116. if err != nil {
  117. core.Log.Warnf("获取成员数量失败: %v", err)
  118. memberCount = 0
  119. }
  120. groupInfo := GroupData{
  121. ChatId: chat.ID,
  122. ChatType: chat.Type,
  123. Title: chat.Title,
  124. Username: chat.UserName,
  125. Description: chat.Description,
  126. MemberCount: memberCount,
  127. BotJoinedAt: time.Now().Unix(),
  128. }
  129. c.Resp(groupInfo)
  130. }
  131. // GroupData 群组数据结构
  132. type GroupData struct {
  133. ChatId int64 `json:"chatId"`
  134. ChatType string `json:"chatType"`
  135. Title string `json:"title"`
  136. Username string `json:"username"`
  137. Description string `json:"description"`
  138. MemberCount int `json:"memberCount"`
  139. BotJoinedAt int64 `json:"botJoinedAt"`
  140. }