package telegram import ( "app/commons/core" "app/commons/services" "fmt" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" "strings" ) // handleCallbackQuery 处理回调查询 func handleCallbackQuery(callback *tgbotapi.CallbackQuery) { data := callback.Data // 处理抢红包 if strings.HasPrefix(data, "grab_") { packetNo := strings.TrimPrefix(data, "grab_") grabRedPacket(callback, packetNo) return } // 其他回调 answerCallback(callback.ID, "未知操作") } // grabRedPacket 抢红包 func grabRedPacket(callback *tgbotapi.CallbackQuery, packetNo string) { telegramID := callback.From.ID username := callback.From.UserName // 检查绑定状态 bindService := &services.TgBindService{} bound, userId := bindService.IsUserBound(telegramID, username) if !bound { answerCallback(callback.ID, "⚠️ 请先绑定账户!私聊我发送 /bind") // 私聊发送绑定提示 sendTextMessage(telegramID, "⚠️ 您尚未绑定平台账户,无法抢红包。\n\n请发送 /bind 获取绑定链接。") core.Log.Infof("抢红包 - 用户: %s, 红包: %s, 结果: 未绑定", username, packetNo) return } // 用真实 userId 抢红包 redPacketService := &services.RedPacketService{} record, err := redPacketService.GrabRedPacket( packetNo, userId, telegramID, username, ) if err != nil { answerCallback(callback.ID, err.Error()) core.Log.Infof("抢红包 - 用户: %s(userId:%d), 红包: %s, 结果: %s", username, userId, packetNo, err.Error()) return } // 抢到红包 amount := record.Amount.InexactFloat64() answerCallback(callback.ID, fmt.Sprintf("🎉 恭喜抢到 %.2f VND!", amount)) // 从数据库查询完整的红包信息和所有领取记录,构建实时消息 packet, records, _ := redPacketService.GetRedPacketDetail(packetNo) if packet == nil { return } // 构建红包头部信息 var packetTypeText string if packet.PacketType == 1 { packetTypeText = "普通红包" } else { packetTypeText = "手气红包" } editText := fmt.Sprintf("🧧 红包\n💰 %s\n\n总金额: %.2f VND | 个数: %d | %s", packet.BlessingWords, packet.TotalAmount.InexactFloat64(), packet.TotalCount, packetTypeText) // 追加每个人的领取记录 editText += fmt.Sprintf("\n\n📋 领取明细 (%d/%d):", packet.GrabbedCount, packet.TotalCount) for _, r := range records { displayName := r.TelegramUsername if displayName != "" { displayName = "@" + displayName } else { displayName = fmt.Sprintf("用户%d", r.TelegramId) } editText += fmt.Sprintf("\n✅ %s — %.2f VND", displayName, r.Amount.InexactFloat64()) } // 根据剩余情况决定是否保留按钮 if packet.RemainCount > 0 { keyboard := tgbotapi.NewInlineKeyboardMarkup( tgbotapi.NewInlineKeyboardRow( tgbotapi.NewInlineKeyboardButtonData( fmt.Sprintf("🎁 抢红包 (%d/%d)", packet.GrabbedCount, packet.TotalCount), fmt.Sprintf("grab_%s", packetNo), ), ), ) editMessageWithKeyboard(callback.Message.Chat.ID, callback.Message.MessageID, editText, keyboard) } else { editText += "\n\n🎊 红包已抢完!" editMessage(callback.Message.Chat.ID, callback.Message.MessageID, editText) } core.Log.Infof("抢红包 - 用户: %s(userId:%d), 红包: %s, 金额: %s, 结果: 成功", username, userId, packetNo, record.Amount.String()) }