简体中文 English 繁體中文 日本語 Русский
本文为静态镜像,内容以交互版为准 在交互式文档中心打开 →

OneBot12プラットフォーム仕様ドキュメント

OneBot12Adapter は、OneBot V12 プロトコルに基づいて構築されたアダプターであり、ErisPulse フレームワークの基本プロトコルアダプターです。


ドキュメント情報

基本情報

v5 フレームワークの更新(4.3.0)

このアダプタは v5 フレームワークに準拠しました(段階的なアップグレード、API は互換性があります):

既存の機能(4.2.0 以降でサポート):複数アカウント、Api DSL の標準アクションマッピング(get_self_info→get_login_info など)、Request DSL(友人/グループリクエストの承認:event.approve() / event.reject())、EventMixin、i18n。

標準Apiアクション(Api DSL)

OneBot12 バックエンドは、OB12の標準アクション名をすべてネイティブでサポートしています。Api DSL はデフォルトで call_api を直接透過的に委譲します(マッピングは不要です):

from ErisPulse import sdk
ob12 = sdk.adapter.get("onebot12")

result = await ob12.Api.get_self_info()
result = await ob12.Api.get_friend_list()
await ob12.Api.delete_message(message_id="MSG_ID")

# アカウントを指定(複数アカウント)
result = await ob12.Api.Using("main").get_self_info()

# プラットフォーム拡張アクション
result = await ob12.Api.call("extend_action", param=1)

アクションのサポートは、バックエンドの実装(NapCat/Lagrange/LLOneBot など)に準じます。サポートされていないアクションは、バックエンドがエラーを返し透過的に転送します。

リクエスト操作(Request DSL)

OneBot12 標準の handle_quick_request 動作に基づき、フレンドリクエストとグループ参加招待の承認/拒否を処理します。

Event 便利メソッド

from ErisPulse.Core.Event import request

@request.on_friend_request()
async def handle_friend_request(event):
    if event.get("platform") != "onebot12":
        return
    comment = event.get("comment", "")
    if comment == "passphrase":
        await event.approve()      # 承認
    else:
        await event.reject()       # 拒否

手動による Request DSL 呼び出し

await ob12.Request("request_flag").accept()
await ob12.Request("request_flag").reject()
await ob12.Request("request_flag").Using("main").accept()

支持するメッセージ送信タイプ

すべての送信メソッドは、チェーン式の構文で実装されています。例:

from ErisPulse.Core import adapter
onebot12 = adapter.get("onebot12")

# デフォルトのアカウントを使って送信
await onebot12.Send.To("group", group_id).Text("Hello World!")

# 特定のアカウントを使って送信
await onebot12.Send.To("group", group_id).Account("main").Text("来自主アカウントのメッセージ")

大文字小文字を区別しない呼び出し

すべての送信メソッドとチェーン式修飾メソッドは、大文字小文字を区別しない呼び出しをサポートしており、アダプターは正しい標準メソッド名に自動的にマッピングします:

# 以下すべての呼び出し方法は等価です
await onebot12.Send.To("user", 123).Text("hello")
await onebot12.Send.To("user", 123).text("hello")
await onebot12.Send.To("user", 123).TEXT("hello")

# チェーン式修飾メソッドも同様にサポート
await onebot12.Send.To("group", 123).At(456).Text("hello")
await onebot12.Send.To("group", 123).at(456).TEXT("hello")
await onebot12.Send.To("group", 123).AT(456).text("hello")

支持されていないメソッドの呼び出し

存在しないメソッドを呼び出した場合、アダプターは例外をスローするのではなく、ユーザーにわかりやすいテキストのメッセージを返します:

# 支持されていないメソッドを呼び出す
result = await onebot12.Send.To("user", 123).UnsupportedMethod("test")

# 戻り値は送信されたテキストメッセージです
# メッセージ内容: [サポートされていない送信タイプ] メソッド名: UnsupportedMethod, パラメータ: [args[0]: 'test']

基本的なメッセージタイプ

チェーン式修飾メソッド(selfを返してチェーン式呼び出しをサポート)

原始メッセージ送信

その他のメッセージタイプ

管理機能

OneBot12標準イベント

OneBot12アダプターはOneBot12標準を完全に遵守しており、イベント形式の変換は不要で、フレームワークに直接送信されます。

新機能:元のイベントタイプフィールド

standards/event-conversion.md 規格に準拠し、すべてのイベントには元のイベントタイプフィールド onebot12_raw_type が保持されます:

{
    "id": "event-id",
    "type": "message",              # イベントタイプ
    "onebot12_raw_type": "message", # 元のイベントタイプ(typeと同じ)
    "detail_type": "private",
    "self": {"user_id": "bot-id"},
    "user_id": "user-id",
    "message": [{"type": "text", "data": {"text": "Hello"}}],
    "alt_message": "Hello",
    "time": 1234567890
}

