Telethon Websocket解析EtherDrops消息转JSON的方案问询
高效解析Telethon获取的EtherDROPS6_bot消息为指定JSON结构
问题描述
我通过Telethon对接EtherDROPS6_bot获取消息,消息为单一字符串格式,需要将其解析为指定结构的JSON用于后续业务。但消息中Sent和Received的位置可能颠倒,常规顺序解析方法不适用,现寻求高效的解析方案。
当前Telethon代码
from telethon.sync import TelegramClient, events with TelegramClient('name', api_id, api_hash) as client: # client.send_message('me', 'Hello, myself!') print(client.download_profile_photo('me')) @client.on(events.NewMessage(chats=['EtherDROPS6_bot'])) # pattern='(?i).*Hello' async def handler(event): # print(event.text) wallet = event.text if "Sent" in wallet and "Received" in wallet: print('^^^^^ START OF EVENT ^^^^^ ') print(type(wallet)) #<<<< <class 'str'> print(wallet) # res = wallet.replace(']', '\n').replace(')', '\n').replace('[', '').replace('(', '') print('^^^^^ END OF EVENT ^^^^^ ')
示例消息
[WALLET_1](https://etherscan.io/address/0xABC) ETH Sent: 0.1 ETH (~$304) To: [Uniswap V3: Router 2 ](https://etherscan.io/address/0xUNISWAP_CONTRACT)Received: 4,780 [ERC-20_TOKEN_NAME](https://etherscan.io/token/0xTOKEN_CONTRACT) From: [0xfa17..ABCD ](https://etherscan.io/address/0xCONTRACT_PAIR)[Tx hash](https://etherscan.io/tx/0xTXHASH)
目标JSON结构
data = { "TRADE": { "WALLET": [ { "OWNER": "" } ], "RECEIVED": [ { "RCVD TOKEN": "", "RCVD TOKEN QTY": "", "RCVD TOKEN CONTRACT ADDRESS": "" } ], "SENT": [ { "SENT TOKEN": "", "SENT TOKEN QTY": "", "SENT TOKEN CONTRACT ADDRESS": "" } ], "VALUATION": [ { "SENT VALUE": "" } ], "NETWORK": [ { "NETWORK": "" } ] } }
解决方案
可以用正则表达式实现不依赖顺序的解析,分别匹配Sent、Received、钱包地址等关键信息,具体实现如下:
解析代码示例
import re import json async def handler(event): wallet_text = event.text if "Sent" in wallet_text and "Received" in wallet_text: # 初始化目标结构 result = { "TRADE": { "WALLET": [{"OWNER": ""}], "RECEIVED": [{"RCVD TOKEN": "", "RCVD TOKEN QTY": "", "RCVD TOKEN CONTRACT ADDRESS": ""}], "SENT": [{"SENT TOKEN": "", "SENT TOKEN QTY": "", "SENT TOKEN CONTRACT ADDRESS": ""}], "VALUATION": [{"SENT VALUE": ""}], "NETWORK": [{"NETWORK": ""}] } } # 解析钱包地址 wallet_match = re.search(r'\[WALLET_\d+\]\(https://etherscan.io/address/(0x[0-9a-fA-F]+)\)', wallet_text) if wallet_match: result["TRADE"]["WALLET"][0]["OWNER"] = wallet_match.group(1) # 解析网络类型 if "ETH" in wallet_text: result["TRADE"]["NETWORK"][0]["NETWORK"] = "Ethereum" # 解析Sent区块(独立匹配,不受位置影响) sent_match = re.search(r'Sent: ([\d.]+) (\w+) \(\~(\$[\d,]+)\).*To: .+\(https://etherscan.io/address/(0x[0-9a-fA-F]+)\)', wallet_text, re.DOTALL) if sent_match: result["TRADE"]["SENT"][0]["SENT TOKEN QTY"] = sent_match.group(1) result["TRADE"]["SENT"][0]["SENT TOKEN"] = sent_match.group(2) result["TRADE"]["VALUATION"][0]["SENT VALUE"] = sent_match.group(3) result["TRADE"]["SENT"][0]["SENT TOKEN CONTRACT ADDRESS"] = sent_match.group(4) # 解析Received区块(独立匹配,不受位置影响) received_match = re.search(r'Received: ([\d,]+) \[([^\]]+)\]\(https://etherscan.io/token/(0x[0-9a-fA-F]+)\)', wallet_text, re.DOTALL) if received_match: result["TRADE"]["RECEIVED"][0]["RCVD TOKEN QTY"] = received_match.group(1).replace(',', '') result["TRADE"]["RECEIVED"][0]["RCVD TOKEN"] = received_match.group(2) result["TRADE"]["RECEIVED"][0]["RCVD TOKEN CONTRACT ADDRESS"] = received_match.group(3) # 转换为JSON格式输出 json_result = json.dumps(result, indent=2) print(json_result)
核心说明
- 使用
re.DOTALL参数让正则表达式的.匹配换行符,处理消息中的换行问题 - 两个核心区块
Sent和Received采用独立正则匹配,完全不受彼此出现顺序的影响 - 对Received数量中的逗号做去除处理,方便后续数值计算
- 可根据实际消息格式微调正则规则,覆盖更多边缘场景
内容的提问来源于stack exchange,提问作者snub-fighter
相关产品推荐
相关产品推荐

