Commit 2a99051d authored by zhanhuasheng's avatar zhanhuasheng

增加readMsg操作

parent d11bbce8
...@@ -14,7 +14,6 @@ class ws: ...@@ -14,7 +14,6 @@ class ws:
self.loop = asyncio.get_event_loop() self.loop = asyncio.get_event_loop()
self.client_id = '' self.client_id = ''
self.attr = attr self.attr = attr
async def get_client_id(self): async def get_client_id(self):
if not self.websocket: if not self.websocket:
self.websocket = await websockets.connect(self.url) self.websocket = await websockets.connect(self.url)
...@@ -28,6 +27,7 @@ class ws: ...@@ -28,6 +27,7 @@ class ws:
self.client_id = enter_res['data']['cliend'] self.client_id = enter_res['data']['cliend']
else: # action是enter的情况 else: # action是enter的情况
self.client_id = enter_res['data']['clientId'] self.client_id = enter_res['data']['clientId']
setattr(Global,self.attr+'_clientId',self.client_id)
async def connect(self): async def connect(self):
try: try:
self.websocket = await websockets.connect(self.url) self.websocket = await websockets.connect(self.url)
...@@ -81,12 +81,15 @@ async def ws_action(ws,case): ...@@ -81,12 +81,15 @@ async def ws_action(ws,case):
:param case: 传入用例数据 :param case: 传入用例数据
''' '''
action = case['action'] #操作 action = case['action'] #操作
if action == 'sendmsg': if action in ['sendmsg','readMsg']:
response = await ws_send_handler(case=case,ws=ws) response = await ws_send_handler(case=case,ws=ws,action=action)
if response.get('code'): if response.get('code'):
assert response['code'] == 200 assert response['code'] == 200
else: elif response.get('response'):
assert response['response']['code'] == 200 assert response['response']['code'] == 200
else:
if response['action'] == 'roomTips':
assert response['brandId'] == getattr(Global,'brandId')
# type = case['data']['type'] # type = case['data']['type']
# if type in ['emoji','file','link','rate','product']: # if type in ['emoji','file','link','rate','product']:
# if type not in ['file','rate','emoji']: #type等于file的时候,后端返回的action,willdesk端是sendmsg,c端又是readMsg,干脆不作判断了 # if type not in ['file','rate','emoji']: #type等于file的时候,后端返回的action,willdesk端是sendmsg,c端又是readMsg,干脆不作判断了
...@@ -104,24 +107,25 @@ async def ws_action(ws,case): ...@@ -104,24 +107,25 @@ async def ws_action(ws,case):
msg = json.loads(await ws.receive_message()) msg = json.loads(await ws.receive_message())
ws_assert(expected=expected,msg=msg) ws_assert(expected=expected,msg=msg)
async def ws_send_handler(case,ws): async def ws_send_handler(case,ws,action):
data = case['data'] # 消息数据 data = case['data'] # 消息数据
content = getattr(livechatData,data['type']) #根据type取到对应格式的content roomId = str(data['roomId']) if ws.attr == 'willdesk' else ''
uid = data['uid'] if data.get('uid') else getattr(Global, 'userId')
siteId = data['siteId'] if data.get('siteId') else getattr(Global, 'shopId')
fromId = data['fromId'] if data.get('fromId') else getattr(Global, 'customerId')
if ws.client_id == '':
await ws.get_client_id()
client_id = ws.client_id
if action == 'sendmsg':
content = getattr(livechatData, data['type']) # 根据type取到对应格式的content
if data['type'] != 'rate': if data['type'] != 'rate':
msgType = 'newstext' msgType = 'newstext'
else: else:
msgType = 'rate' msgType = 'rate'
fromId = data['fromId'] if data.get('fromId') else getattr(Global, 'customerId')
siteId = data['siteId'] if data.get('siteId') else getattr(Global, 'shopId')
uid = data['uid'] if data.get('uid') else getattr(Global, 'userId')
username = data['username'] if data.get('username') else getattr(Global, 'name') username = data['username'] if data.get('username') else getattr(Global, 'name')
isNote = data['isNote'] if data.get('isNote') else False isNote = data['isNote'] if data.get('isNote') else False
roomId = str(data['roomId']) if ws.attr == 'willdesk' else ''
avatar = 'https://img.willdesk.com/${picUrl}' if ws.attr == 'willdesk' else '' avatar = 'https://img.willdesk.com/${picUrl}' if ws.attr == 'willdesk' else ''
if ws.client_id == '':
await ws.get_client_id()
client_id = ws.client_id
if ws.attr == 'willdesk': if ws.attr == 'willdesk':
data = { data = {
'avatar': avatar, 'avatar': avatar,
...@@ -155,8 +159,21 @@ async def ws_send_handler(case,ws): ...@@ -155,8 +159,21 @@ async def ws_send_handler(case,ws):
'uid': str(fromId), # userId 'uid': str(fromId), # userId
'username': Global.customerName # 品牌名称 'username': Global.customerName # 品牌名称
} }
elif action == 'readMsg':
msgId = data['msgId'],
msgTime = data['msgTime']
data = {
'clientId' : client_id,
'msgId' : str(msgId),
'msgTime' : str(msgTime),
'platform' : 1,
'roomId' : roomId,
'siteId' : str(siteId),
'utype' : 2 if ws.attr == 'willdesk' else 1,
'uid' : str(uid) if ws.attr == 'willdesk' else str(fromId)
}
messageData = { messageData = {
'action': 'sendmsg', 'action': action,
'data': data, 'data': data,
'seq': str(random.randint(1000000000000000, 9999999999999999)) 'seq': str(random.randint(1000000000000000, 9999999999999999))
} }
...@@ -204,34 +221,4 @@ async def ws_run(ws,case): ...@@ -204,34 +221,4 @@ async def ws_run(ws,case):
await asyncio.wait_for(task,timeout=10) await asyncio.wait_for(task,timeout=10)
# loop.run_until_complete(ws_action(ws=ws,case=case)) # loop.run_until_complete(ws_action(ws=ws,case=case))
# 使用示例
# async def main():
# data = json.dumps({
# 'action': "sendmsg",
# 'data': {
# 'avator': "",
# 'clientId': 'cnkm29etjph80j73niq0',
# 'isNote': False,
# 'message': '{"contentList":"testtesttest<br>","attachmentList":[]}',
# 'msgType': 'newstext',
# 'platform': 1,
# 'roomId': '',
# 'siteId': '7238',
# 'uType': 1,
# 'sendTime': int(time.time()),
# 'uid': '36202',
# 'username': 'zhs'
# },
# 'seq' : str(random.randint(1000000000000000, 9999999999999999))
# })
# webs = ws(url=f'wss://atestws.sealapps.com/ws?platform=1&authorization=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE3MDk3NzY4OTgsImlzcyI6IndpbGxkZXNrLmNvbSIsInN1YiI6IkxvZ2luQXV0aFRva2VuIiwidXNlcl9pbmZvIjp7ImNvbXBhbnlJZCI6NjA1OSwiYnJhbmRJZCI6NjA1MiwiY3VzdG9tZXJTZXJ2aWNlSWQiOjU3NDYsImxvZ2luUm9sZSI6MSwiY2xpZW50SWQiOiJjbmtpMDBnNmxnY2M3MWNxZHM1ZyIsImxvZ2luVGltZSI6MTcwOTc3Njg5OH0sImtleSI6ImFsbHBsYXRmb3JtLXdpbGxkZXNrLXVzZXIifQ.EllFwfirKFTB88BzBigz64tBz1SJQFFwFp0tWlQzWVU&t=1709802959971',attr='willdesk')
# await asyncio.create_task(webs.send_message(message=data))
# msg = await asyncio.create_task(webs.receive_message())
# print(msg)
#
# loop = asyncio.get_event_loop()
# loop.run_until_complete(main())
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment