OneBot API Specification
repository·main·Indexed 24 days ago
https://github.com/botuniverse/onebotA 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.
What's inside OneBot
- 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.
What is the OneBot standard?
mainOneBot 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.Understand the OneBot Event data structure
mainAn 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
objectand 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.Requirements for implementing OneBot Message Segments
mainWhen implementing OneBot message segments, developers should follow these guidelines for parameter handling and error reporting:
- Parameter Support: If a standard message segment is implemented, it is recommended to support all standard defined parameters.
- 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
34xxxerror code indicating a message sending failure. If failure detection is difficult, the error may be ignored.
- Unsupported Segment: If a message segment cannot be sent, return error code
Authenticate HTTP Requests via access_token
mainIf an
access_tokenis configured and is not an empty string, the OneBot implementation must perform authentication using the following priority:- Authorization Header: Check for the
Authorizationheader. The value must exactly matchBearer <access_token>(whitespace trimming is not required on either side). - URL Query Parameter: If the header is missing, check for an
access_tokenquery 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 Unauthorizedstatus code.- Authorization Header: Check for the
Understand the OneBot ecosystem architecture
mainThe OneBot ecosystem consists of three primary layers that interact via the OneBot Standard:
- Bot Platform: The chat software providing the API (e.g.,
telegram,discord,qq). - 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.
- 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.
- Bot Platform: The chat software providing the API (e.g.,
Core OneBot communication concepts: Events, Actions, and Messages
mainOneBot 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).
- Event: Something that happened on the platform or within the implementation. Implementations push events to applications. Types include:
Data Type Note: Handling `bytes` in JSON
mainWhen interacting with OneBot file actions, any field defined with thebytesdata type must be represented as a Base64 encoded string within the JSON payload.Use WebSocket for OneBot communication
mainWebSocket 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.Understand the structure of `message.private` events
mainThe
message.privateevent is triggered when a user sends a private message. These events are based on the OneBot Connect - Event specification, where the top-leveltypefield must bemessageand thedetail_typefield must beprivate.When implementing handlers for these events, note that if a
sub_typeis not explicitly mentioned in documentation, its value should be an empty string ("").OneBot Connect (OBC) communication methods and protocols
mainOneBot 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)
Understand the structure of OneBot notice events
mainAll notice events in OneBot are based on the OneBot Connect - Event protocol. To identify a notice event, the
typefield must be set tonotice.Specific event details are distinguished by the
detail_typefield. If asub_typeis not explicitly defined for an event, its value should be an empty string ("").