-
Notifications
You must be signed in to change notification settings - Fork 1.5k
en workshop impl platform adapter
This article will use the aiocqhttp adapter as an example to explain how to implement a new message platform adapter for LangBot, in order to integrate new message platform protocols such as WhatsApp and Discord.
Add a new message platform adapter, for example: integrate WhatsApp, Discord, etc.
- Base class: `pkg.platform.adapter.MessageSourceAdapter`
- Decorator: `pkg.platform.adapter.adapter_class(name: str)`
- Reference implementation: `pkg.platform.sources/`
To implement a message platform adapter, we essentially need to implement a class that inherits from the base class and register it using the decorator. Finally, it will be available for users to select and use during runtime.
Create a new file aiocqhttp.py in the pkg/platform/sources/ directory:
from .. import adapter
@adapter.adapter_class("aiocqhttp")
class AiocqhttpAdapter(adapter.MessageSourceAdapter):
passAlso import this module in pkg/platform/manager.py at line 40:
from .sources import nakuru, aiocqhttp, qqbotpyFinally, write your own manifest file by referring to other adapters' manifest files.
The main work of a message platform adapter is to receive events from the message platform protocol, convert them into LangBot's format, pass them to the core components, and after LangBot's core components process them, pass the content that needs to be replied to the message platform adapter, which then converts the content into the platform protocol format and sends it to the message platform.
The following diagram shows the processing patterns for two scenarios: message platform protocol without SDK and with mature SDK. In the first case, we need to implement connection management and network format data conversion to LB message Python objects according to the message platform protocol documentation. In the second case, the message platform has a mature SDK, we only need to create connections according to the SDK documentation, register event listeners, and implement message converters.

In the pkg.platform.sources.aiocqhttp file, we can see not only AiocqhttpAdapter, but also class AiocqhttpMessageConverter(adapter.MessageConverter): and class AiocqhttpEventConverter(adapter.EventConverter):.
-
MessageConverteris used for converting between LangBot messages and platform messages. -
EventConverteris used for converting between LangBot events and platform events.
TBC...
Automatically synchronized from langbot-app/langbot-wiki.
简体中文
指南
开发者
- 插件开发
- 插件 SDK API
- 核心开发
API 参考
- Service API
English
Guides
Developers
-
Plugin Development
- Plugin Development Tutorial
- Completing Plugin Configuration Information
- Plugin Directory Structure
- Component Development
- Code Style Guide
- Migration Guide
- Publish Plugin
- Plugin SDK API
- Core Development
API Reference
- Service API