πŸ€– AI Chatbot for B2B – Macroferro Industrial Sales Assistant

From vague product queries to automated invoices – how I built a conversation-driven wholesale platform with FastAPI, OpenAI & Telegram Bot API.

If you want a more detailed analysis of the Backend, you can check the README.md in the project’s Github repository.

1. 🎯 Why This Project?

Industrial wholesalers often rely on phone calls or PDF catalogues to take orders. This process is slow, error-prone and only works during office hours. Macroferro demonstrates that an AI-powered chatbot can take over the entire sales funnel – from product discovery to checkout – while integrating seamlessly with existing inventory and ERP systems.

Tech-wise, the goal was to combine modern async Python tooling with state-of-the-art NLP to create a production-ready backend.

2. ⚑ Key Features at a Glance

Capability Description
πŸ” Semantic Search Vector embeddings in Qdrant let the bot understand β€œI need something to fasten wood” and suggest the right screws.
πŸ›’ Full Cart Logic Users add, remove or update quantities in natural language or via slash commands.
🧠 Contextual Understanding Phrases like β€œtell me about the 2nd one” are resolved against the last search result list.
🏷️ Customer Recognition Returning clients get autofilled checkout forms and personalised suggestions.
πŸ“„ Automatic Invoicing The backend generates PDF invoices and emails them via SendGrid.
☁️ Cloud-Native Stack Dockerised FastAPI backend, PostgreSQL, Redis and Qdrant – ready for any cloud.

3. πŸ—οΈ Architecture Overview

Handlers everywhere. The bot logic is split into ProductHandler, CartHandler, CheckoutHandler and an AIAnalyzer that interprets every message with GPT-4o.

3.1 πŸ”§ Detailed Layer Architecture

IMG – search flow

3.2 πŸ“Š Data Flow Between Layers

  1. πŸ“± User message β†’ Telegram webhook
  2. πŸšͺ API endpoint receives and validates request
  3. 🧠 AIAnalyzer processes intent with GPT-4o
  4. 🧠 Specialized handler executes business logic
  5. πŸ’Ύ CRUD operations handle data access
  6. πŸ—„οΈ Database queries across PostgreSQL/Redis/Qdrant
  7. πŸ”Œ External APIs called when needed (OpenAI/SendGrid)
  8. 🧠 Response generation creates appropriate reply
  9. πŸšͺ JSON response returned via FastAPI
  10. πŸ“± Message delivery through Telegram Bot API

This layered architecture ensures separation of concerns, testability, and scalability – each layer has a single responsibility and can be modified independently.

4. 🎬 Demo Walk-Through

4.1 πŸ‘€ New Customer Journey

Click to enlarge GIFs

  1. Product Discovery
    GIF – search flow
  2. Adding & Updating Cart
    GIF – cart flow
  3. Guided Checkout
    GIF – returning customer
  4. Generated Invoice PDF
    Screenshot – invoice Screenshot – invoice2

4.2 πŸ”„ Returning Customer Journey

Screenshot – checkout questions

4.3 πŸ’Ύ Behind the Scenes – Database Operations

Real system data powering the conversational experience

PostgreSQL Tables
PgAdmin showing 250+ products, categories, and processed orders

Qdrant Vector Dashboard
Vector database with semantic search index – enabling β€œI need screws for wood” queries

All flows are captured step-by-step in docs/FLUJO_INTERACCION.md.

5. πŸ”§ System Deep Dive

5.1 πŸ’Ύ Data Layer in Action

From conversation to database – this bot update the stock levels in sync in real time too.here’s how data flows through the system:

PostgreSQL – Structured Data

PostgreSQL Schema
Relational data: Products β†’ Categories β†’ Orders β†’ Clients with real production data

Qdrant – Semantic Search Engine

Qdrant Dashboard
200 product embeddings with search metrics – powering natural language understanding

5.2 πŸš€ API Layer

FastAPI Documentation
25+ REST endpoints with automatic OpenAPI documentation and interactive testing

5.3 🧠 NLP Pipeline

  1. Intent Detection – open-domain prompt with few-shot examples.
  2. Entity Extraction – regular expressions + GPT correction.
  3. Fallback Logic – keyword match and semantic search if GPT is uncertain.

5.4 πŸš€ DevOps & Infrastructure

Docker Services
All services orchestrated with Docker Compose – ready for any cloud deployment

  • 🐳 Docker Compose, Makefile targets for everyday tasks.
  • βš™οΈ GitHub Actions CI: Ruff, Black, MyPy, PyTest.
  • πŸͺ Pre-commit hooks keep the repo clean.

6. 🚧 Challenges & Lessons Learned

  • Resolving β€œthe 4th one.” Index preservation in recent results required a custom batch insert to Redis.

  • Asynchronous Background Jobs. PDF generation + email must NOT reuse FastAPI DB sessions – fixed by spawning fresh connections.

  • Prompt Robustness. Small prompt tweaks (β€œIf you are not 80 % confident, ask back”) improved accuracy significantly.

7. πŸ“‹ Roadmap

  • πŸ’³ Stripe payments integration.
  • πŸ“Š Admin dashboard (React) for inventory & orders.
  • 🌍 Multilingual support (Spanish/English).

8. πŸ“ Repository & Live Demo

  • πŸ’» Code: https://github.com/dasafo/Macroferro
  • πŸ€– Demo Bot: @ferreteria_IA_bot (you can create another bot, all you need is following the instructions and configure the paramenters!!)

Questions? Feedback? Ping me on LinkedIn – I’d love to chat about conversational commerce!