Skip to content

Pharmacy Order Automation

WhatsApp pharmacy ordering: AI reads requests and prescriptions, auto-reserves safe orders, and routes the rest to a pharmacist on Telegram.

n8nGeminiNext.jsTypeScriptGoogle SheetsWhatsAppTelegram
Demo only. Not medical advice. Not a real pharmacy. Customer names are synthetic and uploaded images are never stored.

Interactive pharmacy workflow simulator

Pharmacy Order Automation

WhatsApp Business

Pharmacy Orders - Google Sheet

Read-only

Case study

How it's built

The problem

Neighborhood pharmacies take dozens of WhatsApp messages an hour: voice-like Arabic slang, misspelled English brand names, multi-item lists, and blurry prescription photos. Staff end up manually checking shelf stock, translating alternatives, and writing down holds that never expire.

I built Pharmacy Order Automation to automate safe over-the-counter reservations while keeping a licensed pharmacist strictly in control of every prescription medicine and stock exception.

How it works

The browser only talks to Next.js route handlers under /api/pharmacy/*. Each handler validates requests with zod, checks image size and magic bytes, enforces rate limits, and calls protected n8n webhooks with a server-only secret header.

Browser
Next.js /api/pharmacy/*
n8n workflows
Gemini
Google Sheets
Telegram
The browser never sees the n8n URL, secret header, or uploaded photo storage.

Routing rules

Every matched item creates a normalized line row (R-YYMMDD-XXXX) and routes through deterministic safety rules:

ConditionOutcome
No prescription needed & in stockAuto-reserves stock immediately for 2 hours, deducts Google Sheet inventory, and replies in English & Arabic with pickup total.
Prescription needed (photo uploaded or catalog.requires_rx)Saves rows with status pending, tells the customer a pharmacist is reviewing, and posts an Approve/Reject card on Telegram. Stock is NOT deducted yet.
Prescription needed & out of stockTelegram card flags out of stock with a suggested alternative (name, code, price). Approve is blocked until stock exists; Reject remains enabled.
No prescription needed & out of stockSkips reservation and replies on WhatsApp that the item is unavailable while suggesting the configured alternative.

Tech decisions

  • n8n for workflow orchestration. Separate event-driven flows handle customer messages, Telegram callback approvals, and a 2-hour scheduled expiry sweep with built-in retries.
  • Thin Next.js security boundary. N8N_PHARMACY_BASE_URL and N8N_SECRET never reach client bundles; both inputs and upstream n8n JSON responses are checked against zod schemas.
  • Google Sheets as transparent ops state. Non-technical staff can inspect live stock, reservations, and errors directly without requiring a custom backoffice first.
  • AI extracts, humans approve. Gemini 2.5 Flash only parses messy text/photos into structured medicine arrays. Only a pharmacist clicking Telegram inline callbacks can approve a prescription.

What I'd improve

  • Replace Google Sheets with Postgres row-level locking (SELECT ... FOR UPDATE) so concurrent reservations never race on the last unit in stock.
  • Integrate directly with pharmacy POS/ERP inventory instead of periodic catalog sync.
  • Add OCR confidence scoring and automatic pharmacist quantity/medicine editing right inside Telegram.
  • Push real-time Server-Sent Events (SSE) from n8n completion webhooks to eliminate client polling.