Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于在写onebot12协议java-sdk的一些想法 #248

Open
eiriksgata opened this issue Oct 27, 2023 Discussed in #247 · 0 comments
Open

关于在写onebot12协议java-sdk的一些想法 #248

eiriksgata opened this issue Oct 27, 2023 Discussed in #247 · 0 comments

Comments

@eiriksgata
Copy link

Discussed in https://github.com/orgs/botuniverse/discussions/247

Originally posted by eiriksgata October 25, 2023
讨论1: 关于事件的数据拓展字段。

我们都知道在12标准协议中,必填字段有 id、time、type、detail_type、sub_type ,其余如果想要进行拓展,那么其拓展的字段是位于 当前 Event 所在的并级下,我认为这是一种不合理的设计。

如文档中给出的例子:

{
    "id": "b6e65187-5ac0-489c-b431-53078e9d2bbb",
    "self": {
        "platform": "qq",
        "user_id": "123234"
    },
    "time": 1632847927.599013,
    "type": "message",
    "detail_type": "private",
    "sub_type": "",
    "message_id": "6283",
    "message": [
        {
            "type": "text",
            "data": {
                "text": "OneBot is not a bot"
            }
        }
    ],
    "alt_message": "OneBot is not a bot",
    "user_id": "123456788",
    "qq.nickname": "海阔天空"
}

根据例子来看,其事件是往机器人平台发送一条私聊消息,其中包含 message、 user_id 等相关字段,这些字段都是位于 Event 所在的当前层级,对于常规的弱语言(形如脚本语言)来说,json增加字段确实没什么问题,但是对于强语言类型来说,我们必然是要定义一个Class 来 实现实体类,或者是 ts 定义一个 type | interface 来增强语言类型。

想message、user_id 这种拓展字段 ,本身应该是填写在 Event 所在的 某个字段下级,例如下面例子:

{
  "id": "b6e65187-5ac0-489c-b431-53078e9d2bbb",
  "self": {
    "platform": "qq",
    "user_id": "123234"
  },
  "time": 1632847927.599013,
  "type": "message",
  "detail_type": "private",
  "sub_type": "",
  "content": {
    "message_id": "6283",
    "message": [
      {
        "type": "text",
        "data": {
          "text": "OneBot is not a bot"
        }
      }
    ],
    "alt_message": "OneBot is not a bot",
    "user_id": "123456788",
    "qq.nickname": "海阔天空"
  }
}

上述例子中 将 message 、user_id等数据 ,放入 content 中, 随着你 type 不同而 对 content 中的字段而不同。

因此我们在写 实体类时,可以采用 泛型 的形式对 json 进行轻松的反序列化,同时也增强了语言的可读性。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant