package redpacket import ( "app/commons/constant" "app/commons/core" "app/commons/i18n" "app/commons/model/entity" "app/commons/services" "app/telegram" "fmt" "github.com/gin-gonic/gin" "github.com/shopspring/decimal" "time" ) // SendRedPacket 发红包 func (s *Server) SendRedPacket(ctx *gin.Context) { c := s.FromContext(ctx) type request struct { GroupID string `json:"groupId" binding:"required"` // Telegram群组ID TotalAmount decimal.Decimal `json:"totalAmount" binding:"required"` // 红包总金额 TotalCount int `json:"totalCount" binding:"required"` // 红包个数 PacketType int `json:"packetType" binding:"required"` // 1=普通, 2=手气 Symbol string `json:"symbol"` // 币种,默认VND BlessingWords string `json:"blessingWords"` // 祝福语 ExpireMinutes int `json:"expireMinutes"` // 过期时间(分钟),默认10 MaxGrabAmount decimal.Decimal `json:"maxGrabAmount"` // 单人最大可领取金额(0=不限制) Lang string `json:"lang"` // 消息语言: vi/id/en/zh } req := new(request) if err := c.ShouldBindBodyWithJSON(req); err != nil { c.Fail(constant.ErrorParams) return } // 防刷控制(3秒内不能重复发) if !c.RepeatFilter(fmt.Sprintf("SendRedPacket:%d", c.UserId()), time.Second*3) { c.Fail(constant.ErrorFrequent) return } // 默认语言 if req.Lang == "" { req.Lang = "vi" } // 默认币种(根据语言) if req.Symbol == "" { req.Symbol = i18n.DefaultSymbol(req.Lang) } // 创建红包 packet, err := s.RedPacketService.CreateRedPacket( c.UserId(), req.GroupID, req.PacketType, req.TotalAmount, req.TotalCount, req.Symbol, req.BlessingWords, req.ExpireMinutes, req.MaxGrabAmount, req.Lang, ) if err != nil { c.Fail(err.Error()) return } // 发送到 Telegram 群组并保存消息ID go func() { userSendToTelegram(packet, "System", req.ExpireMinutes, req.Lang) }() c.Resp(gin.H{ "packetId": packet.Id, "packetNo": packet.PacketNo, "message": "红包发送成功", }) } // userSendToTelegram 用户发红包到Telegram群组并保存消息ID func userSendToTelegram(packet *entity.TgRedPacket, senderName string, expireMinutes int, lang string) { if packet == nil { return } // 调用 telegram 包的发送函数,获取返回的消息ID msgId := telegram.SendRedPacketToGroup( packet.GroupId, packet.PacketNo, senderName, packet.TotalAmount.InexactFloat64(), packet.TotalCount, packet.PacketType, packet.BlessingWords, expireMinutes, lang, packet.Symbol, ) // 保存 Telegram 消息ID到红包记录 if msgId > 0 { db := services.NewComService().DB() db.Model(&entity.TgRedPacket{}).Where("id = ?", packet.Id).Update("message_id", msgId) } core.Log.Infof("红包已发送到Telegram群组 - PacketNo: %s, GroupID: %s, MessageID: %d", packet.PacketNo, packet.GroupId, msgId) }