メッセージイベント (Message Events)

# プライベートチャットメッセージ
{
    "id": "event-id",
    "type": "message",
    "onebot12_raw_type": "message",
    "detail_type": "private",
    "self": {"user_id": "bot-id"},
    "user_id": "user-id",
    "message": [{"type": "text", "data": {"text": "Hello"}}],
    "alt_message": "Hello",
    "time": 1234567890
}

# グループチャットメッセージ
{
    "id": "event-id",
    "type": "message",
    "onebot12_raw_type": "message",
    "detail_type": "group",
    "self": {"user_id": "bot-id"},
    "user_id": "user-id",
    "group_id": "group-id",
    "message": [{"type": "text", "data": {"text": "Hello group"}}],
    "alt_message": "Hello group",
    "time": 1234567890
}

通知イベント (Notice Events)

# グループメンバーの追加
{
    "id": "event-id",
    "type": "notice",
    "onebot12_raw_type": "notice",
    "detail_type": "group_member_increase",
    "self": {"user_id": "bot-id"},
    "group_id": "group-id",
    "user_id": "user-id",
    "operator_id": "operator-id",
    "sub_type": "approve",
    "time": 1234567890
}

# グループメンバーの削減
{
    "id": "event-id",
    "type": "notice",
    "onebot12_raw_type": "notice",
    "detail_type": "group_member_decrease",
    "self": {"user_id": "bot-id"},
    "group_id": "group-id",
    "user_id": "user-id",
    "operator_id": "operator-id",
    "sub_type": "leave",
    "time": 1234567890
}

要求イベント (Request Events)

# フレンドリクエスト
{
    "id": "event-id",
    "type": "request",
    "onebot12_raw_type": "request",
    "detail_type": "friend",
    "self": {"user_id": "bot-id"},
    "user_id": "user-id",
    "comment": "申請メッセージ",
    "flag": "request-flag",
    "time": 1234567890
}

# グループ招待リクエスト
{
    "id": "event-id",
    "type": "request",
    "onebot12_raw_type": "request",
    "detail_type": "group",
    "self": {"user_id": "bot-id"},
    "group_id": "group-id",
    "user_id": "user-id",
    "comment": "申請メッセージ",
    "flag": "request-flag",
    "sub_type": "invite",
    "time": 1234567890
}

元イベント (Meta Events)

# ライフサイクルイベント
{
    "id": "event-id",
    "type": "meta_event",
    "onebot12_raw_type": "meta_event",
    "detail_type": "lifecycle",
    "self": {"user_id": "bot-id"},
    "sub_type": "enable",
    "time": 1234567890
}

# ハートビートイベント
{
    "id": "event-id",
    "type": "meta_event",
    "onebot12_raw_type": "meta_event",
    "detail_type": "heartbeat",
    "self": {"user_id": "bot-id"},
    "interval": 5000,
    "status": {"online": true},
    "time": 1234567890
}

設定オプション

アカウント設定

各アカウントは以下のオプションを個別に設定できます:

設定例

[OneBotv12_Adapter.accounts.main]
mode = "server"
server_path = "/onebot12-main"
server_token = "main_token"
enabled = true
platform = "onebot12"
implementation = "go-cqhttp"

[OneBotv12_Adapter.accounts.backup]
mode = "client"
client_url = "ws://127.0.0.1:3002"
client_token = "backup_token"
enabled = true
platform = "onebot12"
implementation = "shinonome"

[OneBotv12_Adapter.accounts.test]
mode = "client"
client_url = "ws://127.0.0.1:3003"
enabled = false

デフォルト設定

アカウントの設定が一切ない場合、アダプターは自動的に以下のようにデフォルトアカウントを作成します:

[OneBotv12_Adapter.accounts.default]
mode = "server"
server_path = "/onebot12"
enabled = true
platform = "onebot12"

送信メソッドの戻り値

メッセージ送信メソッド

すべてのメッセージ送信メソッド(例:.Text(), .Image(), .Raw_ob12() など)は、asyncio.Task オブジェクトを返します。これにより、送信結果を直接 await で取得できます。

task = await onebot12.Send.To("group", 123456).Text("Hello")

チェーン修飾メソッド

すべてのチェーン修飾メソッド(例:.At(), .AtAll(), .Reply())は、self を返し、チェーン呼び出しをサポートします。

# 複数の修飾メソッドを組み合わせて使用
await onebot12.Send.To("group", 123456).Reply("msg123").At(789).At(790).Text("テキスト")

APIレスポンス標準

アダプターは ErisPulse の標準化された返却規格(standards/api-response.md)に準拠しています:

