You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

81 lines
2.2 KiB

#!/usr/bin/env python3
import ssl, time, hashlib, json, websocket
DEVICE_NO = '199839C0AAAE99653D2E'
DEVICE_ID = '2032013791868043266'
PRODUCT_ID = '2031271967792472065'
PRODUCT_KEY = 'hotepZZwvouWQjHz'
DEVICE_SECRET = 'ijdthZGQELhkmigf'
ts = str(int(time.time() * 1000))
sign = hashlib.md5((DEVICE_SECRET + ts).encode()).hexdigest()
url = (f'wss://ivs.chinamobiledevice.com:11443/app-ws/v2/asr'
f'?deviceNo={DEVICE_NO}&deviceId={DEVICE_ID}'
f'&productId={PRODUCT_ID}&productKey={PRODUCT_KEY}'
f'&sign={sign}&sn=py-test&ts={ts}')
print(f'connecting...\n{url}')
ws = websocket.WebSocket(sslopt={'cert_reqs': ssl.CERT_NONE})
ws.settimeout(15)
ws.connect(url)
print('connected! sending start frame immediately...')
import uuid
start_frame = json.dumps({
'type': 'start',
'data': {
'cuid': DEVICE_ID,
'format': 'pcm',
'sample': 16000,
'support_dcs': 2,
'chunk_size': 10240,
'support_tts': True,
'support_text2dcs': True,
'user_agent': 'voitrans/1.0',
'dialog_request_id': str(uuid.uuid4()),
'access_rc': True,
'support_part_tts': True,
'need_stoplisten': True,
'need_dialogue_finish': True,
'result_trans2directive': False,
'rc_version': '5',
'messages': [],
'client_context': [],
}
})
ws.send(start_frame)
print('start frame sent, waiting for ready...')
# 等待 ready
while True:
msg = ws.recv()
print('received:', msg)
data = json.loads(msg)
if data.get('type') == 'ready':
print('--- ready received, sending text "你好" ---')
break
# 发送文本
text_frame = json.dumps({'type': 'text', 'data': {'text': '你好'}})
ws.send(text_frame)
print('text frame sent')
# 发送 finish
finish_frame = json.dumps({'type': 'finish'})
ws.send(finish_frame)
print('finish frame sent, waiting for AI response...')
# 接收所有回包直到 end=1
while True:
msg = ws.recv()
print('received:', msg)
try:
data = json.loads(msg)
if data.get('end') == 1:
print('--- session end (end=1), closing ---')
break
except Exception:
pass
ws.close()
print('done.')