From dc6a40d96a234a72d3accbc267ff702b40779027 Mon Sep 17 00:00:00 2001 From: wood Date: Tue, 3 Sep 2024 19:17:59 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8E=A8=E9=80=81=E6=96=B0=E6=B6=88=E6=81=AF?= =?UTF-8?q?=E6=97=B6=E5=88=A0=E9=99=A4=E6=97=A7=E6=B6=88=E6=81=AF,=20?= =?UTF-8?q?=E4=B8=8D=E7=84=B6=E5=85=A8=E6=98=AF=E6=8E=A8=E9=80=81=E9=80=9A?= =?UTF-8?q?=E7=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/binance.py | 65 +++++++++++++++++++++++++++----------------------- 1 file changed, 35 insertions(+), 30 deletions(-) diff --git a/src/binance.py b/src/binance.py index 8f688da..a215bf6 100644 --- a/src/binance.py +++ b/src/binance.py @@ -3,7 +3,6 @@ import ccxt import telebot import schedule import time -import logging from datetime import datetime, timedelta import pytz @@ -14,6 +13,9 @@ CHAT_ID = os.environ['CHAT_ID'] bot = telebot.TeleBot(BOT_TOKEN) SYMBOLS = os.environ['SYMBOLS'].split(',') +# 用于存储上一条消息的ID +last_message_id = None + def get_ticker_info(symbol): ticker = exchange.fetch_ticker(symbol) return { @@ -27,6 +29,7 @@ def get_ticker_info(symbol): 'bid': ticker['bid'], 'ask': ticker['ask'] } + def format_change(change_percent): if change_percent > 0: return f"🔼 +{change_percent:.2f}%" @@ -34,7 +37,9 @@ def format_change(change_percent): return f"🔽 {change_percent:.2f}%" else: return f"◀▶ {change_percent:.2f}%" + def send_price_update(): + global last_message_id now = datetime.now(singapore_tz) message = f"市场更新 - {now.strftime('%Y-%m-%d %H:%M:%S')} (SGT)\n\n" @@ -50,38 +55,38 @@ def send_price_update(): message += f"24h 成交额: ${info['quote_volume']:.2f}\n" message += f"买一/卖一: ${info['bid']:.7f} / ${info['ask']:.7f}\n\n" - bot.send_message(CHAT_ID, message, parse_mode='Markdown') + # 如果存在上一条消息,则删除它 + if last_message_id: + try: + bot.delete_message(chat_id=CHAT_ID, message_id=last_message_id) + except Exception as e: + print(f"Failed to delete previous message: {e}") + + # 发送新消息并保存其ID + sent_message = bot.send_message(CHAT_ID, message, parse_mode='Markdown') + last_message_id = sent_message.message_id def run(): - logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s') - logger = logging.getLogger('BinanceUpdater') - + # 立即执行一次价格更新 + print("Sending initial price update...") + send_price_update() + + # 设置定时任务,每小时整点执行 + for hour in range(24): + schedule.every().day.at(f"{hour:02d}:00").do(send_price_update) + + print("Scheduled tasks set. Waiting for next hour...") + + # 等待下一个整点 + now = datetime.now(singapore_tz) + next_hour = (now + timedelta(hours=1)).replace(minute=0, second=0, microsecond=0) + time.sleep((next_hour - now).total_seconds()) + + print("Starting main loop...") + while True: - try: - # 立即执行一次价格更新 - logger.info("Sending initial price update...") - send_price_update() - - # 设置定时任务,每小时整点执行 - for hour in range(24): - schedule.every().day.at(f"{hour:02d}:00").do(send_price_update) - - logger.info("Scheduled tasks set. Waiting for next hour...") - - # 等待下一个整点 - now = datetime.now(singapore_tz) - next_hour = (now + timedelta(hours=1)).replace(minute=0, second=0, microsecond=0) - time.sleep((next_hour - now).total_seconds()) - - logger.info("Starting main loop...") - - while True: - schedule.run_pending() - time.sleep(30) # 每30秒检查一次,可以根据需要调整 - except Exception as e: - logger.error(f"An error occurred in BinanceUpdater: {str(e)}") - logger.info("Attempting to restart BinanceUpdater in 60 seconds...") - time.sleep(60) # 等待60秒后重试 + schedule.run_pending() + time.sleep(30) # 每30秒检查一次,可以根据需要调整 if __name__ == '__main__': run()