OneBot API Specification

repository·main·Indexed 24 days ago

https://github.com/botuniverse/onebot

A standardized API specification designed to unify chat bot development across various platforms, enabling code portability. It defines communication protocols including HTTP, HTTP Webhook, WebSocket, and Reverse WebSocket, providing guidelines for authentication via access tokens, event handling, and request/response formatting using JSON and MessagePack.

Tokens
23.3K
Snippets
78
Records
137
Agent score
83%

What's inside OneBot

  1. What is OneBot?

    main
    OneBot is a standardized application programming interface (API) for chat bots. It aims to unify the development interfaces across different chat platforms, allowing developers to write business logic once and deploy it across multiple robot platforms.
  2. What is the OneBot standard?

    main

    OneBot is an application interface standard for chat bots. It provides an abstraction for the APIs used to develop chat bots, standardizing communication methods, data formats, and fields.

    By following this standard, developers can write their business logic once and deploy it across multiple different chat platforms.

    Note: OneBot is a specification (a document), not a centralized software implementation. Implementations (such as LibOneBot) are created by the community for various languages, runtimes, and platforms.

  3. Understand the OneBot Event data structure

    main

    An Event is an object pushed by OneBot to an application. It represents data spontaneously generated by OneBot or received from a robot platform.

    Every event must be an object and must contain the following mandatory fields to be considered valid. If any of these fields are missing or have incorrect types, the data should not be treated as a valid event.

  4. Requirements for implementing OneBot Message Segments

    main

    When implementing OneBot message segments, developers should follow these guidelines for parameter handling and error reporting:

    1. Parameter Support: If a standard message segment is implemented, it is recommended to support all standard defined parameters.
    2. Error Handling for Segments:
      • Unsupported Segment: If a message segment cannot be sent, return error code 10005 (Unsupported Segment).
      • Unsupported Segment Data: If a segment is implemented but specific parameters cannot be supported, return error code 10007 (Unsupported Segment Data). If the parameter is non-essential, it may be ignored.
      • Partial/Total Send Failure: If the robot platform prevents a segment (or the entire message) from being sent, it is recommended to return a 34xxx error code indicating a message sending failure. If failure detection is difficult, the error may be ignored.
  5. Authenticate HTTP Requests via access_token

    main

    If an access_token is configured and is not an empty string, the OneBot implementation must perform authentication using the following priority:

    1. Authorization Header: Check for the Authorization header. The value must exactly match Bearer <access_token> (whitespace trimming is not required on either side).
    2. URL Query Parameter: If the header is missing, check for an access_token query parameter. Its value must exactly match <access_token>.

    If neither is present or they do not match, the request must fail with an HTTP 401 Unauthorized status code.

  6. Understand the OneBot ecosystem architecture

    main

    The OneBot ecosystem consists of three primary layers that interact via the OneBot Standard:

    1. Bot Platform: The chat software providing the API (e.g., telegram, discord, qq).
    2. OneBot Implementation (Implementation End): A program that connects to a Bot Platform and provides an interface that conforms to the OneBot Standard. It pushes events to applications.
    3. OneBot Application (Application End): A program that implements business logic by interacting with an Implementation End according to the OneBot Standard. It calls actions to control the bot.

    Developers typically build OneBot Applications using a OneBot SDK (like NoneBot or Koishi) to avoid handling low-level communication logic.

  7. Core OneBot communication concepts: Events, Actions, and Messages

    main

    OneBot communication is built around three core data types:

    • Event: Something that happened on the platform or within the implementation. Implementations push events to applications. Types include:
      • Meta Event: e.g., OneBot Heartbeat.
      • Message Event: e.g., receiving a private message.
      • Notice Event: e.g., a group member leaving.
      • Request Event: e.g., a friend request.
    • Action: An interface used by the application to control the bot or query information. Applications call actions. Actions may require Action Params (or Params).
    • Message: Data used to represent chat content. In events, it is an array of Message Segments. In action parameters, it can be a string (simplified plain text), a single segment, or an array of segments.
    • Message Segment (Segment): A component of a message, used for mixed media (e.g., an image segment or a text segment).
  8. Use WebSocket for OneBot communication

    main

    WebSocket is recommended when the application can actively access the OneBot implementation (e.g., via public IP or local network), when an unknown number of applications need to connect to a single OneBot instance, or when high performance is required.

    To implement WebSocket support, the OneBot implementation should start a WebSocket server listening on a configured <host>:<port> and accept connection requests at the / path. The server pushes events to the application and parses incoming messages as action requests.

  9. Understand the structure of `message.private` events

    main

    The message.private event is triggered when a user sends a private message. These events are based on the OneBot Connect - Event specification, where the top-level type field must be message and the detail_type field must be private.

    When implementing handlers for these events, note that if a sub_type is not explicitly mentioned in documentation, its value should be an empty string ("").

  10. OneBot Connect (OBC) communication methods and protocols

    main

    OneBot Connect (OBC) defines how Implementations and Applications connect over a network. It consists of two parts: Communication Methods and Data Protocols.

    Communication Methods

    • HTTP: The Implementation acts as an HTTP server.
    • HTTP Webhook: The Implementation acts as an HTTP client.
    • 正向 WebSocket (WebSocket/WS): The Implementation acts as a WebSocket server.
    • 反向 WebSocket (WebSocket Reverse/WS Reverse): The Implementation acts as a WebSocket client.

    Data Protocols

    • Event
    • Action Request (or Request)
    • Action Response (or Response)