DEV Community

Midhun P M
Midhun P M

Posted on

I Built a WhatsApp AI Agent for Kochi Metro — Here's How It Works

How I built MetroMind, a WhatsApp-based agentic AI commute assistant
for Kochi Metro using n8n, FastAPI, LangChain, and GTFS data — and placed Top 10
at the OpenAI Codex Nightline Hackathon.

canonical_url: https://midhunpm.in

Real talk — Kochi Metro's app is terrible. You open it, wait for it to load,
tap through four screens, and by that time you've already missed your train.

So I built MetroMind — a WhatsApp AI agent that handles your entire Kochi
Metro commute through a chat. No app switching, no loading screens. Just message
it like you'd message a friend.

It placed Top 10 at the OpenAI Codex Nightline Hackathon. Here's how it works.


What MetroMind Does

  • Route planning — "How do I get from Aluva to MG Road?" and it tells you exactly which trains, interchanges, and timings
  • Fare calculation — real fares pulled from live GTFS data, not hardcoded guesses
  • Ticket booking — automated via Playwright since KMRL doesn't have a public booking API (more on this below)
  • Natural language — ask it anything in plain English or Malayalam, it figures out what you need

All through WhatsApp. No app install required.


The Stack

WhatsApp (Twilio) → n8n → FastAPI → LangChain → GTFS Data

Playwright (booking automation)

  • n8n as the orchestration layer — handles the WhatsApp webhook from Twilio, routes messages, manages conversation state
  • FastAPI backend — core business logic, GTFS parsing, route calculation
  • LangChain — agent layer that interprets user intent and decides which tools to call
  • GTFS — Kerala's open transit data format, has all station info, routes, and schedules
  • Twilio — WhatsApp Business API for the messaging interface

The Interesting Part — Reverse Engineering KMRL's API

KMRL doesn't have a public booking API. Their app encrypts all traffic with AES.

So I reverse engineered it.

Decompiled the APK, traced the encryption keys, figured out the request/response
format. Then built a Playwright-based automation that handles the booking flow
headlessly — it's essentially a bot that fills out the booking form faster than
a human can.

Not the cleanest solution, but it works. And it's way faster than doing it manually.


Why n8n Instead of Just FastAPI

I self-host n8n on my homeserver (Dell i5, Ubuntu 24.04, behind Cloudflare Tunnel

  • Traefik). Using n8n meant I could:

  • Visually wire up the WhatsApp → agent → response pipeline

  • Add new triggers and integrations without touching code

  • Monitor every workflow run with full execution logs

For a project that's fundamentally about orchestration, a visual orchestration
tool made a lot of sense.


What I'd Do Differently

GTFS data freshness — KMRL updates their GTFS feed inconsistently. I ended
up having to manually refresh it. A proper cron job with a diff check would've
been cleaner.

Session management — WhatsApp conversations don't have a native session
concept. I hacked around it with n8n's static data but a proper Redis session
store would've been better.

The AES reverse engineering — fun to do, nightmare to maintain. If KMRL
ever updates their app, the whole booking flow breaks. A proper API partnership
would obviously be ideal.


Try It / Source

The project is part of my portfolio at midhunpm.in.

I'm Midhun, a CS undergrad at Sahrdaya College of Engineering building AI agents
and full-stack systems. If you're working on something similar or want to talk
transit tech, AI agents, or self-hosted infrastructure — find me on
LinkedIn or
GitHub.


Built with n8n, FastAPI, LangChain, Twilio, Playwright, and way too much
curiosity about how KMRL's app actually works.

Top comments (3)

Collapse
 
citedy profile image
Dmitry Sergeev

did you have to deal with a lot of latency using n8n for the whatsapp trigger or is it pretty snappy?

Collapse
 
midhun_pm_4f9bc1f20dbd1c profile image
Midhun P M

it was pretty laggy and had abit of delay but most of it was due to LLM and the n8n delay was mostly just like 500ms-1500ms max i think 90% of the other delays were just the actual LLM calls and i used a free API so it was kinda slow

Some comments may only be visible to logged-in visitors. Sign in to view all comments.