LINE Bot with Google AP2 (Agent Payments Protocol) demo
linebot-ap2 is an early-stage Python project in the AI payments / x402 ecosystem, focused on adk-python, ap2, linebot. It currently has 6 GitHub stars and 2 forks, and sits alongside related tools like agentic-commerce-skills-plugins, ledgerflow, awesome-agent-payments-protocol, x402-demo-arbitrum, awesome-agentic-commerce, sardis.
這是一個採用現代化架構的企業級 LINE Bot 應用程式,整合 Google ADK (Agent SDK) 和 Gemini AI 模型,專為電商購物和 AP2 (Agent Payments Protocol) 支付協議設計。應用程式具備完整的購物體驗功能,從商品搜尋、購物車管理到安全支付處理一應俱全,並完全符合 AP2 協議標準。
src/ 佈局,清晰的關注點分離/health 提供系統狀態監控/metrics 提供詳細的應用程式指標zoneinfo、異步語法等pyproject.toml 配置核心功能:
技術特色:
ProductService 進行商品管理MandateService 支援 AP2 協議安全功能:
企業級特性:
功能:
linebot-ap2/
├── src/linebot_ap2/ # 主要應用程式套件
│ ├── agents/ # 增強版代理實現
│ │ ├── enhanced_shopping_agent.py
│ │ └── enhanced_payment_agent.py
│ ├── common/ # 共用工具和組件
│ │ ├── session_manager.py # 增強版 session 管理
│ │ ├── intent_detector.py # 智能意圖檢測
│ │ ├── retry_handler.py # 重試和錯誤處理
│ │ └── logger.py # 結構化日誌系統
│ ├── config/ # 配置管理
│ │ └── settings.py # Pydantic 設定管理
│ ├── models/ # 資料模型
│ │ ├── payment.py # 支付相關模型
│ │ ├── product.py # 商品和購物車模型
│ │ └── agent.py # 代理回應模型
│ ├── services/ # 業務邏輯服務
│ │ ├── mandate_service.py # AP2 mandate 管理
│ │ ├── payment_service.py # 支付處理服務
│ │ └── product_service.py # 商品管理服務
│ └── tools/ # 代理工具
│ ├── shopping_tools.py # 增強版購物工具
│ └── payment_tools.py # 增強版支付工具
├── main.py # 增強版 FastAPI 應用程式
├── main_new.py # 開發版本 (可選)
├── pyproject.toml # 現代 Python 專案配置
├── requirements.txt # 舊版依賴 (向後相容)
└── CLAUDE.md # Claude Code 指導文件
ChannelSecret: LINE Channel SecretChannelAccessToken: LINE Channel Access Token GOOGLE_API_KEY: Google Gemini API 金鑰 (不使用 Vertex AI 時)DEBUG: 啟用偵錯模式 (預設: False)LOG_LEVEL: 日誌等級 - DEBUG, INFO, WARNING, ERROR (預設: INFO)HOST: 伺服器主機 (預設: 0.0.0.0)PORT: 伺服器連接埠 (預設: 8080)SESSION_TIMEOUT_MINUTES: Session 逾時時間 (預設: 30)MAX_OTP_ATTEMPTS: OTP 最大嘗試次數 (預設: 3)OTP_EXPIRY_MINUTES: OTP 過期時間 (預設: 5)如果設定 GOOGLE_GENAI_USE_VERTEXAI=True,需額外設定:
GOOGLE_CLOUD_PROJECT: Google Cloud 專案 IDGOOGLE_CLOUD_LOCATION: Google Cloud 地區複製專案
git clone <repository-url>
cd linebot-ap2
使用 pyproject.toml 安裝
# 基本安裝
pip install -e .
# 包含開發工具
pip install -e ".[dev]"
# 或使用 uv (更快的套件管理器)
uv sync
uv sync --group dev
設定環境變數 (創建 .env 檔案)
ChannelSecret=your_line_channel_secret
ChannelAccessToken=your_line_access_token
GOOGLE_API_KEY=your_google_api_key
DEBUG=True
LOG_LEVEL=DEBUG
啟動增強版應用程式
# 推薦:使用增強版 main.py
uvicorn main:app --host 0.0.0.0 --port 8080 --reload
# 使用自訂配置
uvicorn main:app --host 0.0.0.0 --port 8080 --reload --log-level debug
# 使用舊版 requirements.txt
pip install -r requirements.txt
# 啟動應用程式
uvicorn main:app --host 0.0.0.0 --port 8080
安裝開發依賴後,可使用以下工具:
# 程式碼格式化
black src/
# 排序 imports
isort src/
# 程式碼檢查
flake8 src/
# 型別檢查
mypy src/
# 執行測試
pytest
# 執行所有檢查
black src/ && isort src/ && flake8 src/ && mypy src/ && pytest
🛍️ 購物體驗:
🔐 安全支付流程:
🌤️ 天氣時間:
意圖檢測特色:
自動路由邏輯:
監控端點:
GET /health - 系統健康檢查
{
"status": "healthy",
"active_sessions": 5,
"timestamp": 1234567890.123
}
GET /metrics - 詳細應用程式指標
{
"active_sessions": 5,
"active_users": ["user1", "user2"],
"app_name": "linebot_ap2",
"model": "gemini-2.5-flash"
}
核心端點:
POST / - LINE webhook 回調 (增強錯誤處理)快速啟動:
# 使用增強版應用程式
uvicorn main:app --host 0.0.0.0 --port 8080 --reload
# 使用 ngrok 暴露至網際網路
ngrok http 8080
進階本地開發:
# 啟用偵錯模式
DEBUG=True LOG_LEVEL=DEBUG uvicorn main:app --host 0.0.0.0 --port 8080 --reload
# 監控健康狀態
curl http://localhost:8080/health
# 查看應用程式指標
curl http://localhost:8080/metrics
使用內建的 Dockerfile 建置和部署應用程式:
docker build -t linebot-ap2 .
docker run -p 8080:8080 \
-e ChannelSecret=YOUR_SECRET \
-e ChannelAccessToken=YOUR_TOKEN \
-e GOOGLE_API_KEY=YOUR_GEMINI_KEY \
linebot-ap2
驗證 Google Cloud:
gcloud auth login
設定 Google Cloud 專案:
gcloud config set project YOUR_PROJECT_ID
建置並推送 Docker 映像至 Google Container Registry:
gcloud builds submit --tag gcr.io/YOUR_PROJECT_ID/linebot-ap2
部署至 Cloud Run:
gcloud run deploy linebot-ap2 \
--image gcr.io/YOUR_PROJECT_ID/linebot-ap2 \
--platform managed \
--region asia-east1 \
--allow-unauthenticated \
--set-env-vars ChannelSecret=YOUR_SECRET,ChannelAccessToken=YOUR_TOKEN,GOOGLE_API_KEY=YOUR_GEMINI_KEY
注意:生產環境建議使用 Secret Manager 儲存敏感的環境變數。
取得服務 URL:
gcloud run services describe linebot-ap2 --platform managed --region asia-east1 --format 'value(status.url)'
在 LINE Developer Console 中設定服務 URL 作為 LINE Bot webhook URL。
為了更好的安全性,將 API 金鑰儲存為機密:
為敏感資料建立機密:
echo -n "YOUR_SECRET" | gcloud secrets create line-channel-secret --data-file=-
echo -n "YOUR_TOKEN" | gcloud secrets create line-channel-token --data-file=-
echo -n "YOUR_GEMINI_KEY" | gcloud secrets create gemini-api-key --data-file=-
授予 Cloud Run 服務存取這些機密的權限:
gcloud secrets add-iam-policy-binding line-channel-secret --member=serviceAccount:YOUR_PROJECT_NUMBER-compute@developer.gserviceaccount.com --role=roles/secretmanager.secretAccessor
gcloud secrets add-iam-policy-binding line-channel-token --member=serviceAccount:YOUR_PROJECT_NUMBER-compute@developer.gserviceaccount.com --role=roles/secretmanager.secretAccessor
gcloud secrets add-iam-policy-binding gemini-api-key --member=serviceAccount:YOUR_PROJECT_NUMBER-compute@developer.gserviceaccount.com --role=roles/secretmanager.secretAccessor
使用機密部署:
gcloud run deploy linebot-ap2 \
--image gcr.io/YOUR_PROJECT_ID/linebot-ap2 \
--platform managed \
--region asia-east1 \
--allow-unauthenticated \
--update-secrets=ChannelSecret=line-channel-secret:latest,ChannelAccessToken=line-channel-token:latest,GOOGLE_API_KEY=gemini-api-key:latest
內建監控端點:
# 健康檢查
curl http://your-service-url/health
# 詳細指標
curl http://your-service-url/metrics
Google Cloud Console 監控:
# 查看應用程式日誌
gcloud logging read "resource.type=cloud_run_revision AND resource.labels.service_name=linebot-ap2"
# 即時日誌串流
gcloud logging tail "resource.type=cloud_run_revision AND resource.labels.service_name=linebot-ap2"
關鍵指標監控:
結構化日誌格式:
2025-10-31 10:00:00 | session_manager | INFO | ✓ New session created: session_user123 for user: user123
2025-10-31 10:00:01 | shopping_tools | INFO | Product search: query='iPhone', category='', price_range=None-None
2025-10-31 10:00:02 | payment_service | INFO | Payment initiated: mandate=mandate_abc123, user=user123
監控警報建議:
git checkout -b feature/your-featureblack src/ && isort src/ && flake8 src/ && mypy src/pytestgit commit -m "Add: your feature description"git push origin feature/your-feature此專案採用 MIT 授權條款。詳見 LICENSE 檔案。
🚀 立即開始使用企業級 LINE Bot AP2 購物助手!
Skills & plugins for agentic commerce : UCP, ACP, AP2, A2A, WebMCP, Magento 2, BigCommerce, WooCommerce
A chain-agnostic AI-native payment infrastructure, authz layer for x402
🚀 Curated resources for Google's Agent Payments Protocol (AP2), A2A & x402 — blogs, videos, repos & specs. Contributions welcome!
No description.
A curated list of awesome Agentic commerce, Universal Commerce Protocol (UCP), Agentic payments protocol (AP2) resources, tools, and implementations.
The open authority layer + SDKs for AI-agent payments. Thin Python/TS clients, an MCP server, framework adapters (LangChain/CrewAI/Hermes/OpenClaw/…), and @sardis/reference — a pure policy simulator + AP2/TAP/x402 verifiers showing exactly how Sardis decides if an agent may spend. The hosted engine that moves money is private.
Lucid Agents Commerce SDK. Bootstrap AI agents in 60 seconds that can pay, sell, and participate in agentic commerce supply chains. Our protocol agnostic SDK provides CLI-generated templates and drop-in adapters for Hono, Express, Next.js, and TanStack, giving you instant access to crypto/fiat payment rails (AP2, A2A, x402, ERC8004).
Building blocks for Agentic payments (x402, MPP, AP2) for TypeScript, Rust, Go, Python, Ruby, PHP, Lua, Kotlin and Swift.
Building blocks for Agentic payments (x402, MPP, AP2) for TypeScript, Rust, Go, Python, Ruby, PHP, Lua, Kotlin and Swift.
Trust infrastructure for million-agent economies on Solana - identity, reputation, and validation designed for continuous feedback at scale.
A neutral landscape analysis of AI agent payment protocols (x402, MPP, L402, Google AP2, Visa TAP + ICC, Mastercard Agent Pay, Amex ACE, Google/Shopify UCP, OpenAI ACP, and others). Maintained by Genesis Software Group, Copenhagen. Updated April 2026. CC BY 4.0.
A curated list of protocols and standards for AI agents — MCP, A2A, ACP, AG-UI, AP2, x402, and 50+ more, organized by stack layer