nonebot / nonebot2

跨平台 Python 异步聊天机器人框架 / Asynchronous multi-platform chatbot framework written in Python
https://nonebot.dev
MIT License
5.84k stars 551 forks source link

Bug: 飞书API错误 #523

Closed d-w-x closed 2 years ago

d-w-x commented 3 years ago

描述问题:

使用 nonebot-adapter-feishu 时,发送私聊消息错误。

如何复现?

  1. 使用 nonebot-adapter-feishu 构建一个机器人。
  2. 向它发送 私聊 消息。
  3. 错误码 <NetWorkError message=HTTP request received unexpected status code: 400 response body: {"code":99992364,"msg":"user id cross tenant","data":{}}>

错误报告

参见飞书文档,发送消息时应该用 union_id 而不是 open_id,追踪发送调用栈发现:

https://github.com/nonebot/nonebot2/blob/b66a4df4c52f7a2c9725f260d9337c7d93b3eb30/packages/nonebot-adapter-feishu/nonebot/adapters/feishu/bot.py#L330-L336

这里调用了 event.get_user_id(),而:

https://github.com/nonebot/nonebot2/blob/b66a4df4c52f7a2c9725f260d9337c7d93b3eb30/packages/nonebot-adapter-feishu/nonebot/adapters/feishu/event.py#L200-L202

这里却返回了用户的 open_id 造成错误。实际应该为:

    @overrides(Event)
    def get_user_id(self) -> str:
        return self.event.sender.sender_id.union_id

这个是发送私聊消息错误的解决方案, 请评估 get_user_id 的修改对项目其他部分造成的影响。

yanyongyu commented 3 years ago

平台适配作者最近有点忙,可能要过一段时间

StarHeartHunt commented 3 years ago

我这边有在考虑 event.get_user_id() 同时返回三个 ID,方便开发者基于 NoneBot2 的事件处理延伸出去做多样化需求(比如需要多服务用户互认的场景,做机器人前端这样的),但是这个星期比较忙没空排查一遍会有哪些影响,这部分可能会延后处理。

StarHeartHunt commented 2 years ago

翻了一下飞书文档,想起来当时为啥用 open_id 了,是因为富文本消息里的 at 有且只有 open_id,目前我给 MessageEvent 加了一个拓展方法 get_all_user_id 来获取其他用户 ID。

d-w-x commented 2 years ago

感谢回复。

StarHeartHunt commented 2 years ago

提供的 call_api 强制将 response 的返回值进行 json 序列化

call_api 没有这么做,我估计你看差到 send 去了 image

d-w-x commented 2 years ago

https://github.com/nonebot/nonebot2/blob/20d0ecfc7fa690ffb616665c1613bce6647929a9/packages/nonebot-adapter-feishu/nonebot/adapters/feishu/bot.py#L279-L290