what it does
A Telegram bot workflow that lists free class slots for the next 7 days from Postgres and lets students book or cancel seats, using a partial unique index and ON CONFLICT DO NOTHING to prevent double-booking. Includes SQL schema, sample callback input and booking output, and a 29-second demo video.
A Telegram bot that lets students book studio classes — and refuses to double-book the same seat, even when two people tap at the same second.
Students type /slots. The bot reads the next 7 days from Postgres, removes the seats already taken, and sends the free ones as tappable buttons. Tapping one writes the booking. /cancel <id> releases it, and only for the student who made it.
WHY IT CANNOT DOUBLE-BOOK
bookings has a partial unique index on (room_id, starts_at) where status = 'confirmed', and the insert is ON CONFLICT DO NOTHING. When two people race, one insert wins and the other returns zero rows. The Check booking node turns zero rows into "That slot was just taken" instead of writing a second row.
DATABASE SETUP — run this once in your Postgres:
CREATE TABLE classes (
class_id text PRIMARY KEY,
class_name text NOT NULL,
room_id text NOT NULL,
seats int NOT NULL DEFAULT 10,
starts_at timestamptz NOT NULL,
duration_min int NOT NULL DEFAULT 60
);
CREATE TABLE bookings (
booking_id bigserial PRIMARY KEY,
class_id text NOT NULL,
room_id text NOT NULL,
starts_at timestamptz NOT NULL,
chat_id text NOT NULL,
status text NOT NULL DEFAULT 'confirmed'
);
CREATE UNIQUE INDEX one_booking_per_room_slot
ON bookings (room_id, starts_at)
WHERE status = 'confirmed';
COLUMNS THE WORKFLOW READS AND WRITES
classes: class_id (text, PK), class_name (text), room_id (text), seats (int), starts_at (timestamptz), duration_min (int)
bookings: booking_id (bigserial, PK), class_id (text), room_id (text), starts_at (timestamptz), chat_id (text), status (text)
SAMPLE INPUT — the callback when a student taps a slot:
{"callback_query": {"data": "book|yoga_mon|studio_a|1795166400000", "from": {"id": 771203944}}}
SAMPLE OUTPUT — the row that lands in bookings:
1184 | yoga_mon | studio_a | 2026-11-21 18:00+07 | 771203944 | confirmed
A second tap on the same slot returns zero rows, so the bot replies "That slot was just taken". The 29-second demo video on this page shows exactly that: the canvas, then the two taps side by side.
SETUP — about 10 minutes
1. Import the JSON into n8n: Workflows, then Import from File, pick the downloaded .json, then Save.
2. Run the SQL above in your Postgres.
3. Attach your Postgres credential to Load classes, Load bookings, Try to book and Cancel booking.
4. Create a bot with @BotFather, copy the token, and attach a Telegram credential to Telegram Trigger, Send slots, Reply and Confirm cancel.
5. Open Settings and set your studio name and timezone offset if yours is not +07:00.
6. Activate the workflow, then send /slots to your bot.
LIMITS TO KNOW
/slots reads the next 7 days and at most 60 classes, which keeps the button list inside Telegram's inline keyboard limit. Each confirmed booking is one row; the only seat cap is the seats value you set per class. Postgres has no practical row limit at this size.
Requirements: your own n8n instance, a Telegram bot token, and a PostgreSQL database (Supabase, Neon, RDS or self-hosted all work).
Tested on 11 cases: same-room conflicts, bookings in UTC against classes in +07:00, out-of-hours classes, invalid times, malformed callback data, and both booking outcomes.