--- title: "コンポーネント: イベントプロセッサー" --- イベントプロセッサー(EventProcessor)は、メッセージ受信やメンバー参加などの Bot イベントを処理するコンポーネントです。インストール後、ユーザーは LangBot で「プラグインプロセッサー」を作成してコンポーネントを選び、Bot のイベントを紐付けます。 環境の準備は[プラグイン開発チュートリアル](/ja/plugin/dev/tutor)を参照してください。 ## イベントプロセッサーコンポーネントの追加 1 つのプラグインに複数のイベントプロセッサーを追加できます。プラグインのディレクトリで `lbp comp EventProcessor` を実行し、名前に `welcome`、説明に `Welcome new members` を入力します。 ```bash lbp comp EventProcessor ``` 生成される `components/event_processor/welcome.yaml` はコンポーネントを定義するファイルで、`welcome.py` に処理を記述します。 ## マニフェストファイル: イベントプロセッサー `welcome.yaml` を次の内容に変更します。 ```yaml apiVersion: langbot/v1 kind: EventProcessor metadata: name: welcome label: en_US: Welcome members zh_Hans: 欢迎新成员 ja_JP: 新しいメンバーを歓迎 description: en_US: Welcome new members and respond to /hello. zh_Hans: 欢迎新成员,并响应 /hello 消息。 ja_JP: 新しいメンバーを歓迎し、/hello に返信します。 spec: events: - group.member_joined - message.received capabilities: tool_calling: true permissions: tools: [detail, call] config: - name: greeting type: string label: en_US: Greeting zh_Hans: 欢迎语 ja_JP: 歓迎メッセージ required: true default: Welcome aboard! - name: reply_enabled type: boolean label: en_US: Send replies zh_Hans: 发送回复 ja_JP: 返信する default: true execution: python: path: ./welcome.py attr: Welcome ``` `spec.events` は対応するイベント、`spec.config` は詳細画面でユーザーが変更できる設定です。この例ではメンバー参加とメッセージ受信に対応し、歓迎メッセージと返信の有効・無効を設定できます。返信には、上記の `tool_calling` と `permissions.tools` の指定が必要です。 ## プラグインの実装 `Welcome` クラスの `initialize` メソッド内で処理関数を登録します。`welcome.py` を次の内容に変更してください。 ```python from langbot_plugin.api.definition.components.event_processor import ( EventProcessor, EventProcessorContext, ) from langbot_plugin.api.entities.builtin.platform.events import ( MemberJoinedEvent, MessageReceivedEvent, ) from langbot_plugin.api.entities.builtin.platform.message import Plain class Welcome(EventProcessor): async def initialize(self): await super().initialize() @self.handler(MemberJoinedEvent) async def on_join(ctx: EventProcessorContext): name = ctx.event.member.nickname or str(ctx.event.member.id) await ctx.log(f"Member joined: {name}") if not ctx.config.get("reply_enabled", True): await ctx.log("Replies are disabled for this processor") return greeting = ctx.config.get("greeting", "Welcome aboard!") await ctx.reply(f"{name}, {greeting}") await ctx.log("Welcome action completed") @self.handler(MessageReceivedEvent) async def on_message(ctx: EventProcessorContext): text = "".join( item.text for item in ctx.event.message_chain if isinstance(item, Plain) ).strip() if text != "/hello": await ctx.log("Message ignored: expected /hello") return if ctx.config.get("reply_enabled", True): await ctx.reply(ctx.config.get("greeting", "Welcome aboard!")) else: await ctx.log("Replies are disabled for this processor") ``` `on_join` は参加したメンバーに挨拶し、`on_message` は `/hello` に返信します。「返信する」を無効にすると、ログだけを記録します。 `ctx.event` は現在のイベント、`ctx.config` はこのプロセッサーの設定です。返信には `ctx.reply()`、ログの記録には `ctx.log()` を使います。関数の実行が終わると、イベントの処理も終了します。 ## イベントの登録 `@self.handler(イベントクラス)` で処理関数を登録し、対応する識別子を `spec.events` に追加します。例えば `MemberJoinedEvent` は `group.member_joined` に対応します。 | イベント | マニフェストの識別子 | SDK のイベントクラス | | --- | --- | --- | | メッセージ受信 | `message.received` | `MessageReceivedEvent` | | メッセージ編集 | `message.edited` | `MessageEditedEvent` | | メッセージ削除 | `message.deleted` | `MessageDeletedEvent` | | メッセージへのリアクション | `message.reaction` | `MessageReactionEvent` | | フィードバック受信 | `feedback.received` | `FeedbackReceivedEvent` | | メンバー参加 | `group.member_joined` | `MemberJoinedEvent` | | メンバー退出 | `group.member_left` | `MemberLeftEvent` | | メンバーのミュート | `group.member_banned` | `MemberBannedEvent` | | グループ情報更新 | `group.info_updated` | `GroupInfoUpdatedEvent` | | 友達申請受信 | `friend.request_received` | `FriendRequestReceivedEvent` | | 友達追加 | `friend.added` | `FriendAddedEvent` | | 友達削除 | `friend.removed` | `FriendRemovedEvent` | | Bot のグループ招待 | `bot.invited_to_group` | `BotInvitedToGroupEvent` | | Bot のグループからの削除 | `bot.removed_from_group` | `BotRemovedFromGroupEvent` | | Bot のミュート | `bot.muted` | `BotMutedEvent` | | Bot のミュート解除 | `bot.unmuted` | `BotUnmutedEvent` | | プラットフォーム固有のイベント | `platform.specific` | `PlatformSpecificEvent` | 利用できるイベントとフィールドは、SDK の [events.py](https://github.com/langbot-app/langbot-plugin-sdk/blob/main/src/langbot_plugin/api/entities/builtin/platform/events.py)を参照してください。対応イベントはプラットフォームによって異なります。 ## イベントプロセッサーのテスト [プラグイン開発チュートリアル](/ja/plugin/dev/tutor)に従ってデバッグ接続を設定し、プラグインのディレクトリで `lbp run` を実行します。LangBot で次の操作を行ってください。 1. 「プラグインプロセッサー」を作成し、詳細画面で「新しいメンバーを歓迎」を選び、設定を保存します。 2. 左側でメンバー参加を選択し、ニックネーム、メンバー ID、グループ ID を入力してテストを実行します。 3. 返信とログを確認します。メッセージ受信を選んで `/hello` を入力すると、返信のテストもできます。 デバッグ中の返信は Mock で再現され、実際のメッセージは送信されません。コンポーネントの YAML を変更したら `lbp run` を再起動してください。 インストール後、Bot のメンバー参加とメッセージ受信をこのプロセッサーに紐付けると、実際のイベントを処理できます。 ## 次のステップ - [EventProcessorDemo](https://github.com/langbot-app/langbot-plugin-demo/tree/main/EventProcessorDemo)で他の実装例を確認できます。 - パイプラインの前処理やモデル呼び出し完了時などに機能を追加する場合は、[イベントリスナー](/ja/plugin/dev/components/event-listener)を使用してください。