Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
W
willdesk_apiauto
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
29
Issues
29
List
Boards
Labels
Service Desk
Milestones
Merge Requests
29
Merge Requests
29
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Metrics
Incidents
Environments
Packages & Registries
Packages & Registries
Package Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
桦生 詹
willdesk_apiauto
Commits
2a99051d
Commit
2a99051d
authored
Mar 12, 2024
by
zhanhuasheng
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
增加readMsg操作
parent
d11bbce8
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
66 additions
and
79 deletions
+66
-79
Utils/websocket_handler.py
Utils/websocket_handler.py
+66
-79
No files found.
Utils/websocket_handler.py
View file @
2a99051d
...
...
@@ -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
el
se
:
el
if
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,24 +107,25 @@ 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
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'
:
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
''
if
ws
.
client_id
==
''
:
await
ws
.
get_client_id
()
client_id
=
ws
.
client_id
if
ws
.
attr
==
'willdesk'
:
data
=
{
'avatar'
:
avatar
,
...
...
@@ -155,8 +159,21 @@ async def ws_send_handler(case,ws):
'uid'
:
str
(
fromId
),
# userId
'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
=
{
'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<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())
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment