新增企业微信、微博、WhatsApp、Workplace 四个渠道扩展。 企业微信渠道扩展主要模块:config, gateway, webhook, webhook_bot, outbound, streaming, pairing, security, crypto, dedupe, persistent_dedupe, card, directory, events, externalcontact, media, mentions, menu, message, oauth, status 微博渠道扩展主要模块:config, gateway, webhook, outbound, streaming, pairing, security, dedupe, passive_reply, broadcast, message, menu, media, subscription, template, status WhatsApp 渠道扩展主要模块:config, gateway, webhook, outbound, streaming, pairing, security, dedupe, actions, monitor, status Workplace 渠道扩展主要模块:config, gateway, webhook, outbound, streaming, pairing, security, dedupe, actions, challenge, groups, media, mentions, menu, monitor, persona, quick_reply, signature, subscriptions, template, threading, users, status
47 lines
1.7 KiB
Python
47 lines
1.7 KiB
Python
import json
|
|
import logging
|
|
|
|
from fastapi import APIRouter, Request, HTTPException, Query
|
|
from fastapi.responses import PlainTextResponse, JSONResponse
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
def create_webhook_router(plugin) -> APIRouter:
|
|
router = APIRouter(prefix="/api/channel/whatsapp", tags=["whatsapp"])
|
|
|
|
@router.get("/webhook")
|
|
async def verify_webhook(
|
|
hub_mode: str = Query(None, alias="hub.mode"),
|
|
hub_challenge: str = Query(None, alias="hub.challenge"),
|
|
hub_verify_token: str = Query(None, alias="hub.verify_token"),
|
|
):
|
|
account = await plugin.config_adapter.resolve_account("default")
|
|
verify_token = account.get("verify_token", "")
|
|
if not verify_token:
|
|
raise HTTPException(status_code=500, detail="verify_token not configured")
|
|
|
|
if hub_mode == "subscribe" and hub_verify_token == verify_token:
|
|
logger.info("WhatsApp webhook verified successfully")
|
|
return PlainTextResponse(hub_challenge or "", status_code=200)
|
|
|
|
logger.warning("WhatsApp webhook verification failed: token mismatch")
|
|
raise HTTPException(status_code=403, detail="Verification failed")
|
|
|
|
@router.post("/webhook")
|
|
async def receive_webhook(request: Request):
|
|
body = await request.body()
|
|
headers = request.headers
|
|
|
|
if not plugin.verify_signature(body, headers):
|
|
logger.warning("WhatsApp webhook signature verification failed")
|
|
raise HTTPException(status_code=403, detail="Invalid signature")
|
|
|
|
payload = json.loads(body)
|
|
logger.debug("WhatsApp webhook received: %s", json.dumps(payload, indent=2))
|
|
|
|
await plugin.handle_webhook(payload)
|
|
|
|
return JSONResponse({"status": "ok"})
|
|
|
|
return router |