init code
This commit is contained in:
43
internal/services/tele/commands/chat.go
Normal file
43
internal/services/tele/commands/chat.go
Normal 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)
|
||||
}
|
||||
}
|
||||
12
internal/services/tele/commands/general.go
Normal file
12
internal/services/tele/commands/general.go
Normal 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)
|
||||
}
|
||||
5
internal/services/tele/commands/helper/error.go
Normal file
5
internal/services/tele/commands/helper/error.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package helper
|
||||
|
||||
import "errors"
|
||||
|
||||
var ErrorParamCountMismatch = errors.New("param count mismatched")
|
||||
41
internal/services/tele/commands/helper/handler.go
Normal file
41
internal/services/tele/commands/helper/handler.go
Normal 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)
|
||||
}
|
||||
}
|
||||
18
internal/services/tele/commands/market.go
Normal file
18
internal/services/tele/commands/market.go
Normal 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))
|
||||
}
|
||||
11
internal/services/tele/commands/start.go
Normal file
11
internal/services/tele/commands/start.go
Normal 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)
|
||||
}
|
||||
49
internal/services/tele/commands/token.go
Normal file
49
internal/services/tele/commands/token.go
Normal 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)
|
||||
}
|
||||
Reference in New Issue
Block a user