init code

This commit is contained in:
thuanle
2024-10-24 09:53:23 +07:00
parent a7559b3f9d
commit 92a63c7885
43 changed files with 1115 additions and 0 deletions

View File

@@ -0,0 +1,43 @@
package commands
import (
"github.com/rs/zerolog/log"
"gopkg.in/telebot.v3"
"me.thuanle/bbot/internal/configs/tele"
"me.thuanle/bbot/internal/helper/binancex"
"strings"
)
func OnChatHandler(context telebot.Context) error {
text := strings.ToLower(context.Text())
switch {
case text == "p":
return OnGetTopPrices(context)
case text == "f" || text == "fee":
return OnGetTopFundingFee(context)
case binancex.IsToken(text):
return OnTokenInfo(context)
default:
//ignore it
}
return nil
}
func OnStickerHandler(context telebot.Context) error {
stickerId := context.Message().Sticker.UniqueID
switch stickerId {
case tele.StickerTop:
return OnGetTopPrices(context)
default:
token, ok := tele.Sticker2TokenMap[stickerId]
if !ok {
log.Debug().
Any("sticker", context.Message().Sticker).
Msg("Sticker received")
return nil
}
return OnTokenInfoByToken(context, token)
}
}

View File

@@ -0,0 +1,12 @@
package commands
import (
"gopkg.in/telebot.v3"
"me.thuanle/bbot/internal/services/tele/chat"
"me.thuanle/bbot/internal/utils/netx"
)
func OnGetIp(context telebot.Context) error {
ip := netx.GetPublicIp()
return chat.ReplyMessageCode(context, ip)
}

View File

@@ -0,0 +1,5 @@
package helper
import "errors"
var ErrorParamCountMismatch = errors.New("param count mismatched")

View File

@@ -0,0 +1,41 @@
package helper
import (
"gopkg.in/telebot.v3"
)
func throwErrorParamCountMismatch(_ telebot.Context) error {
return ErrorParamCountMismatch
}
// Base on the number of Args in context, it will execute the (n+1)-th function in the list
// - If the context have 0 argument, it will call funcs[0]
// - If the context have 1 argument, it will call funcs[1]
// Otherwise, it will call `other` function
func HandleZ0nArgs(other telebot.HandlerFunc, funcs ...telebot.HandlerFunc) telebot.HandlerFunc {
return func(context telebot.Context) error {
args := context.Args()
if len(args) > len(funcs)-1 {
return other(context)
}
return funcs[len(args)](context)
}
}
// Base on the number of Args in context, it will execute the (n+1)-th function in the list
// - If the context have 0 argument, it will call funcs[0]
// - If the context have 1 argument, it will call funcs[1]
// Otherwise, it will throw error
func Handle0nArgs(funcs ...telebot.HandlerFunc) telebot.HandlerFunc {
return HandleZ0nArgs(throwErrorParamCountMismatch, funcs...)
}
func HandleGetSet(getFunc telebot.HandlerFunc, setFunc telebot.HandlerFunc) telebot.HandlerFunc {
return func(context telebot.Context) error {
if len(context.Args()) == 0 {
return getFunc(context)
}
return setFunc(context)
}
}

View File

@@ -0,0 +1,18 @@
package commands
import (
"gopkg.in/telebot.v3"
"me.thuanle/bbot/internal/services/controllers"
"me.thuanle/bbot/internal/services/tele/chat"
"me.thuanle/bbot/internal/services/tele/view"
)
func OnGetTopPrices(context telebot.Context) error {
sym, price, rate := controllers.GetTopPrices()
return chat.ReplyMessagePre(context, view.RenderOnGetTopPricesMessage(sym, price, rate))
}
func OnGetTopFundingFee(context telebot.Context) error {
fee, float64s, cds := controllers.GetTopFundingFee()
return chat.ReplyMessagePre(context, view.RenderOnGetTopFundingFeeMessage(fee, float64s, cds))
}

View File

@@ -0,0 +1,11 @@
package commands
import "gopkg.in/telebot.v3"
const what = `Welcome on board! Here is some tips:
- /p to get pricing
- /l to list current positions`
func OnStart(context telebot.Context) error {
return context.Send(what)
}

View File

@@ -0,0 +1,49 @@
package commands
import (
"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"
"strings"
)
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 {
symbols := binancex.Token2Symbols(token)
if len(symbols) == 0 {
return nil
}
showStickerMode(context, token)
fp, fundRate, fundTime, ok := data.Market.GetFuturePrice(symbols[0])
if !ok {
return nil
}
sSymbol := binancex.Future2SpotSymbol(symbols[0])
sp, _ := data.Market.GetSpotPrice(sSymbol)
_ = chat.ReplyMessage(context, view.RenderOnPriceMessage(symbols[0], sp, fp, fundRate, fundTime))
return nil
}
func OnTokenInfo(context telebot.Context) error {
text := strings.ToLower(context.Text())
return OnTokenInfoByToken(context, text)
}