5.8 KiB
Microsoft Teams Adapter Implementation
Overview
A new Microsoft Teams platform adapter has been added to LangBot, enabling support for both personal chats and channel/group chats on Microsoft Teams.
Files Created/Modified
New Files
-
src/langbot/pkg/platform/sources/teams.py- Main adapter implementationTeamsMessageConverter: Converts between LangBot message format and Teams Activity formatTeamsEventConverter: Converts between Teams Activity events and LangBot platform eventsTeamsAdapter: Main adapter class with webhook handling
-
src/langbot/pkg/platform/sources/teams.yaml- Adapter manifest- Defines adapter metadata, configuration schema, and execution details
- Configuration fields:
app_idandapp_password
Modified Files
pyproject.toml- Added dependencies:botbuilder-core>=4.15.0botbuilder-schema>=4.15.0botframework-connector>=4.15.0- Added "teams" keyword
Features
Supported Message Types
- ✅ Plain text messages
- ✅ Image attachments (base64, URL, and file path)
- ✅ @mentions (converted to At components)
- ✅ Message replies with quote support
Supported Chat Types
- ✅ Personal chats (1-on-1 conversations)
- ✅ Channel chats (group/team conversations)
Message Handling
- Incoming Messages: Received via webhook at the unified webhook endpoint
- Outgoing Messages: Sent via Bot Framework SDK using conversation references
- Event Types: FriendMessage (personal) and GroupMessage (channel/group)
Configuration Requirements
Azure Setup
-
Register an Azure AD Application:
- Go to Azure Portal → Azure Active Directory → App registrations
- Create a new registration
- Note the Application (client) ID - this is your
app_id - Create a Client Secret - this is your
app_password
-
Create Azure Bot Resource:
- Go to Azure Portal → Create a resource → Azure Bot
- Link it to your Azure AD application
- Enable the Microsoft Teams channel
- Set the messaging endpoint to:
https://<your-domain>/bots/<bot-uuid>
LangBot Configuration
When creating a Teams bot in LangBot, provide:
adapter: teams
adapter_config:
app_id: "<your-microsoft-app-id>"
app_password: "<your-microsoft-app-password>"
Architecture
Webhook Mode
The Teams adapter operates in webhook mode, similar to the Slack adapter:
- Integrates with LangBot's unified webhook system
- Receives messages at
/bots/<bot-uuid>endpoint - No independent server process required
Message Flow
-
Incoming:
- Teams → Azure Bot Service → LangBot Webhook
- Bot Framework validates the request
- Activity converted to LangBot event format
- Event dispatched to registered listeners
-
Outgoing:
- LangBot message chain → Teams Activity format
- Reply sent via Bot Framework adapter
- Uses conversation reference for proper routing
Authentication
- Uses Bot Framework authentication with Microsoft App credentials
- JWT token validation handled by
BotFrameworkAdapter - Authorization header validated on each incoming request
Limitations & Notes
- Direct Send: The
send_messagemethod is limited - usereply_messagefor best results - Conversation References: The adapter uses conversation references from incoming activities to send replies
- Image Handling: Images are converted to inline attachments using data URIs
- Streaming: Streaming replies are not yet implemented
Testing
To test the adapter:
- Install dependencies:
uv sync - Verify adapter initialization:
uv run python -c "from src.langbot.pkg.platform.sources.teams import TeamsAdapter; print('OK')" - Configure a Teams bot in LangBot with your Azure credentials
- Expose the LangBot API endpoint publicly (use ngrok or similar)
- Set the messaging endpoint in Azure Bot Service
- Add the bot to Teams and start chatting
Troubleshooting
Pydantic Validation Error on Initialization
Fixed: The adapter was updated to properly handle optional fields (adapter, app, bot_uuid) by:
- Setting
default=Nonein pydantic.Field definitions - Not passing these fields to
super().__init__() - Setting the adapter instance after parent class initialization
This resolves the validation error: "Input should be an instance of Quart" / "Input should be a valid string"
Future Enhancements
Potential improvements:
- Adaptive Cards support
- Rich message formatting (markdown, cards)
- File attachments (non-image)
- Streaming message support
- Proactive messaging
- Team/channel member list retrieval
- Reaction handling
Dependencies
The following Python packages were added:
botbuilder-core- Core Bot Framework functionalitybotbuilder-schema- Activity and entity schemasbotframework-connector- Bot Framework connector for authentication
References
- Microsoft Teams Bot Framework Documentation
- Bot Framework SDK for Python
- Teams Conversation Bot Sample
Sources
Research sources consulted during implementation: