I've been selling used phones online for about ten years. In all that time, the messages have barely changed. Most of them are some version of two questions:

"Do you have it?" and "How much is it?"

They come in the evening, at the weekend, while I'm in the middle of fixing a phone, and they come in Serbian, English and sometimes Romani. If I don't answer quickly, the buyer has already moved on to the next seller.

So I built a bot that answers those two questions for me on Instagram, and hands everything else back to me. This post covers how it works and which decisions turned out to matter most.

The one rule: the AI never writes the answer

The scariest thing a shop bot can do is say something false with confidence, like telling a buyer a phone is in stock when it was sold yesterday, or quoting the wrong price.

So I split the job in two:

  1. The AI only understands the message. It returns a small JSON object: what the buyer wants (stock, price, greeting, thanks, or something else), which product they mean, which language they wrote in, and how confident it is.
  2. The answer comes from a template. The bot looks the product up in my stock list and fills in a reply template I wrote myself.
export function fill(text: string, vars: Record<string, string>): string { return text.replace(/\{(\w+)\}/g, (m, k: string) => (k in vars ? vars[k] : m)) } 

A template looks like Da, imamo {name}{details}, cena {price}.{link}. The price, the condition and the link all come from the stock record, never from the model. The bot can be wrong about which phone you mean, but it can't make up a price.

If the model is unsure (confidence under 0.5), or the question is about something else, like repairs, delivery or bargaining, the bot doesn't guess. It sends a short "the owner will reply soon" message and alerts me on WhatsApp with the buyer's text. Photos and voice messages go straight to me as well.

For understanding, the bot tries Gemini first and falls back to Groq. If no model answers at all, a keyword-based fallback still handles the basics.

My Instagram posts are the stock list

I didn't want to keep a second inventory in sync by hand. Every phone I sell already has an Instagram post with a caption like:

Samsung Galaxy A14, 4/128gb, perfektno stanje, ekstra ponuda. … Cena: 75€.

So the bot imports my posts and parses the captions: model, memory, condition, price and currency. A handful of regular expressions do most of the work, and the AI only steps in when the first line isn't a model name. Posts without a price, like repair announcements, are skipped. Every product keeps a link to its post, and the bot sends that link with the answer, so the buyer sees the photos right away.

A few rules made the import safe to run automatically:

  • On the first import, everything is marked as sold. Most of my older posts are long gone, so I switch the few that are still available back to "in stock". Starting from "sold" means the bot can't offer a phone that no longer exists.
  • Later imports add new posts as "in stock", and they also run when a buyer writes, at most every 30 minutes. A phone I posted ten minutes ago is already known.
  • Manual edits are never overwritten. If a caption later says PRODATO (sold), or the post is deleted, the product is marked as sold.

Matching takes memory into account, so "S21 FE 6/128" and "S21 Ultra 12/256" stay separate, and a general "imaš li S21?" ("do you have an S21?") lists every S21 currently in stock. It also handles Cyrillic, missing diacritics and the many ways people spell "Galaxy".

Making sure the bot stays out of my way

A bot that talks over the owner is worse than no bot. A few safeguards:

  • When I reply in a chat myself, the bot goes quiet there for 12 hours by default.
  • At most 6 bot replies per chat per hour. If it ever ends up talking to another bot, the loop stops on its own.
  • Every webhook is verified with Meta's signature, duplicate deliveries are ignored, and the bot never answers its own messages.

An admin panel that works with the keyboard

Stock, reply templates and settings all live in a small admin panel. I use a screen reader, so I built it to work completely from the keyboard. It works just as well with a mouse, which is the point. Every action button names the item it acts on ("Mark as sold: iPhone 11"), and the result of each action is announced and focused. I tested it with axe-core in light and dark mode, with no violations.

The page I use most has a "Try the bot" box: I type a buyer's message, see exactly what the bot would answer, and nothing gets sent. I check every template change there before it goes live.

Alerts on WhatsApp

When the bot hands a chat to me, I get a WhatsApp message with what the buyer wrote. Since the bot already lives in a Meta app, adding WhatsApp was the natural choice: the same app gets the WhatsApp product, and Meta gives every app a test number that can message up to five numbers you verify yourself, free for 90 days and with no payment method required. For alerts that only go to me, that's all I need to get started, and Telegram stays as a free backup channel.

There's one catch. A business can only start a WhatsApp chat with an approved message template, so the alert is a one-line template, Instagram bot ti prosleđuje poruku kupca: {{1}}. Odgovori mu u Instagram porukama. ("The Instagram bot is forwarding a buyer's message: … Reply in Instagram DMs."), with the buyer's message as the variable. Meta won't approve a template that starts or ends with a variable, so the variable sits in the middle. Template variables can't contain line breaks, so the bot joins the lines with " · " before sending.

The alert channel itself is the easiest part to swap. The bot sends to WhatsApp and Telegram today, and the same hand-off could just as well go to Viber, email, SMS or Slack, whichever app the shop owner actually checks.

Privacy

The bot stores no names, usernames or profile data. Conversations are tracked by a hashed ID only. Message text is kept only when a conversation is handed to me, so I can answer it. The stock data lives in a private Sanity dataset.

The stack

  • Next.js on Vercel, TypeScript, with a daily cron job for the import
  • Instagram API with Instagram Login (Meta webhooks) for messages
  • Sanity for stock, templates and hand-offs
  • Gemini, with Groq as a fallback, for understanding messages
  • WhatsApp Cloud API for alerts (Telegram works too)

What I'd tell anyone building a shop bot

Let the AI do the part it's good at, which is understanding messy human messages, and keep it away from facts it can't know: prices, stock, promises. Templates sound less clever than free-form answers, but buyers don't want clever. They want "yes, 75 €, here are the photos" at 2 a.m.

I build bots like this for other small businesses too: for Instagram, WhatsApp, Telegram and Viber. If you have a shop that gets the same five questions all day, feel free to get in touch.

This article was written with the help of AI.