Line bot Webhook Flask Fast API Cloud run Automation Ai integration

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

By Dan 2025-10-07 4 min read
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

TermCategoryDescription
LINE BotMessaging PlatformLINE’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.
WebhookCommunication MechanismAn 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.
FlaskPython Web FrameworkA lightweight, flexible, and easy-to-learn framework ideal for RESTful APIs and Webhook-based applications.
FastAPIPython Web FrameworkA modern, high-performance async framework with type hinting and automatic OpenAPI documentation.
ngrokLocal Tunnel ToolExposes a local server (e.g. localhost:5000) to the internet via a secure public HTTPS URL — useful for Webhook testing.
Google Cloud RunCloud Deployment PlatformA fully managed, serverless container service by Google. Automatically scales and provides HTTPS endpoints for production bots.

⚙️ 2. Feature Comparison

FeatureFlaskFastAPIngrokGoogle Cloud Run
RoleLightweight web frameworkHigh-performance async frameworkLocal tunnel for testingServerless deployment platform
Best Use CaseSimple Webhook prototypesHigh concurrency / AI-based servicesTemporary Webhook testingProduction deployment
PerformanceMedium (synchronous)High (asynchronous)N/AHigh (auto-scaling)
Setup DifficultyEasyMediumVery easyMedium (requires Docker)
Ideal StageEarly developmentMid-to-late stageTestingProduction
NotesCan use Gunicorn for scalingBuilt-in Swagger docsFree version may disconnectLow cost, managed infrastructure

🔗 3. How These Tools Work Together

1️⃣ LINE Bot + Webhook

When a user sends a message to your LINE Bot:

  1. LINE’s server sends the event to your Webhook URL via POST.
  2. 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

CategoryToolFunctionIntegration
DatabasePostgreSQL / FirestoreStore chat logs and user statesConnect via ORM in Flask/FastAPI
Cache SystemRedisSpeed up lookups and responsesUse with FastAPI async tasks
AI / LLMOpenAI API / LangChain / RAGSmart responses and semantic understandingCall AI model from Webhook handler
Task QueueCelery / Background TasksHandle long-running tasks (e.g. scraping, analytics)Native support in FastAPI
Auto DeploymentGitHub Actions / Cloud BuildBuild and deploy to Cloud Run automaticallyContinuous 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.