Advanced: E-Commerce Bot
Build a complete shopping experience with product browsing, cart management, checkout, and order tracking. This tutorial showcases nearly every node type available.
What We're Building
A full e-commerce bot that:
- 📦 Displays a product catalog with images
- 🛒 Manages a shopping cart
- 💳 Processes checkout with validation
- 📧 Sends order confirmations
- 🔔 Notifies admins of new orders
- 📊 Tracks order status
Prerequisites
- Completed the basic tutorials
- Understanding of variables and conditions
- Familiarity with keyboards and buttons
Part 1: Setting Up the Product Catalog
Step 1.1: Initialize Shop Data
First, we'll store our products in a global variable. Create a Startup Trigger flow:
- Add Startup Trigger node
- Add Set Variable node for products:
Variable: global.products
Value:
[
{"id": "prod_001", "name": "Wireless Headphones", "price": 79.99, "category": "electronics", "image": "https://example.com/headphones.jpg"},
{"id": "prod_002", "name": "Smart Watch", "price": 199.99, "category": "electronics", "image": "https://example.com/watch.jpg"},
{"id": "prod_003", "name": "Bluetooth Speaker", "price": 49.99, "category": "electronics", "image": "https://example.com/speaker.jpg"},
{"id": "prod_004", "name": "Cotton T-Shirt", "price": 24.99, "category": "clothing", "image": "https://example.com/tshirt.jpg"},
{"id": "prod_005", "name": "Running Shoes", "price": 89.99, "category": "clothing", "image": "https://example.com/shoes.jpg"}
]
- Add Set Variable for categories:
Variable: global.categories
Value:
[
{"id": "electronics", "name": "📱 Electronics", "emoji": "📱"},
{"id": "clothing", "name": "👕 Clothing", "emoji": "👕"}
]
Step 1.2: Create the Shop Entry Point
-
Add Command Trigger for
/shop -
Add Condition to check if user has a cart:
- Expression:
var.cart != nil - True: Skip initialization
- False: Initialize empty cart
- Expression:
-
Add Set Variable for new users:
- Variable:
var.cart - Value:
[]
- Variable:
-
Add Send Message with category menu:
🛍️ Welcome to Our Shop!
Browse our categories below:
- Add Reply Markup with inline buttons:
- 📱 Electronics |
category_electronics - 👕 Clothing |
category_clothing - 🛒 View Cart (
{{len(var.cart)}}) |view_cart
- 📱 Electronics |
Part 2: Browsing Products
Step 2.1: Handle Category Selection
-
Add Message Trigger for callbacks matching
category_* -
Add Transform node to extract category ID:
- Expression:
replace(ctx.Input, "category_", "") - Save to
flow.selected_category
- Expression:
-
Add Filter node to get products in category:
- Input:
global.products - Condition:
item.category == flow.selected_category - Save to
flow.category_products
- Input:
-
Add Set Variable:
- Variable:
flow.product_index - Value:
0
- Variable:
Step 2.2: Display Product Card
Create a reusable product display pattern:
-
Add Get node to get current product:
- Array:
flow.category_products - Index:
flow.product_index - Save to
flow.current_product
- Array:
-
Add Send Photo node:
- Photo:
{{flow.current_product.image}} - Caption:
`{{flow.current_product.name}}`
💰 Price: $`{{flow.current_product.price}}`
`{{flow.product_index + 1}}` of `{{len(flow.category_products)}}` - Photo:
-
Add Reply Markup with navigation:
- ⬅️ Previous |
nav_prev - ➡️ Next |
nav_next - 🛒 Add to Cart |
add_{{flow.current_product.id}} - 🏠 Back to Menu |
back_menu
- ⬅️ Previous |
Step 2.3: Product Navigation
- Add Message Trigger for
nav_prevcallback - Add Condition:
flow.product_index > 0 - If True:
- Transform:
flow.product_index - 1 - Update display (call product display subflow)
- Transform:
- If False:
- Answer Callback: "Already at first product"
Similar for nav_next:
- Condition:
flow.product_index < len(flow.category_products) - 1
Part 3: Shopping Cart Management
Step 3.1: Add to Cart
-
Add Message Trigger for callbacks matching
add_* -
Add Transform to extract product ID:
- Expression:
replace(ctx.Input, "add_", "") - Save to
flow.adding_product_id
- Expression:
-
Add Filter to find the product:
- Input:
global.products - Condition:
item.id == flow.adding_product_id - Get first result
- Input:
-
Add Transform to add to cart:
- Expression:
append(var.cart, flow.product_to_add)
- Expression:
-
Update
var.cartwith new array -
Answer Callback Query:
✅ Added `{{flow.product_to_add.name}}` to cart!
Step 3.2: View Cart
- Add Message Trigger for
view_cartcallback - Add Condition:
len(var.cart) == 0
If empty:
🛒 Your cart is empty!
Browse our /shop to add items.
If has items:
-
Add Map node to format cart items:
- Expression:
"• " + item.name + " - $" + string(item.price)
- Expression:
-
Add Transform to calculate total:
- Expression:
sum(var.cart, "price") - Save to
flow.cart_total
- Expression:
-
Add Send Message:
🛒 Your Cart (`{{len(var.cart)}}` items)
`{{join(flow.formatted_items, "\n")}}`
━━━━━━━━━━━━━━━━━
💰 Total: $`{{flow.cart_total}}`
- Add Reply Markup:
- 🗑️ Clear Cart |
clear_cart - ✅ Checkout |
checkout - 🏠 Continue Shopping |
back_menu
- 🗑️ Clear Cart |
Step 3.3: Clear Cart
- Add Message Trigger for
clear_cart - Add Set Variable:
- Variable:
var.cart - Value:
[]
- Variable:
- Add Edit Message:
🗑️ Cart cleared!
Browse our /shop to add items.
Part 4: Checkout Flow
Step 4.1: Start Checkout
- Add Message Trigger for
checkoutcallback - Add Condition:
len(var.cart) > 0
If empty: Redirect to shop
If has items:
-
Add Set Variable to start checkout:
flow.checkout_step="name"
-
Add Send Message:
📦 Checkout
Let's complete your order! I'll need a few details.
Step 1 of 4: What's your full name?
Step 4.2: Collect Customer Information
Name Collection:
- Add Ask node:
- Question: Already sent above
- Expect:
text - Validation:
len(value) >= 2 - Error: "Please enter your full name (at least 2 characters)"
- Save to
flow.customer_name
Email Collection:
- Add Send Message:
Step 2 of 4: What's your email address?
- Add Ask node:
- Expect:
text - Validation:
contains(value, "@") && contains(value, ".") - Error: "Please enter a valid email address"
- Save to
flow.customer_email
- Expect:
Phone Collection:
- Add Send Message:
Step 3 of 4: What's your phone number?
- Add Ask node:
- Expect:
text - Validation:
len(replace(value, " ", "")) >= 10 - Error: "Please enter a valid phone number"
- Save to
flow.customer_phone
- Expect:
Address Collection:
- Add Send Message:
Step 4 of 4: What's your delivery address?
- Add Ask node:
- Expect:
text - Validation:
len(value) >= 10 - Error: "Please enter your complete address"
- Save to
flow.customer_address
- Expect:
Step 4.3: Order Confirmation
-
Add Transform to generate order ID:
- Expression:
"ORD-" + string(now().Unix())[4:] - Save to
flow.order_id
- Expression:
-
Add Send Message with order summary:
📋 Order Summary
Order ID: `{{flow.order_id}}`
━━━━━━━━━━━━━━━━━
📦 Items:
`{{join(flow.formatted_items, "\n")}}`
💰 Total: $`{{flow.cart_total}}`
━━━━━━━━━━━━━━━━━
📍 Delivery Details:
Name: `{{flow.customer_name}}`
Email: `{{flow.customer_email}}`
Phone: `{{flow.customer_phone}}`
Address: `{{flow.customer_address}}`
Please confirm your order:
- Add Reply Markup:
- ✅ Confirm Order |
confirm_order - ❌ Cancel |
cancel_order
- ✅ Confirm Order |
Step 4.4: Process Order
-
Add Message Trigger for
confirm_order -
Add Set Variable to save order:
- Variable:
var.orders - Value:
append(var.orders ?? [], {
"id": flow.order_id,
"items": var.cart,
"total": flow.cart_total,
"customer": {
"name": flow.customer_name,
"email": flow.customer_email,
"phone": flow.customer_phone,
"address": flow.customer_address
},
"status": "pending",
"created_at": now()
}) - Variable:
-
Add Set Variable to clear cart:
var.cart=[]
-
Add Edit Message:
🎉 Order Confirmed!
Your order `{{flow.order_id}}` has been placed successfully.
📧 Confirmation email sent to `{{flow.customer_email}}`
You can track your order with:
/track `{{flow.order_id}}`
Thank you for shopping with us! 💜
Part 5: Admin Notifications
Step 5.1: Notify Admin
After order confirmation, add:
- Add Send Message node with Chat ID set to
{{global.admin_chat_id}}:
🔔 NEW ORDER
Order: `{{flow.order_id}}`
Customer: `{{flow.customer_name}}`
Email: `{{flow.customer_email}}`
Phone: `{{flow.customer_phone}}`
Items:
`{{join(flow.formatted_items, "\n")}}`
Total: $`{{flow.cart_total}}`
Address: `{{flow.customer_address}}`
- Add Reply Markup for admin:
- ✅ Accept |
admin_accept_{{flow.order_id}} - ❌ Cancel |
admin_cancel_{{flow.order_id}} - 📦 Ship |
admin_ship_{{flow.order_id}}
- ✅ Accept |
Part 6: Order Tracking
Step 6.1: Track Command
-
Add Command Trigger for
/track -
Add Transform to get order ID from args:
- Expression:
ctx.Args[0] ?? "" - Save to
flow.tracking_id
- Expression:
-
Add Condition:
flow.tracking_id != ""
If no ID provided:
📦 Order Tracking
Please provide your order ID:
/track ORD-XXXXX
Or use /orders to see all your orders.
If ID provided:
-
Add Filter to find order:
- Input:
var.orders - Condition:
item.id == flow.tracking_id
- Input:
-
Add Condition:
len(flow.found_orders) > 0
If found:
📦 Order Status
Order: `{{flow.order.id}}`
Status: `{{flow.order.status == "pending" ? "⏳ Processing" : flow.order.status == "shipped" ? "🚚 Shipped" : "✅ Delivered"}}`
Items: `{{len(flow.order.items)}}` items
Total: $`{{flow.order.total}}`
Ordered: `{{flow.order.created_at}}`
If not found:
❌ Order not found
Please check your order ID and try again.
Complete Flow Diagram
Nodes Used
| Category | Nodes |
|---|---|
| Triggers | Command, Message/Callback, Startup |
| Actions | Send Message, Send Photo, Edit Message, Answer Callback |
| Logic | Condition, Switch |
| Flow | Ask, Form |
| Data | Set Variable, Get, Filter, Map, Transform, Append |
| Utility | Delay |
Enhancement Ideas
- Payment Integration: Connect to Stripe/PayPal
- Inventory Tracking: Deduct stock on order
- Discount Codes: Add promo code support
- Order History:
/orderscommand - Wishlist: Save products for later
Next Steps
- Booking System → — Appointment scheduling
- Multi-Language Bot → — Internationalization