π€ 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 anAIAnalyzer
that interprets every message with GPT-4o.
3.1 π§ Detailed Layer Architecture
3.2 π Data Flow Between Layers
- π± User message β Telegram webhook
- πͺ API endpoint receives and validates request
- π§ AIAnalyzer processes intent with GPT-4o
- π§ Specialized handler executes business logic
- πΎ CRUD operations handle data access
- ποΈ Database queries across PostgreSQL/Redis/Qdrant
- π External APIs called when needed (OpenAI/SendGrid)
- π§ Response generation creates appropriate reply
- πͺ JSON response returned via FastAPI
- π± 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
- Product Discovery
- Adding & Updating Cart
- Guided Checkout
- Generated Invoice PDF
4.2 π Returning Customer Journey
4.3 πΎ Behind the Scenes – Database Operations
Real system data powering the conversational experience
PgAdmin showing 250+ products, categories, and processed orders
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
Relational data: Products β Categories β Orders β Clients with real production data
Qdrant – Semantic Search Engine
200 product embeddings with search metrics – powering natural language understanding
5.2 π API Layer
25+ REST endpoints with automatic OpenAPI documentation and interactive testing
5.3 π§ NLP Pipeline
- Intent Detection β open-domain prompt with few-shot examples.
- Entity Extraction β regular expressions + GPT correction.
- Fallback Logic β keyword match and semantic search if GPT is uncertain.
5.4 π DevOps & Infrastructure
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!