| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- package telegram
- import (
- "app/commons/core"
- "app/commons/i18n"
- "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
- // 先查询红包获取语言设置
- redPacketService := &services.RedPacketService{}
- packetInfo, _, _ := redPacketService.GetRedPacketDetail(packetNo)
- lang := "vi"
- if packetInfo != nil && packetInfo.Lang != "" {
- lang = packetInfo.Lang
- }
- m := i18n.GetLang(lang)
- // 检查绑定状态
- bindService := &services.TgBindService{}
- bound, userId := bindService.IsUserBound(telegramID, username)
- if !bound {
- answerCallback(callback.ID, m.NotBound)
- sendTextMessage(telegramID, m.NotBoundDM)
- core.Log.Infof("抢红包 - 用户: %s, 红包: %s, 结果: 未绑定", username, packetNo)
- return
- }
- // 用真实 userId 抢红包
- 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(m.GrabSuccess, amount, packetInfo.Symbol))
- // 从数据库查询完整的红包信息和所有领取记录,构建实时消息
- packet, records, _ := redPacketService.GetRedPacketDetail(packetNo)
- if packet == nil {
- return
- }
- // 构建红包头部信息
- packetTypeText := i18n.PacketTypeText(packet.PacketType, lang)
- editText := fmt.Sprintf("%s\n💰 %s\n\n%s: %.2f %s | %s: %d | %s",
- m.RedPacketTitle,
- packet.BlessingWords,
- m.TotalAmount, packet.TotalAmount.InexactFloat64(), packet.Symbol,
- m.Count, packet.TotalCount,
- packetTypeText)
- // 追加每个人的领取记录
- editText += fmt.Sprintf("\n\n"+m.GrabDetail, packet.GrabbedCount, packet.TotalCount)
- for _, r := range records {
- displayName := r.TelegramUsername
- if displayName != "" {
- displayName = "@" + displayName
- } else {
- displayName = fmt.Sprintf(m.UserPrefix, r.TelegramId)
- }
- editText += fmt.Sprintf("\n"+m.GrabRecord, displayName, r.Amount.InexactFloat64(), packet.Symbol)
- }
- // 根据剩余情况决定是否保留按钮
- if packet.RemainCount > 0 {
- keyboard := tgbotapi.NewInlineKeyboardMarkup(
- tgbotapi.NewInlineKeyboardRow(
- tgbotapi.NewInlineKeyboardButtonData(
- fmt.Sprintf(m.GrabButtonWithNum, packet.GrabbedCount, packet.TotalCount),
- fmt.Sprintf("grab_%s", packetNo),
- ),
- ),
- )
- editMessageWithKeyboard(callback.Message.Chat.ID, callback.Message.MessageID, editText, keyboard)
- } else {
- editText += "\n\n" + m.AllGrabbedMsg
- editMessage(callback.Message.Chat.ID, callback.Message.MessageID, editText)
- }
- core.Log.Infof("抢红包 - 用户: %s(userId:%d), 红包: %s, 金额: %s, 结果: 成功",
- username, userId, packetNo, record.Amount.String())
- }
|