| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- package admin
- import (
- "app/commons/core"
- "app/telegram"
- "fmt"
- "github.com/gin-gonic/gin"
- tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
- "time"
- )
- // GetTelegramGroups 获取 Bot 加入的群组列表
- func (s *Server) GetTelegramGroups(ctx *gin.Context) {
- c := s.FromContext(ctx)
- // 检查 Bot 是否启用
- if !telegram.IsEnabled() {
- c.Fail("Telegram Bot 未启用")
- return
- }
- bot := telegram.GetBot()
- if bot == nil {
- c.Fail("无法获取 Bot 实例")
- return
- }
- // 获取群组列表
- // 注意: Telegram Bot API 没有直接获取所有群组的方法
- // 这里我们需要从 getUpdates 中提取群组信息
- // 或者从数据库中查询已记录的群组信息
- type GroupInfo struct {
- ChatId int64 `json:"chatId"`
- ChatType string `json:"chatType"`
- Title string `json:"title"`
- Username string `json:"username"`
- MemberCount int `json:"memberCount"`
- BotJoinedAt int64 `json:"botJoinedAt"`
- }
- // 这里简化处理,返回一个提示
- // 实际应该从数据库查询已记录的群组
- c.Resp(gin.H{
- "message": "请通过 Bot 添加到群组后,群组信息会自动记录",
- "groups": []GroupInfo{},
- })
- }
- // SyncTelegramGroups 同步 Bot 加入的群组到数据库
- // 从数据库获取已自动记录的群组信息
- func (s *Server) SyncTelegramGroups(ctx *gin.Context) {
- c := s.FromContext(ctx)
- // 检查 Bot 是否启用
- if !telegram.IsEnabled() {
- c.Fail("Telegram Bot 未启用")
- return
- }
- // 从数据库获取已自动记录的群组
- dbGroups, err := telegram.GetAllGroupsFromDB()
- if err != nil {
- core.Log.Errorf("从数据库获取群组失败: %v", err)
- c.Fail(fmt.Sprintf("获取群组失败: %v", err))
- return
- }
- // 转换为 GroupData 格式
- groups := make([]GroupData, 0, len(dbGroups))
- for _, g := range dbGroups {
- groups = append(groups, GroupData{
- ChatId: g.ChatId,
- ChatType: g.ChatType,
- Title: g.Title,
- Username: g.Username,
- Description: g.Description,
- MemberCount: g.MemberCount,
- BotJoinedAt: g.BotJoinedAt,
- })
- }
- core.Log.Infof("从数据库获取了 %d 个群组", len(groups))
- c.Resp(gin.H{
- "message": fmt.Sprintf("成功同步 %d 个群组", len(groups)),
- "groups": groups,
- "count": len(groups),
- })
- }
- // GetChatInfo 获取指定群组的详细信息
- func (s *Server) GetChatInfo(ctx *gin.Context) {
- c := s.FromContext(ctx)
- type request struct {
- ChatId int64 `json:"chatId" form:"chatId" binding:"required"`
- }
- req := new(request)
- if err := c.ShouldBindQuery(req); err != nil {
- c.Fail("参数错误")
- return
- }
- // 检查 Bot 是否启用
- if !telegram.IsEnabled() {
- c.Fail("Telegram Bot 未启用")
- return
- }
- bot := telegram.GetBot()
- if bot == nil {
- c.Fail("无法获取 Bot 实例")
- return
- }
- // 获取群组信息
- chat, err := bot.GetChat(tgbotapi.ChatInfoConfig{
- ChatConfig: tgbotapi.ChatConfig{
- ChatID: req.ChatId,
- },
- })
- if err != nil {
- core.Log.Errorf("获取群组信息失败: %v", err)
- c.Fail(fmt.Sprintf("获取群组信息失败: %v", err))
- return
- }
- // 获取成员数量
- memberCount, err := bot.GetChatMembersCount(tgbotapi.ChatMemberCountConfig{
- ChatConfig: tgbotapi.ChatConfig{
- ChatID: req.ChatId,
- },
- })
- if err != nil {
- core.Log.Warnf("获取成员数量失败: %v", err)
- memberCount = 0
- }
- groupInfo := GroupData{
- ChatId: chat.ID,
- ChatType: chat.Type,
- Title: chat.Title,
- Username: chat.UserName,
- Description: chat.Description,
- MemberCount: memberCount,
- BotJoinedAt: time.Now().Unix(),
- }
- c.Resp(groupInfo)
- }
- // GroupData 群组数据结构
- type GroupData struct {
- ChatId int64 `json:"chatId"`
- ChatType string `json:"chatType"`
- Title string `json:"title"`
- Username string `json:"username"`
- Description string `json:"description"`
- MemberCount int `json:"memberCount"`
- BotJoinedAt int64 `json:"botJoinedAt"`
- }
|