修改了限流器,现在限流器是根据bot_token限流了

This commit is contained in:
zayac 2024-06-03 22:58:03 +08:00
parent f510000e10
commit 3e75cb6ef6

View File

@ -17,7 +17,6 @@ telegram_bot_token = os.getenv("TELEGRAM_BOT_TOKEN")
logger.remove()
# 添加新的日志处理器,设置日志级别为 DEBUG
logger.add(sys.stderr, level="INFO")
@ -48,11 +47,13 @@ async def _send_message_to_user(bot_token, target_id, message, rate_limiter, ses
await asyncio.sleep(1)
return await _send_message_to_user(bot_token, target_id, message, rate_limiter, session)
parse_mode = determine_parse_mode(message)
base_url = f"https://api.telegram.org/bot{bot_token}/sendMessage"
params = {
"chat_id": target_id,
"text": message,
"parse_mode": "MarkdownV2"
"parse_mode": parse_mode
}
max_retry = 3
@ -74,15 +75,46 @@ async def _send_message_to_user(bot_token, target_id, message, rate_limiter, ses
logger.debug('消息发送失败')
def determine_parse_mode(message_text):
"""
Determine the appropriate parse mode based on the message content.
For simplicity, assume that if the message contains certain HTML tags, use HTML parse mode.
Otherwise, use MarkdownV2.
"""
html_tags = ['<b>', '<i>', '<a>', '<code>', '<pre>']
if any(tag in message_text for tag in html_tags):
return 'HTML'
return 'MarkdownV2'
def parse_message(body):
parts = body.split('|')
if len(parts) == 3:
bot_token, target_id, message_text = parts
else:
parts = body.split('#')
if len(parts) == 3:
bot_token, target_id, message_text = parts
else:
raise ValueError("Unknown message format")
return bot_token, target_id, message_text
async def message_handler(message: aio_pika.IncomingMessage):
async with message.process():
logger.info(message.body.decode())
bot_token, target_id, message_text = message.body.decode().split('|')
try:
bot_token, target_id, message_text = parse_message(message.body.decode())
except ValueError as e:
logger.error(f'无法解析消息格式: {e}')
return
if target_id.startswith('-'): # 群组ID以'-'开头
rate_limiter = group_rate_limiters[target_id]
rate_limiter = group_rate_limiters[bot_token]
else:
rate_limiter = rate_limiters[target_id]
await _send_message_to_user(bot_token, target_id, message_text, rate_limiter, session)
rate_limiter = rate_limiters[bot_token]
if message_text:
await _send_message_to_user(bot_token, target_id, message_text, rate_limiter, session)
async def main():