Skip to content

Commit a4eff3c

Browse files
committed
fix: session.send_msg
1 parent be6a369 commit a4eff3c

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

bilibili_api/data/api/session.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
"url": "https://api.vc.bilibili.com/web_im/v1/web_im/send_msg",
9090
"method": "POST",
9191
"verify": true,
92+
"wbi": true,
9293
"data": {
9394
"msg[sender_uid]": "int: 自己的 UID",
9495
"msg[receiver_id]": "int: 对方 UID",
@@ -97,6 +98,10 @@
9798
"msg[msg_status]": "const int: 0",
9899
"msg[content]": "str: 消息内容"
99100
},
101+
"params": {
102+
"sender_uid": "int: 自己的 UID",
103+
"receiver_id": "int: 对方 UID"
104+
},
100105
"comment": "给用户发信息"
101106
}
102107
}

bilibili_api/session.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,8 @@ async def send_msg(
363363
"""
364364
给用户发送私聊信息。目前仅支持纯文本。
365365
366+
调用 API 需要发送者用户的 UID,可将此携带在凭据类的 DedeUserID 字段,不携带模块将自动获取对应 UID。
367+
366368
Args:
367369
credential (Credential) : 凭证
368370
@@ -379,8 +381,11 @@ async def send_msg(
379381
credential.raise_for_no_bili_jct()
380382

381383
api = API["operate"]["send_msg"]
382-
self_info = await get_self_info(credential)
383-
sender_uid = self_info["mid"]
384+
if credential.has_dedeuserid() and int(credential.dedeuserid) != 0:
385+
sender_uid = int(credential.dedeuserid)
386+
else:
387+
self_info = await get_self_info(credential)
388+
sender_uid = self_info["mid"]
384389

385390
if msg_type == EventType.TEXT:
386391
real_content = json.dumps({"content": content})
@@ -416,7 +421,17 @@ async def send_msg(
416421
"build": 0,
417422
"mobi_app": "web",
418423
}
419-
return await Api(**api, credential=credential).update_data(**data).result
424+
query = {
425+
"w_sender_uid": sender_uid,
426+
"w_receiver_id": receiver_id,
427+
}
428+
429+
return (
430+
await Api(**api, credential=credential)
431+
.update_params(**query)
432+
.update_data(**data)
433+
.result
434+
)
420435

421436

422437
class Session(AsyncEvent):

docs/modules/session.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,8 @@ from bilibili_api import session
348348

349349
给用户发送私聊信息。目前仅支持纯文本。
350350

351+
调用 API 需要发送者用户的 UID,可将此携带在凭据类的 DedeUserID 字段,不携带模块将自动获取对应 UID。
352+
351353

352354
| name | type | description |
353355
| - | - | - |

0 commit comments

Comments
 (0)