Files
crypto-price-bot/internal/services/tele/commands/token.go
T
thuanle 88de029e14 feat: unify token message composition across sources
Remove alpha-first early return and build one rich message from all
available sources (spot/future/alpha) with conditional rows.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-26 17:19:59 +07:00

136 lines
3.2 KiB
Go

package commands
import (
"strings"
"gopkg.in/telebot.v3"
"me.thuanle/bbot/internal/configs/tele"
"me.thuanle/bbot/internal/data"
"me.thuanle/bbot/internal/helper/binancex"
"me.thuanle/bbot/internal/services/tele/chat"
"me.thuanle/bbot/internal/services/tele/view"
)
type buildRichTokenMessageArgs struct {
Token string
HasSpot bool
SpotPrice float64
HasFuture bool
FuturePrice float64
FundingRate float64
FundingTimeMs int64
HasAlpha bool
AlphaPrice float64
HasAlpha24h bool
Alpha24h float64
HasMarginAPR bool
MarginAPRPercent float64
}
func buildRichTokenMessageInput(a buildRichTokenMessageArgs) view.RichTokenMessageInput {
return view.RichTokenMessageInput{
Token: a.Token,
HasSpot: a.HasSpot,
SpotPrice: a.SpotPrice,
HasFuture: a.HasFuture,
FuturePrice: a.FuturePrice,
FundingRate: a.FundingRate,
FundingTimeMs: a.FundingTimeMs,
HasAlpha: a.HasAlpha,
AlphaPrice: a.AlphaPrice,
HasAlpha24h: a.HasAlpha24h,
Alpha24hChange: a.Alpha24h,
HasMarginAPR: a.HasMarginAPR,
MarginAPRPercent: a.MarginAPRPercent,
}
}
func showStickerMode(context telebot.Context, token string) {
token = strings.ToUpper(token)
stickerIdx, ok := tele.Token2StickerIdxMap[token]
if !ok {
return
}
stickers, err := context.Bot().StickerSet(tele.StickerSet)
if err == nil && stickers != nil {
_ = context.Reply(&stickers.Stickers[stickerIdx])
}
}
func OnTokenInfoByToken(context telebot.Context, token string) error {
token = strings.ToUpper(token)
showStickerMode(context, token)
var (
hasAlpha, hasAlpha24h bool
alphaPrice, alpha24h float64
hasFuture, hasSpot bool
futurePrice float64
fundingRate float64
fundingTime int64
spotPrice float64
)
if alphaToken, ok := data.Market.GetAlphaToken(token); ok {
hasAlpha = true
alphaPrice = alphaToken.GetPrice()
hasAlpha24h = true
alpha24h = alphaToken.GetPercentChange24h()
}
symbols := binancex.Token2Symbols(token)
if len(symbols) > 0 {
fp, fr, ft, ok := data.Market.GetFuturePrice(symbols[0])
if ok {
hasFuture = true
futurePrice = fp
fundingRate = fr
fundingTime = ft
sSymbol := binancex.Future2SpotSymbol(symbols[0])
if sp, ok := data.Market.GetSpotPrice(sSymbol); ok {
hasSpot = true
spotPrice = sp
}
}
}
marginRates := data.Market.GetMarginInterestRates()
marginAPR := marginRates[token] * 365 * 100
hasMarginAPR := marginRates[token] != 0
if !hasSpot && !hasFuture && !hasAlpha {
return nil
}
msg := view.RenderRichTokenMessage(buildRichTokenMessageInput(buildRichTokenMessageArgs{
Token: token,
HasSpot: hasSpot,
SpotPrice: spotPrice,
HasFuture: hasFuture,
FuturePrice: futurePrice,
FundingRate: fundingRate,
FundingTimeMs: fundingTime,
HasAlpha: hasAlpha,
AlphaPrice: alphaPrice,
HasAlpha24h: hasAlpha24h,
Alpha24h: alpha24h,
HasMarginAPR: hasMarginAPR,
MarginAPRPercent: marginAPR,
}))
_ = chat.ReplyMessage(context, msg)
return nil
}
func OnTokenInfo(context telebot.Context) error {
text := strings.ToLower(context.Text())
return OnTokenInfoByToken(context, text)
}