Merge pull request #524 from nonebot/fix/feishu-private-msg

🐛 fix feishu private message event response
This commit is contained in:
StarHeart 2021-09-17 20:46:27 +08:00 committed by GitHub
commit 55a402203e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 61 additions and 27 deletions

View File

@ -533,3 +533,19 @@ Event 基类。提供获取关键信息的方法,其余信息可直接获取
* `bool` * `bool`
## _class_ `MessageTemplate`
基类:`string.Formatter`, `Generic`[`nonebot.adapters._template.TM`]
消息模板格式化实现类
### `format(*args, **kwargs)`
* **说明**
根据模板和参数生成消息对象

View File

@ -30,7 +30,7 @@ NoneBot.adapters 模块
:special-members: __init__ :special-members: __init__
:show-inheritance: :show-inheritance:
.. automodule:: nonebot.adapters._formatter .. automodule:: nonebot.adapters._template
:members: :members:
:private-members: :private-members:
:special-members: __init__ :special-members: __init__

View File

@ -1,21 +1,22 @@
import json import json
import re import re
from typing import TYPE_CHECKING, Any, Dict, Optional, Tuple, Union from typing import (TYPE_CHECKING, Any, AsyncIterable, Dict, Iterable,
Optional, Tuple, Union)
import httpx import httpx
from aiocache import Cache, cached from aiocache import Cache, cached
from aiocache.serializers import PickleSerializer from aiocache.serializers import PickleSerializer
from nonebot.log import logger
from nonebot.utils import escape_tag
from nonebot.typing import overrides
from nonebot.message import handle_event
from nonebot.adapters import Bot as BaseBot from nonebot.adapters import Bot as BaseBot
from nonebot.drivers import Driver, HTTPRequest, HTTPResponse from nonebot.drivers import Driver, HTTPRequest, HTTPResponse
from nonebot.log import logger
from nonebot.message import handle_event
from nonebot.typing import overrides
from nonebot.utils import escape_tag
from .config import Config as FeishuConfig from .config import Config as FeishuConfig
from .event import (Event, GroupMessageEvent, MessageEvent, PrivateMessageEvent, from .event import (Event, GroupMessageEvent, MessageEvent,
get_event_model) PrivateMessageEvent, get_event_model)
from .exception import ActionFailed, ApiNotAvailable, NetworkError from .exception import ActionFailed, ApiNotAvailable, NetworkError
from .message import Message, MessageSegment, MessageSerializer from .message import Message, MessageSegment, MessageSerializer
from .utils import AESCipher, log from .utils import AESCipher, log
@ -101,7 +102,7 @@ def _check_nickname(bot: "Bot", event: "Event"):
first_msg_seg.data["text"] = first_text[m.end():] first_msg_seg.data["text"] = first_text[m.end():]
def _handle_api_result(result: Optional[Dict[str, Any]]) -> Any: def _handle_api_result(result: Union[Optional[Dict[str, Any]], str, bytes, Iterable[bytes], AsyncIterable[bytes]]) -> Any:
""" """
:说明: :说明:
@ -123,6 +124,8 @@ def _handle_api_result(result: Optional[Dict[str, Any]]) -> Any:
if result.get("code") != 0: if result.get("code") != 0:
raise ActionFailed(**result) raise ActionFailed(**result)
return result.get("data") return result.get("data")
else:
return result
class Bot(BaseBot): class Bot(BaseBot):
@ -289,7 +292,10 @@ class Bot(BaseBot):
params=data.get("query", {}), params=data.get("query", {}),
headers=headers)) headers=headers))
if 200 <= response.status_code < 300: if 200 <= response.status_code < 300:
if response.headers["content-type"].startswith("application/json"):
result = response.json() result = response.json()
else:
result = response.content
return _handle_api_result(result) return _handle_api_result(result)
raise NetworkError(f"HTTP request received unexpected " raise NetworkError(f"HTTP request received unexpected "
f"status code: {response.status_code} " f"status code: {response.status_code} "
@ -331,16 +337,16 @@ class Bot(BaseBot):
msg = message if isinstance(message, Message) else Message(message) msg = message if isinstance(message, Message) else Message(message)
if isinstance(event, GroupMessageEvent): if isinstance(event, GroupMessageEvent):
receive_id, receive_id_type = event.event.message.chat_id, 'chat_id' receive_id, receive_id_type = event.event.message.chat_id, "chat_id"
elif isinstance(event, PrivateMessageEvent): elif isinstance(event, PrivateMessageEvent):
receive_id, receive_id_type = event.get_user_id(), 'union_id' receive_id, receive_id_type = event.get_user_id(), "open_id"
else: else:
raise ValueError( raise ValueError(
"Cannot guess `receive_id` and `receive_id_type` to reply!") "Cannot guess `receive_id` and `receive_id_type` to reply!")
at_sender = at_sender and bool(event.get_user_id()) at_sender = at_sender and bool(event.get_user_id())
if at_sender and receive_id_type != "union_id": if at_sender and receive_id_type == "chat_id":
msg = MessageSegment.at(event.get_user_id()) + " " + msg msg = MessageSegment.at(event.get_user_id()) + " " + msg
msg_type, content = MessageSerializer(msg).serialize() msg_type, content = MessageSerializer(msg).serialize()

View File

@ -201,6 +201,9 @@ class MessageEvent(Event):
def get_user_id(self) -> str: def get_user_id(self) -> str:
return self.event.sender.sender_id.open_id return self.event.sender.sender_id.open_id
def get_all_user_id(self) -> UserId:
return self.event.sender.sender_id
@overrides(Event) @overrides(Event)
def get_session_id(self) -> str: def get_session_id(self) -> str:
return f"{self.event.message.chat_type}_{self.event.message.chat_id}_{self.get_user_id()}" return f"{self.event.message.chat_type}_{self.event.message.chat_id}_{self.get_user_id()}"
@ -225,11 +228,11 @@ class NoticeEvent(Event):
@overrides(Event) @overrides(Event)
def get_event_name(self) -> str: def get_event_name(self) -> str:
raise ValueError("Event has no name!") return self.header.event_type
@overrides(Event) @overrides(Event)
def get_event_description(self) -> str: def get_event_description(self) -> str:
raise ValueError("Event has no description!") return str(self.dict())
@overrides(Event) @overrides(Event)
def get_message(self) -> Message: def get_message(self) -> Message:

31
poetry.lock generated
View File

@ -25,11 +25,11 @@ pycares = ">=4.0.0"
[[package]] [[package]]
name = "aiofiles" name = "aiofiles"
version = "0.6.0" version = "0.7.0"
description = "File support for asyncio." description = "File support for asyncio."
category = "main" category = "main"
optional = false optional = false
python-versions = "*" python-versions = ">=3.6,<4.0"
[[package]] [[package]]
name = "aiohttp" name = "aiohttp"
@ -508,15 +508,15 @@ url = "packages/nonebot-adapter-mirai"
[[package]] [[package]]
name = "nonebot-plugin-test" name = "nonebot-plugin-test"
version = "0.2.0" version = "0.3.0"
description = "Test frontend for nonebot v2+" description = "Test frontend for nonebot v2+"
category = "dev" category = "dev"
optional = false optional = false
python-versions = ">=3.7,<4.0" python-versions = ">=3.7.3,<4.0.0"
[package.dependencies] [package.dependencies]
aiofiles = ">=0.6.0,<0.7.0" aiofiles = ">=0.7.0,<0.8.0"
nonebot2 = ">=2.0.0-alpha.9,<3.0.0" nonebot2 = ">=2.0.0-alpha.14,<3.0.0"
python-socketio = ">=4.6.1,<5.0.0" python-socketio = ">=4.6.1,<5.0.0"
[[package]] [[package]]
@ -1073,7 +1073,7 @@ quart = ["Quart"]
[metadata] [metadata]
lock-version = "1.1" lock-version = "1.1"
python-versions = "^3.7.3" python-versions = "^3.7.3"
content-hash = "84b334eafa07181a2b4367200ddf7c33658e5b40eae6dfc5e84a1bc06eb271f4" content-hash = "31197077ed4417f1e8f24d309d02e36e5ab5c9492c6dcaf2261d13645161cb76"
[metadata.files] [metadata.files]
aiocache = [ aiocache = [
@ -1085,8 +1085,8 @@ aiodns = [
{file = "aiodns-3.0.0.tar.gz", hash = "sha256:946bdfabe743fceeeb093c8a010f5d1645f708a241be849e17edfb0e49e08cd6"}, {file = "aiodns-3.0.0.tar.gz", hash = "sha256:946bdfabe743fceeeb093c8a010f5d1645f708a241be849e17edfb0e49e08cd6"},
] ]
aiofiles = [ aiofiles = [
{file = "aiofiles-0.6.0-py3-none-any.whl", hash = "sha256:bd3019af67f83b739f8e4053c6c0512a7f545b9a8d91aaeab55e6e0f9d123c27"}, {file = "aiofiles-0.7.0-py3-none-any.whl", hash = "sha256:c67a6823b5f23fcab0a2595a289cec7d8c863ffcb4322fb8cd6b90400aedfdbc"},
{file = "aiofiles-0.6.0.tar.gz", hash = "sha256:e0281b157d3d5d59d803e3f4557dcc9a3dff28a4dd4829a9ff478adae50ca092"}, {file = "aiofiles-0.7.0.tar.gz", hash = "sha256:a1c4fc9b2ff81568c83e21392a82f344ea9d23da906e4f6a52662764545e19d4"},
] ]
aiohttp = [ aiohttp = [
{file = "aiohttp-3.7.4.post0-cp36-cp36m-macosx_10_14_x86_64.whl", hash = "sha256:3cf75f7cdc2397ed4442594b935a11ed5569961333d49b7539ea741be2cc79d5"}, {file = "aiohttp-3.7.4.post0-cp36-cp36m-macosx_10_14_x86_64.whl", hash = "sha256:3cf75f7cdc2397ed4442594b935a11ed5569961333d49b7539ea741be2cc79d5"},
@ -1172,6 +1172,14 @@ brotlipy = [
{file = "brotlipy-0.7.0-cp34-cp34m-manylinux1_x86_64.whl", hash = "sha256:79aaf217072840f3e9a3b641cccc51f7fc23037496bd71e26211856b93f4b4cb"}, {file = "brotlipy-0.7.0-cp34-cp34m-manylinux1_x86_64.whl", hash = "sha256:79aaf217072840f3e9a3b641cccc51f7fc23037496bd71e26211856b93f4b4cb"},
{file = "brotlipy-0.7.0-cp34-cp34m-win32.whl", hash = "sha256:a07647886e24e2fb2d68ca8bf3ada398eb56fd8eac46c733d4d95c64d17f743b"}, {file = "brotlipy-0.7.0-cp34-cp34m-win32.whl", hash = "sha256:a07647886e24e2fb2d68ca8bf3ada398eb56fd8eac46c733d4d95c64d17f743b"},
{file = "brotlipy-0.7.0-cp34-cp34m-win_amd64.whl", hash = "sha256:c6cc0036b1304dd0073eec416cb2f6b9e37ac8296afd9e481cac3b1f07f9db25"}, {file = "brotlipy-0.7.0-cp34-cp34m-win_amd64.whl", hash = "sha256:c6cc0036b1304dd0073eec416cb2f6b9e37ac8296afd9e481cac3b1f07f9db25"},
{file = "brotlipy-0.7.0-cp35-abi3-macosx_10_9_x86_64.whl", hash = "sha256:382971a641125323e90486244d6266ffb0e1f4dd920fbdcf508d2a19acc7c3b3"},
{file = "brotlipy-0.7.0-cp35-abi3-manylinux1_i686.whl", hash = "sha256:82f61506d001e626ec3a1ac8a69df11eb3555a4878599befcb672c8178befac8"},
{file = "brotlipy-0.7.0-cp35-abi3-manylinux1_x86_64.whl", hash = "sha256:7ff18e42f51ebc9d9d77a0db33f99ad95f01dd431e4491f0eca519b90e9415a9"},
{file = "brotlipy-0.7.0-cp35-abi3-manylinux2010_i686.whl", hash = "sha256:8ef230ca9e168ce2b7dc173a48a0cc3d78bcdf0bd0ea7743472a317041a4768e"},
{file = "brotlipy-0.7.0-cp35-abi3-manylinux2010_x86_64.whl", hash = "sha256:b7cf5bb69e767a59acc3da0d199d4b5d0c9fed7bef3ffa3efa80c6f39095686b"},
{file = "brotlipy-0.7.0-cp35-abi3-manylinux2014_aarch64.whl", hash = "sha256:e5c549ae5928dda952463196180445c24d6fad2d73cb13bd118293aced31b771"},
{file = "brotlipy-0.7.0-cp35-abi3-win32.whl", hash = "sha256:79ab3bca8dd12c17e092273484f2ac48b906de2b4828dcdf6a7d520f99646ab3"},
{file = "brotlipy-0.7.0-cp35-abi3-win_amd64.whl", hash = "sha256:ac1d66c9774ee62e762750e399a0c95e93b180e96179b645f28b162b55ae8adc"},
{file = "brotlipy-0.7.0-cp35-cp35m-macosx_10_6_intel.whl", hash = "sha256:07194f4768eb62a4f4ea76b6d0df6ade185e24ebd85877c351daa0a069f1111a"}, {file = "brotlipy-0.7.0-cp35-cp35m-macosx_10_6_intel.whl", hash = "sha256:07194f4768eb62a4f4ea76b6d0df6ade185e24ebd85877c351daa0a069f1111a"},
{file = "brotlipy-0.7.0-cp35-cp35m-manylinux1_i686.whl", hash = "sha256:7e31f7adcc5851ca06134705fcf3478210da45d35ad75ec181e1ce9ce345bb38"}, {file = "brotlipy-0.7.0-cp35-cp35m-manylinux1_i686.whl", hash = "sha256:7e31f7adcc5851ca06134705fcf3478210da45d35ad75ec181e1ce9ce345bb38"},
{file = "brotlipy-0.7.0-cp35-cp35m-manylinux1_x86_64.whl", hash = "sha256:9448227b0df082e574c45c983fa5cd4bda7bfb11ea6b59def0940c1647be0c3c"}, {file = "brotlipy-0.7.0-cp35-cp35m-manylinux1_x86_64.whl", hash = "sha256:9448227b0df082e574c45c983fa5cd4bda7bfb11ea6b59def0940c1647be0c3c"},
@ -1183,6 +1191,7 @@ brotlipy = [
{file = "brotlipy-0.7.0-cp36-cp36m-win32.whl", hash = "sha256:2e5c64522364a9ebcdf47c5744a5ddeb3f934742d31e61ebfbbc095460b47162"}, {file = "brotlipy-0.7.0-cp36-cp36m-win32.whl", hash = "sha256:2e5c64522364a9ebcdf47c5744a5ddeb3f934742d31e61ebfbbc095460b47162"},
{file = "brotlipy-0.7.0-cp36-cp36m-win_amd64.whl", hash = "sha256:09ec3e125d16749b31c74f021aba809541b3564e5359f8c265cbae442810b41a"}, {file = "brotlipy-0.7.0-cp36-cp36m-win_amd64.whl", hash = "sha256:09ec3e125d16749b31c74f021aba809541b3564e5359f8c265cbae442810b41a"},
{file = "brotlipy-0.7.0-cp37-cp37m-macosx_10_14_x86_64.whl", hash = "sha256:4e4638b49835d567d447a2cfacec109f9a777f219f071312268b351b6839436d"}, {file = "brotlipy-0.7.0-cp37-cp37m-macosx_10_14_x86_64.whl", hash = "sha256:4e4638b49835d567d447a2cfacec109f9a777f219f071312268b351b6839436d"},
{file = "brotlipy-0.7.0-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl", hash = "sha256:5664fe14f3a613431db622172bad923096a303d3adce55536f4409c8e2eafba4"},
{file = "brotlipy-0.7.0-cp37-cp37m-win_amd64.whl", hash = "sha256:1379347337dc3d20b2d61456d44ccce13e0625db2611c368023b4194d5e2477f"}, {file = "brotlipy-0.7.0-cp37-cp37m-win_amd64.whl", hash = "sha256:1379347337dc3d20b2d61456d44ccce13e0625db2611c368023b4194d5e2477f"},
{file = "brotlipy-0.7.0-cp38-cp38-manylinux1_i686.whl", hash = "sha256:22a53ccebcce2425e19f99682c12be510bf27bd75c9b77a1720db63047a77554"}, {file = "brotlipy-0.7.0-cp38-cp38-manylinux1_i686.whl", hash = "sha256:22a53ccebcce2425e19f99682c12be510bf27bd75c9b77a1720db63047a77554"},
{file = "brotlipy-0.7.0-cp38-cp38-manylinux1_x86_64.whl", hash = "sha256:4bac11c1ffba9eaa2894ec958a44e7f17778b3303c2ee9f99c39fcc511c26668"}, {file = "brotlipy-0.7.0-cp38-cp38-manylinux1_x86_64.whl", hash = "sha256:4bac11c1ffba9eaa2894ec958a44e7f17778b3303c2ee9f99c39fcc511c26668"},
@ -1472,8 +1481,8 @@ nonebot-adapter-ding = []
nonebot-adapter-feishu = [] nonebot-adapter-feishu = []
nonebot-adapter-mirai = [] nonebot-adapter-mirai = []
nonebot-plugin-test = [ nonebot-plugin-test = [
{file = "nonebot-plugin-test-0.2.0.tar.gz", hash = "sha256:c9ee997c5c96160de4af02d10a7c6301b3fc4e942df7e70906df0534606ea23b"}, {file = "nonebot-plugin-test-0.3.0.tar.gz", hash = "sha256:6cea9342f4df69a73b9aa27a3eaa22a08c6e54b675e2e6638b4b3fc8568cd6fe"},
{file = "nonebot_plugin_test-0.2.0-py3-none-any.whl", hash = "sha256:75cd18cc282815a03250bb86c7d2a8d6a66a5064ac335bedc9a3e268a1e7dd13"}, {file = "nonebot_plugin_test-0.3.0-py3-none-any.whl", hash = "sha256:edb880340436323ccd0a13b31d48975136b6bdc71daa178601c4b05b068cc73e"},
] ]
packaging = [ packaging = [
{file = "packaging-21.0-py3-none-any.whl", hash = "sha256:c86254f9220d55e31cc94d69bade760f0847da8000def4dfe1c6b872fd14ff14"}, {file = "packaging-21.0-py3-none-any.whl", hash = "sha256:c86254f9220d55e31cc94d69bade760f0847da8000def4dfe1c6b872fd14ff14"},

View File

@ -38,7 +38,7 @@ aiohttp = { version = "^3.7.4", extras = ["speedups"], optional = true }
[tool.poetry.dev-dependencies] [tool.poetry.dev-dependencies]
yapf = "^0.31.0" yapf = "^0.31.0"
sphinx = "^4.1.1" sphinx = "^4.1.1"
nonebot-plugin-test = "^0.2.0" nonebot-plugin-test = "^0.3.0"
nonebot-adapter-cqhttp = { path = "./packages/nonebot-adapter-cqhttp", develop = true } nonebot-adapter-cqhttp = { path = "./packages/nonebot-adapter-cqhttp", develop = true }
nonebot-adapter-ding = { path = "./packages/nonebot-adapter-ding", develop = true } nonebot-adapter-ding = { path = "./packages/nonebot-adapter-ding", develop = true }
nonebot-adapter-mirai = { path = "./packages/nonebot-adapter-mirai", develop = true } nonebot-adapter-mirai = { path = "./packages/nonebot-adapter-mirai", develop = true }