Configure Feishu bot credentials for a digital employee (wizard step 5).
(
agent_id: uuid.UUID,
data: ChannelConfigCreate,
current_user: User = Depends(get_current_user),
db: AsyncSession = Depends(get_db),
)
| 362 | |
| 363 | @router.post("/agents/{agent_id}/channel", response_model=ChannelConfigOut, status_code=status.HTTP_201_CREATED) |
| 364 | async def configure_channel( |
| 365 | agent_id: uuid.UUID, |
| 366 | data: ChannelConfigCreate, |
| 367 | current_user: User = Depends(get_current_user), |
| 368 | db: AsyncSession = Depends(get_db), |
| 369 | ): |
| 370 | """Configure Feishu bot credentials for a digital employee (wizard step 5).""" |
| 371 | agent, _access = await check_agent_access(db, current_user, agent_id) |
| 372 | if not is_agent_creator(current_user, agent): |
| 373 | raise HTTPException(status_code=403, detail="Only creator can configure channel") |
| 374 | |
| 375 | # Check existing |
| 376 | result = await db.execute(select(ChannelConfig).where( |
| 377 | ChannelConfig.agent_id == agent_id, |
| 378 | ChannelConfig.channel_type == "feishu", |
| 379 | )) |
| 380 | existing = result.scalar_one_or_none() |
| 381 | if existing: |
| 382 | existing.app_id = data.app_id |
| 383 | existing.app_secret = data.app_secret |
| 384 | existing.encrypt_key = data.encrypt_key |
| 385 | existing.verification_token = data.verification_token |
| 386 | existing.extra_config = data.extra_config or {} |
| 387 | existing.is_configured = True |
| 388 | await db.flush() |
| 389 | |
| 390 | # Start/Stop WS client in background |
| 391 | from app.services.feishu_ws import feishu_ws_manager |
| 392 | import asyncio |
| 393 | mode = existing.extra_config.get("connection_mode", "webhook") |
| 394 | if mode == "websocket": |
| 395 | asyncio.create_task(feishu_ws_manager.start_client(agent_id, existing.app_id, existing.app_secret)) |
| 396 | else: |
| 397 | asyncio.create_task(feishu_ws_manager.stop_client(agent_id)) |
| 398 | |
| 399 | return ChannelConfigOut.model_validate(existing) |
| 400 | |
| 401 | config = ChannelConfig( |
| 402 | agent_id=agent_id, |
| 403 | channel_type=data.channel_type, |
| 404 | app_id=data.app_id, |
| 405 | app_secret=data.app_secret, |
| 406 | encrypt_key=data.encrypt_key, |
| 407 | verification_token=data.verification_token, |
| 408 | extra_config=data.extra_config or {}, |
| 409 | is_configured=True, |
| 410 | ) |
| 411 | db.add(config) |
| 412 | await db.flush() |
| 413 | |
| 414 | # Start WS client in background |
| 415 | from app.services.feishu_ws import feishu_ws_manager |
| 416 | import asyncio |
| 417 | mode = config.extra_config.get("connection_mode", "webhook") |
| 418 | if mode == "websocket": |
| 419 | asyncio.create_task(feishu_ws_manager.start_client(agent_id, config.app_id, config.app_secret)) |
| 420 | |
| 421 | return ChannelConfigOut.model_validate(config) |
nothing calls this directly
no test coverage detected