mirror of
https://github.com/woodchen-ink/Q58Bot.git
synced 2025-07-18 13:52:07 +08:00
修复币安价格播报的定时和自动撤回功能
This commit is contained in:
parent
5744e7e88f
commit
7463bada9a
@ -33,8 +33,12 @@ func init() {
|
|||||||
symbols[i] = strings.ReplaceAll(s, "/", "")
|
symbols[i] = strings.ReplaceAll(s, "/", "")
|
||||||
}
|
}
|
||||||
|
|
||||||
// 初始化 singaporeTZ
|
singaporeTZ, err = time.LoadLocation("Asia/Singapore")
|
||||||
singaporeTZ = time.FixedZone("Asia/Singapore", 8*60*60) // UTC+8
|
if err != nil {
|
||||||
|
log.Printf("Error loading Singapore time zone: %v", err)
|
||||||
|
log.Println("Falling back to UTC+8")
|
||||||
|
singaporeTZ = time.FixedZone("Asia/Singapore", 8*60*60)
|
||||||
|
}
|
||||||
|
|
||||||
bot, err = tgbotapi.NewBotAPI(botToken)
|
bot, err = tgbotapi.NewBotAPI(botToken)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -59,7 +63,6 @@ type tickerInfo struct {
|
|||||||
func getTickerInfo(symbol string) (tickerInfo, error) {
|
func getTickerInfo(symbol string) (tickerInfo, error) {
|
||||||
client := binance.NewClient("", "")
|
client := binance.NewClient("", "")
|
||||||
|
|
||||||
// 获取当前价格
|
|
||||||
ticker, err := client.NewListPricesService().Symbol(symbol).Do(context.Background())
|
ticker, err := client.NewListPricesService().Symbol(symbol).Do(context.Background())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return tickerInfo{}, err
|
return tickerInfo{}, err
|
||||||
@ -67,13 +70,11 @@ func getTickerInfo(symbol string) (tickerInfo, error) {
|
|||||||
if len(ticker) == 0 {
|
if len(ticker) == 0 {
|
||||||
return tickerInfo{}, fmt.Errorf("no ticker found for symbol %s", symbol)
|
return tickerInfo{}, fmt.Errorf("no ticker found for symbol %s", symbol)
|
||||||
}
|
}
|
||||||
// 在 getTickerInfo 函数中
|
|
||||||
last, err := strconv.ParseFloat(ticker[0].Price, 64)
|
last, err := strconv.ParseFloat(ticker[0].Price, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return tickerInfo{}, err
|
return tickerInfo{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取24小时价格变化
|
|
||||||
stats, err := client.NewListPriceChangeStatsService().Symbol(symbol).Do(context.Background())
|
stats, err := client.NewListPriceChangeStatsService().Symbol(symbol).Do(context.Background())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return tickerInfo{}, err
|
return tickerInfo{}, err
|
||||||
@ -103,13 +104,7 @@ func formatChange(changePercent float64) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func sendPriceUpdate() {
|
func sendPriceUpdate() {
|
||||||
var now time.Time
|
now := time.Now().In(singaporeTZ)
|
||||||
if singaporeTZ != nil {
|
|
||||||
now = time.Now().In(singaporeTZ)
|
|
||||||
} else {
|
|
||||||
now = time.Now().UTC()
|
|
||||||
log.Println("Warning: singaporeTZ is nil, using UTC")
|
|
||||||
}
|
|
||||||
message := fmt.Sprintf("市场更新 - %s (SGT)\n\n", now.Format("2006-01-02 15:04:05"))
|
message := fmt.Sprintf("市场更新 - %s (SGT)\n\n", now.Format("2006-01-02 15:04:05"))
|
||||||
|
|
||||||
for _, symbol := range symbols {
|
for _, symbol := range symbols {
|
||||||
@ -147,11 +142,24 @@ func sendPriceUpdate() {
|
|||||||
|
|
||||||
func RunBinance() {
|
func RunBinance() {
|
||||||
log.Println("Starting Binance service...")
|
log.Println("Starting Binance service...")
|
||||||
for {
|
|
||||||
log.Println("Sending price update...")
|
|
||||||
sendPriceUpdate()
|
|
||||||
|
|
||||||
log.Println("Waiting for next update...")
|
sendPriceUpdate()
|
||||||
time.Sleep(1 * time.Hour)
|
|
||||||
|
var scheduleNextUpdate func()
|
||||||
|
scheduleNextUpdate = func() {
|
||||||
|
now := time.Now().In(singaporeTZ)
|
||||||
|
nextHour := now.Add(time.Hour).Truncate(time.Hour)
|
||||||
|
duration := nextHour.Sub(now)
|
||||||
|
|
||||||
|
time.AfterFunc(duration, func() {
|
||||||
|
log.Println("Sending hourly price update...")
|
||||||
|
sendPriceUpdate()
|
||||||
|
scheduleNextUpdate()
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
scheduleNextUpdate()
|
||||||
|
|
||||||
|
// 保持主 goroutine 运行
|
||||||
|
select {}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user