package telegram import ( "app/commons/core" "app/commons/services" "fmt" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" "strings" ) // handleActionCallback 处理按钮面板的回调(action_ 前缀) func handleActionCallback(callback *tgbotapi.CallbackQuery) { action := strings.TrimPrefix(callback.Data, "action_") switch action { case "register": handleRegisterAction(callback) case "balance": handleBalanceAction(callback) case "records": handleRecordsAction(callback) case "help": handleHelpAction(callback) default: answerCallback(callback.ID, "未知操作") } } // handleRegisterAction 一键注册+绑定 func handleRegisterAction(callback *tgbotapi.CallbackQuery) { telegramId := callback.From.ID username := callback.From.UserName firstName := callback.From.FirstName platformService := &services.TgPlatformService{} userId, isNew, err := platformService.RegisterAndBind(telegramId, username, firstName) if err != nil { answerCallback(callback.ID, fmt.Sprintf("⚠️ %s", err.Error())) core.Log.Errorf("一键注册失败 - telegramId: %d, error: %v", telegramId, err) return } if isNew { answerCallback(callback.ID, "✅ 注册成功!已自动绑定,可以抢红包了!") // 私信发送详细信息 text := fmt.Sprintf("🎉 注册成功!\n\n"+ "✅ 平台账户已创建并自动绑定\n"+ "🆔 用户ID: %d\n\n"+ "现在可以直接在群里抢红包了!", userId) sendTextMessage(telegramId, text) } else { // 用户已存在(之前通过OAuth注册但未在bot绑定),现在已自动绑定 answerCallback(callback.ID, "✅ 已绑定!可以抢红包了!") } core.Log.Infof("一键注册 - 用户: %s, telegramId: %d, userId: %d, isNew: %v", username, telegramId, userId, isNew) } // handleBalanceAction 查询余额 func handleBalanceAction(callback *tgbotapi.CallbackQuery) { telegramId := callback.From.ID username := callback.From.UserName // 检查绑定状态 bindService := &services.TgBindService{} bound, _ := bindService.IsUserBound(telegramId, username) if !bound { answerCallback(callback.ID, "⚠️ 请先点击「一键注册」绑定账户") return } // 查询本地抢红包总额 platformService := &services.TgPlatformService{} records, err := platformService.GetUserGrabRecords(telegramId, 0) if err != nil { answerCallback(callback.ID, "⚠️ 查询失败,请稍后再试") return } // 统计抢红包总额和次数 var totalAmount float64 for _, r := range records { totalAmount += r.Amount.InexactFloat64() } text := fmt.Sprintf("💰 账户信息\n\n"+ "🧧 抢红包次数: %d 次\n"+ "💵 抢红包总额: %.2f\n\n"+ "💡 详细余额请登录平台查看", len(records), totalAmount) sendTextMessage(telegramId, text) answerCallback(callback.ID, "💰 已发送到私聊") } // handleRecordsAction 查询抢红包记录 func handleRecordsAction(callback *tgbotapi.CallbackQuery) { telegramId := callback.From.ID username := callback.From.UserName // 检查绑定状态 bindService := &services.TgBindService{} bound, _ := bindService.IsUserBound(telegramId, username) if !bound { answerCallback(callback.ID, "⚠️ 请先点击「一键注册」绑定账户") return } // 查询最近10条记录 platformService := &services.TgPlatformService{} records, err := platformService.GetUserGrabRecords(telegramId, 10) if err != nil { answerCallback(callback.ID, "⚠️ 查询失败,请稍后再试") return } if len(records) == 0 { sendTextMessage(telegramId, "📊 暂无抢红包记录\n\n等待群内红包发放后即可参与!") answerCallback(callback.ID, "📊 已发送到私聊") return } text := "📊 最近抢红包记录\n\n" for i, r := range records { timeStr := r.GrabbedAt.Format("01-02 15:04") bestTag := "" if r.IsBest == 1 { bestTag = " 🏆" } text += fmt.Sprintf("%d. %s | %.2f%s\n", i+1, timeStr, r.Amount.InexactFloat64(), bestTag) } text += "\n💡 🏆 = 手气最佳" sendTextMessage(telegramId, text) answerCallback(callback.ID, "📊 已发送到私聊") } // handleHelpAction 帮助信息 func handleHelpAction(callback *tgbotapi.CallbackQuery) { helpText := `📖 红包机器人帮助 🧧 红包相关: • 群组定期自动发放红包 • 点击红包消息下方的按钮即可抢红包 • 普通红包:每人金额相同 • 手气红包:每人金额随机 📝 使用步骤: 1️⃣ 点击「📝 一键注册」自动注册并绑定 2️⃣ 等待群内红包发放 3️⃣ 点击红包按钮抢红包 💡 按钮功能: • 📝 一键注册 - 自动注册平台账户并绑定 • 💰 余额查询 - 查看抢红包统计 • 📊 抢红包记录 - 查看最近领取记录 • ❓ 帮助 - 显示此帮助信息 ⚠️ 注意: • 未注册无法抢红包 • 每个红包只能抢一次 • 红包有效期由管理员设定` sendTextMessage(callback.From.ID, helpText) answerCallback(callback.ID, "📖 已发送到私聊") } // buildWelcomeKeyboard 构建欢迎消息按钮面板(群组消息用,含注册按钮) func buildWelcomeKeyboard() tgbotapi.InlineKeyboardMarkup { return tgbotapi.NewInlineKeyboardMarkup( tgbotapi.NewInlineKeyboardRow( tgbotapi.NewInlineKeyboardButtonData("📝 一键注册", "action_register"), tgbotapi.NewInlineKeyboardButtonData("💰 余额查询", "action_balance"), ), tgbotapi.NewInlineKeyboardRow( tgbotapi.NewInlineKeyboardButtonData("📊 抢红包记录", "action_records"), tgbotapi.NewInlineKeyboardButtonData("❓ 帮助", "action_help"), ), ) } // buildKeyboardForUser 根据用户绑定状态构建按钮面板(私聊用) // 已注册用户不显示"一键注册"按钮 func buildKeyboardForUser(telegramId int64, username string) tgbotapi.InlineKeyboardMarkup { bindService := &services.TgBindService{} bound, _ := bindService.IsUserBound(telegramId, username) if bound { // 已注册:只显示功能按钮 return tgbotapi.NewInlineKeyboardMarkup( tgbotapi.NewInlineKeyboardRow( tgbotapi.NewInlineKeyboardButtonData("💰 余额查询", "action_balance"), tgbotapi.NewInlineKeyboardButtonData("📊 抢红包记录", "action_records"), ), tgbotapi.NewInlineKeyboardRow( tgbotapi.NewInlineKeyboardButtonData("❓ 帮助", "action_help"), ), ) } // 未注册:显示完整面板(含注册按钮) return buildWelcomeKeyboard() }