From Webhook to Cloud Run: A Quick Introduction to Building a Custom LINE Bot

🚀 Building a Custom LINE Bot: From Webhook to Cloud Run Deployment
LINE Bots have become a popular choice for developers building interactive services — from customer support systems to AI-powered assistants.
In this article, we’ll walk through how to set up a custom LINE Bot using Webhook + Flask/FastAPI, test it locally with ngrok, and deploy it to production using Google Cloud Run.
We’ll also compare similar tools and explain how they work together.
🧩 1. Key Concepts Explained
| Term | Category | Description |
|---|---|---|
| LINE Bot | Messaging Platform | LINE’s official chatbot platform. It allows developers to receive and reply to user messages via APIs. Webhook URL must be set in the LINE Developers Console. |
| Webhook | Communication Mechanism | An event-driven callback. When a user sends a message to your bot, LINE’s server sends the event payload (in JSON) to your server endpoint via POST. |
| Flask | Python Web Framework | A lightweight, flexible, and easy-to-learn framework ideal for RESTful APIs and Webhook-based applications. |
| FastAPI | Python Web Framework | A modern, high-performance async framework with type hinting and automatic OpenAPI documentation. |
| ngrok | Local Tunnel Tool | Exposes a local server (e.g. localhost:5000) to the internet via a secure public HTTPS URL — useful for Webhook testing. |
| Google Cloud Run | Cloud Deployment Platform | A fully managed, serverless container service by Google. Automatically scales and provides HTTPS endpoints for production bots. |
⚙️ 2. Feature Comparison
| Feature | Flask | FastAPI | ngrok | Google Cloud Run |
|---|---|---|---|---|
| Role | Lightweight web framework | High-performance async framework | Local tunnel for testing | Serverless deployment platform |
| Best Use Case | Simple Webhook prototypes | High concurrency / AI-based services | Temporary Webhook testing | Production deployment |
| Performance | Medium (synchronous) | High (asynchronous) | N/A | High (auto-scaling) |
| Setup Difficulty | Easy | Medium | Very easy | Medium (requires Docker) |
| Ideal Stage | Early development | Mid-to-late stage | Testing | Production |
| Notes | Can use Gunicorn for scaling | Built-in Swagger docs | Free version may disconnect | Low cost, managed infrastructure |
🔗 3. How These Tools Work Together
1️⃣ LINE Bot + Webhook
When a user sends a message to your LINE Bot:
- LINE’s server sends the event to your Webhook URL via POST.
- Your backend (Flask or FastAPI) receives it, processes logic, and sends a reply.
📡 Flow:
User → LINE App → LINE Server
↓
Webhook URL (Your Server)
↓
Flask/FastAPI handles request → Sends reply to LINE
2️⃣ Flask / FastAPI + ngrok
During development:
- Your local app runs on
localhost:5000. - ngrok generates a temporary public HTTPS URL such as:
https://abcd1234.ngrok.io - Use that URL as your LINE Webhook endpoint for real-time testing.
✅ Ideal for: Rapid prototyping and debugging
⚠️ Note: Free ngrok sessions may expire and need restarting.
3️⃣ Flask / FastAPI + Google Cloud Run
After testing:
- Package your Flask/FastAPI project in a Docker container.
- Deploy it to Cloud Run, which gives you a public HTTPS endpoint.
- Use that endpoint as your LINE Webhook URL for production use.
✅ Ideal for: Long-term, production-grade bots
⚙️ Works well with:
- Cloud Build (CI/CD deployment)
- Firestore / PostgreSQL (database)
- Cloud Scheduler (automated jobs)
🧠 4. Extended Integrations & Advanced Use Cases
| Category | Tool | Function | Integration |
|---|---|---|---|
| Database | PostgreSQL / Firestore | Store chat logs and user states | Connect via ORM in Flask/FastAPI |
| Cache System | Redis | Speed up lookups and responses | Use with FastAPI async tasks |
| AI / LLM | OpenAI API / LangChain / RAG | Smart responses and semantic understanding | Call AI model from Webhook handler |
| Task Queue | Celery / Background Tasks | Handle long-running tasks (e.g. scraping, analytics) | Native support in FastAPI |
| Auto Deployment | GitHub Actions / Cloud Build | Build and deploy to Cloud Run automatically | Continuous Integration (CI/CD) |
🌐 5. Typical LINE Bot Architecture
[User]
↓
[LINE App]
↓
[LINE Server]
↓ (Webhook POST JSON)
[ngrok / Cloud Run Public URL]
↓
[Flask / FastAPI Backend]
├─ Handle event logic
├─ Call AI model / Database / External API
└─ Send reply to LINE Server
↓
[User receives message]
✨ Conclusion
By combining Webhook, Flask/FastAPI, ngrok, and Google Cloud Run, you can:
- 🧩 Build a customizable, event-driven LINE Bot
- 🧪 Test locally with minimal setup
- ☁️ Deploy to the cloud effortlessly
- 🤖 Integrate with AI models or databases for advanced features
This architecture forms a solid foundation for creating everything from AI assistants to enterprise-level automation bots.