--- title: line-webhook emoji: 🤖 colorFrom: green colorTo: blue sdk: docker app_port: 8000 pinned: false --- # LINE Webhook → HuggingFace Sentiment Server รับ message event จาก LINE Official Account → วิเคราะห์ sentiment → ส่งต่อให้ Lovable AI Dashboard --- ## Architecture ``` LINE OA → Webhook Server → HuggingFace Inference API ↓ ↓ PostgreSQL ←─────── sentiment result ↓ Redis Pub/Sub ↓ Lovable AI Dashboard (real-time) ``` --- ## Quick Start ### 1. Clone & ติดตั้ง ```bash git clone cd line-webhook cp .env.example .env # แก้ไขค่าใน .env ``` ### 2. ใส่ค่าใน .env ```env LINE_CHANNEL_SECRET=<จาก LINE Developers Console> LINE_CHANNEL_ACCESS_TOKEN=<จาก LINE Developers Console> HF_API_TOKEN=<จาก huggingface.co/settings/tokens> ``` ### 3. รันด้วย Docker Compose ```bash docker compose up -d ``` Server พร้อมที่ `http://localhost:8000` ### 4. รัน Local (ไม่ใช้ Docker) ```bash python -m venv venv source venv/bin/activate # Windows: venv\Scripts\activate pip install -r requirements.txt uvicorn app.main:app --reload ``` --- ## ตั้งค่า LINE Webhook URL 1. ไปที่ [LINE Developers Console](https://developers.line.biz/console/) 2. เลือก Channel → **Messaging API** 3. ตั้ง Webhook URL เป็น: ``` https://your-domain.com/webhook ``` > ใช้ **ngrok** สำหรับ local dev: > ```bash > ngrok http 8000 > ``` > แล้วใช้ URL ที่ ngrok ให้ เช่น `https://abc123.ngrok.io/webhook` 4. เปิด **Use webhook** → Verify --- ## ตั้งค่า Secrets บน HuggingFace Spaces > ⚠️ ห้าม upload ไฟล์ `.env` ขึ้น Space โดยตรง ให้ตั้งผ่าน Settings แทน 1. เปิด Space → **Settings** → **Variables and secrets** 2. เพิ่ม Secret ดังนี้ (เลือก **Secret** ไม่ใช่ Variable): | Key | ค่า | |-----|-----| | `LINE_CHANNEL_SECRET` | จาก LINE Developers Console | | `LINE_CHANNEL_ACCESS_TOKEN` | จาก LINE Developers Console | | `HF_API_TOKEN` | จาก huggingface.co/settings/tokens | | `DATABASE_URL` | connection string ของ PostgreSQL | | `REDIS_URL` | connection string ของ Redis | --- ## API Endpoints | Method | Path | คำอธิบาย | |--------|------|-----------| | `POST` | `/webhook` | รับ event จาก LINE (signature verified) | | `GET` | `/health` | Health check | | `GET` | `/api/conversations` | ดึง conversation list พร้อม sentiment | | `GET` | `/api/sentiment-summary` | สรุป sentiment ภาพรวม 7 วัน | --- ## HuggingFace Model แนะนำใช้ model เหล่านี้ (ตั้งใน `HF_MODEL_NAME`): | Model | ภาษา | ความแม่นยำ | |-------|-------|-----------| | `poom-sci/WangchanBERTa-finetuned-sentiment` | ไทยโดยเฉพาะ | ดีที่สุด | | `cardiffnlp/twitter-xlm-roberta-base-sentiment` | Multilingual (ไทยได้) | ดี | | `airesearch/wangchanberta-base-att-spm-uncased` | ไทยเป็นหลัก | ดีมากสำหรับไทย | --- ## Alert System เมื่อ negative score ≥ `NEGATIVE_ALERT_THRESHOLD` (default 0.80): - Push event ไปที่ Redis channel `sentiment:alert_live` - Lovable Dashboard รับ alert แสดง notification ทันที --- ## รัน Tests ```bash pip install pytest pytest-asyncio pytest tests/ -v ```