# 成功時のレスポンス
{
    "status": "ok",              # 必須:実行ステータス
    "retcode": 0,                # 必須:返却コード(0は成功を示す)
    "data": {                     # 必須:レスポンスデータ
        "message_id": "123456",
        "time": 1632847927.599013
    },
    "message_id": "123456",       # 必須:メッセージID(存在しない場合は空文字列)
    "message": "",                # 必須:エラーメッセージ(成功時は空)
    "echo": "1234",               # オプション:リクエスト中のechoをそのまま返す
    "onebot12_raw": {...}        # オプション:元のレスポンスデータ
}

# 失敗時のレスポンス
{
    "status": "failed",           # 必須:実行ステータス
    "retcode": 10003,            # 必須:返却コード(0以外は失敗を示す)
    "data": None,                # 必須:失敗時はnull
    "message_id": "",            # 必須:失敗時は空文字列
    "message": "必要なパラメータが不足しています",    # 必須:エラーメッセージ
    "echo": "1234",              # オプション:リクエスト中のechoをそのまま返す
    "onebot12_raw": {...}        # オプション:元のレスポンスデータ
}

エラーコード規格

OneBot12 の標準エラーコードに準拠します:

複数アカウントによる送信構文

# アカウント選択方法
await onebot12.Send.Using("main").To("group", 123456).Text("メインアカウントのメッセージ")
await onebot12.Send.Using("backup").To("group", 123456).Image("http://example.com/image.jpg")

# API呼び出し方法
await onebot12.call_api("send_message", account_id="main", 
    detail_type="group", group_id=123456, 
    content=[{"type": "text", "data": {"text": "Hello"}}])

非同期処理メカニズム

OneBot12アダプターは非同期非ブロッキング設計を採用しています:

  1. メッセージ送信はイベント処理ループをブロックしません
  2. 複数の並行送信操作を同時に実行できます
  3. APIの応答を即時に処理できます
  4. WebSocket接続はアクティブな状態を維持します
  5. 複数アカウントの並行処理が可能で、各アカウントは独立して動作します

エラー処理

アダプタは包括的なエラー処理メカニズムを提供します:

  1. ネットワーク接続異常時の自動再接続(各アカウントごとに個別に再接続が可能、間隔は30秒)
  2. API呼び出しのタイムアウト処理(固定30秒のタイムアウト)
  3. メッセージ送信失敗時の自動リトライ(最大3回のリトライ)
  4. 対応していないメソッドの呼び出しは、親しみやすいテキストのメッセージを返します

イベント処理の強化

複数アカウントモードでは、すべてのイベントにアカウント情報が自動的に追加されます:

{
    "type": "message",
    "onebot12_raw_type": "message",  // 元のイベントタイプ
    "detail_type": "private",
    "self": {"user_id": "123456"},  // イベントを送信したアカウントID(標準フィールド)
    "platform": "onebot12",
    // ... その他のイベントフィールド
}

管理インターフェース

# すべてのアカウント情報を取得
accounts = onebot12.accounts

# アカウントの接続状態を確認
connection_status = {
    account_id: connection is not None and not connection.closed
    for account_id, connection in onebot12.connections.items()
}

# アカウントの動的有効化/無効化(アダプタの再起動が必要)
onebot12.accounts["test"].enabled = False

OneBot12標準機能

メッセージセグメント標準

OneBot12は標準化されたメッセージセグメント形式を使用します:

# テキストメッセージセグメント
{"type": "text", "data": {"text": "Hello"}}

# 画像メッセージセグメント
{"type": "image", "data": {"file_id": "image-id"}}

# メンションメッセージセグメント
{"type": "mention", "data": {"user_id": "user-id", "user_name": "Username"}}

# 返信メッセージセグメント
{"type": "reply", "data": {"message_id": "msg-id"}}

API標準

OneBot12標準API規格に従います:

最佳実践

  1. 設定管理: さまざまな用途のロボットを分けて管理するために、複数アカウントの設定を使用することを推奨します。
  2. エラー処理: API呼び出しの返り値ステータスを常にチェックしてください。
  3. メッセージ送信: 送信可能なメッセージの種類を使用し、サポートされていないメッセージを送信しないようにしてください。
  4. 接続監視: 接続状態を定期的にチェックし、サービスの可用性を確保してください。
  5. パフォーマンス最適化: バッチ送信時は Batch メソッドを使用し、ネットワークのオーバーヘッドを減らしてください。
  6. メソッド呼び出し: 標準の大文字キャメルケース命名法(例: .Text())を使用することを推奨しますが、異なるプログラミングスタイルとの互換性を考慮して小文字形式もサポートしています(この形式は旧バージョンとの互換性が失われる可能性があります)。