From 2a99051df4dd9fe709ae91d9a0257638037cc485 Mon Sep 17 00:00:00 2001 From: zhanhuasheng Date: Tue, 12 Mar 2024 16:28:58 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0readMsg=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Utils/websocket_handler.py | 145 +++++++++++++++++-------------------- 1 file changed, 66 insertions(+), 79 deletions(-) diff --git a/Utils/websocket_handler.py b/Utils/websocket_handler.py index b13c889..7bb2674 100644 --- a/Utils/websocket_handler.py +++ b/Utils/websocket_handler.py @@ -14,7 +14,6 @@ class ws: self.loop = asyncio.get_event_loop() self.client_id = '' self.attr = attr - async def get_client_id(self): if not self.websocket: self.websocket = await websockets.connect(self.url) @@ -28,6 +27,7 @@ class ws: self.client_id = enter_res['data']['cliend'] else: # action是enter的情况 self.client_id = enter_res['data']['clientId'] + setattr(Global,self.attr+'_clientId',self.client_id) async def connect(self): try: self.websocket = await websockets.connect(self.url) @@ -81,12 +81,15 @@ async def ws_action(ws,case): :param case: 传入用例数据 ''' action = case['action'] #操作 - if action == 'sendmsg': - response = await ws_send_handler(case=case,ws=ws) + if action in ['sendmsg','readMsg']: + response = await ws_send_handler(case=case,ws=ws,action=action) if response.get('code'): assert response['code'] == 200 - else: + elif response.get('response'): assert response['response']['code'] == 200 + else: + if response['action'] == 'roomTips': + assert response['brandId'] == getattr(Global,'brandId') # type = case['data']['type'] # if type in ['emoji','file','link','rate','product']: # if type not in ['file','rate','emoji']: #type等于file的时候,后端返回的action,willdesk端是sendmsg,c端又是readMsg,干脆不作判断了 @@ -104,59 +107,73 @@ async def ws_action(ws,case): msg = json.loads(await ws.receive_message()) ws_assert(expected=expected,msg=msg) -async def ws_send_handler(case,ws): +async def ws_send_handler(case,ws,action): data = case['data'] # 消息数据 - content = getattr(livechatData,data['type']) #根据type取到对应格式的content - - if data['type'] != 'rate': - msgType = 'newstext' - else: - 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') - 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 '' + 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 ws.attr == 'willdesk': - data = { - 'avatar': avatar, - 'clientId': client_id, - 'fromId': str(fromId), # c端userId - 'isNote': isNote, - 'message': content, - 'msgType': msgType, - 'platform': 1, - 'roomChannelType': 'wk', - 'roomId': roomId, - 'roomPlatform': 'willdesk', - 'siteId': str(siteId), # 店铺id - 'uType': 2, - 'uid': str(uid), # userId - 'username': username # 品牌名称 - } - else: + if action == 'sendmsg': + content = getattr(livechatData, data['type']) # 根据type取到对应格式的content + if data['type'] != 'rate': + msgType = 'newstext' + else: + msgType = 'rate' + username = data['username'] if data.get('username') else getattr(Global, 'name') + isNote = data['isNote'] if data.get('isNote') else False + avatar = 'https://img.willdesk.com/${picUrl}' if ws.attr == 'willdesk' else '' + + if ws.attr == 'willdesk': + data = { + 'avatar': avatar, + 'clientId': client_id, + 'fromId': str(fromId), # c端userId + 'isNote': isNote, + 'message': content, + 'msgType': msgType, + 'platform': 1, + 'roomChannelType': 'wk', + 'roomId': roomId, + 'roomPlatform': 'willdesk', + 'siteId': str(siteId), # 店铺id + 'uType': 2, + 'uid': str(uid), # userId + 'username': username # 品牌名称 + } + else: + data = { + 'avator': avatar, + 'clientId': client_id, + 'isNote': isNote, + 'message': content, + 'msgType': msgType, + 'pageUrl': 'https://zhs-test.myshopify.com/', + 'platform': 1, + 'roomId': roomId, + 'sendTime': int(time.time()), + 'siteId': str(siteId), # 店铺id + 'uType': 1, + 'uid': str(fromId), # userId + 'username': Global.customerName # 品牌名称 + } + elif action == 'readMsg': + msgId = data['msgId'], + msgTime = data['msgTime'] data = { - 'avator': avatar, - 'clientId': client_id, - 'isNote': isNote, - 'message': content, - 'msgType': msgType, - 'pageUrl': 'https://zhs-test.myshopify.com/', - 'platform': 1, - 'roomId': roomId, - 'sendTime': int(time.time()), - 'siteId': str(siteId), # 店铺id - 'uType': 1, - 'uid': str(fromId), # userId - 'username': Global.customerName # 品牌名称 + '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 = { - 'action': 'sendmsg', + 'action': action, 'data': data, 'seq': str(random.randint(1000000000000000, 9999999999999999)) } @@ -204,34 +221,4 @@ async def ws_run(ws,case): await asyncio.wait_for(task,timeout=10) # 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
","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()) -- GitLab