Skip to main content

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:

  1. Add Startup Trigger node
  2. 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"}
]
  1. 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

  1. Add Command Trigger for /shop

  2. Add Condition to check if user has a cart:

    • Expression: var.cart != nil
    • True: Skip initialization
    • False: Initialize empty cart
  3. Add Set Variable for new users:

    • Variable: var.cart
    • Value: []
  4. Add Send Message with category menu:

🛍️ Welcome to Our Shop!

Browse our categories below:
  1. Add Reply Markup with inline buttons:
    • 📱 Electronics | category_electronics
    • 👕 Clothing | category_clothing
    • 🛒 View Cart ({{len(var.cart)}}) | view_cart

Part 2: Browsing Products

Step 2.1: Handle Category Selection

  1. Add Message Trigger for callbacks matching category_*

  2. Add Transform node to extract category ID:

    • Expression: replace(ctx.Input, "category_", "")
    • Save to flow.selected_category
  3. Add Filter node to get products in category:

    • Input: global.products
    • Condition: item.category == flow.selected_category
    • Save to flow.category_products
  4. Add Set Variable:

    • Variable: flow.product_index
    • Value: 0

Step 2.2: Display Product Card

Create a reusable product display pattern:

  1. Add Get node to get current product:

    • Array: flow.category_products
    • Index: flow.product_index
    • Save to flow.current_product
  2. 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)}}`
  3. Add Reply Markup with navigation:

    • ⬅️ Previous | nav_prev
    • ➡️ Next | nav_next
    • 🛒 Add to Cart | add_{{flow.current_product.id}}
    • 🏠 Back to Menu | back_menu

Step 2.3: Product Navigation

  1. Add Message Trigger for nav_prev callback
  2. Add Condition: flow.product_index > 0
  3. If True:
    • Transform: flow.product_index - 1
    • Update display (call product display subflow)
  4. 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

  1. Add Message Trigger for callbacks matching add_*

  2. Add Transform to extract product ID:

    • Expression: replace(ctx.Input, "add_", "")
    • Save to flow.adding_product_id
  3. Add Filter to find the product:

    • Input: global.products
    • Condition: item.id == flow.adding_product_id
    • Get first result
  4. Add Transform to add to cart:

    • Expression: append(var.cart, flow.product_to_add)
  5. Update var.cart with new array

  6. Answer Callback Query:

    ✅ Added `{{flow.product_to_add.name}}` to cart!

Step 3.2: View Cart

  1. Add Message Trigger for view_cart callback
  2. Add Condition: len(var.cart) == 0

If empty:

🛒 Your cart is empty!

Browse our /shop to add items.

If has items:

  1. Add Map node to format cart items:

    • Expression: "• " + item.name + " - $" + string(item.price)
  2. Add Transform to calculate total:

    • Expression: sum(var.cart, "price")
    • Save to flow.cart_total
  3. Add Send Message:

🛒 Your Cart (`{{len(var.cart)}}` items)

`{{join(flow.formatted_items, "\n")}}`

━━━━━━━━━━━━━━━━━
💰 Total: $`{{flow.cart_total}}`
  1. Add Reply Markup:
    • 🗑️ Clear Cart | clear_cart
    • ✅ Checkout | checkout
    • 🏠 Continue Shopping | back_menu

Step 3.3: Clear Cart

  1. Add Message Trigger for clear_cart
  2. Add Set Variable:
    • Variable: var.cart
    • Value: []
  3. Add Edit Message:
    🗑️ Cart cleared!

    Browse our /shop to add items.

Part 4: Checkout Flow

Step 4.1: Start Checkout

  1. Add Message Trigger for checkout callback
  2. Add Condition: len(var.cart) > 0

If empty: Redirect to shop

If has items:

  1. Add Set Variable to start checkout:

    • flow.checkout_step = "name"
  2. 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:

  1. 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:

  1. Add Send Message:
Step 2 of 4: What's your email address?
  1. Add Ask node:
    • Expect: text
    • Validation: contains(value, "@") && contains(value, ".")
    • Error: "Please enter a valid email address"
    • Save to flow.customer_email

Phone Collection:

  1. Add Send Message:
Step 3 of 4: What's your phone number?
  1. Add Ask node:
    • Expect: text
    • Validation: len(replace(value, " ", "")) >= 10
    • Error: "Please enter a valid phone number"
    • Save to flow.customer_phone

Address Collection:

  1. Add Send Message:
Step 4 of 4: What's your delivery address?
  1. Add Ask node:
    • Expect: text
    • Validation: len(value) >= 10
    • Error: "Please enter your complete address"
    • Save to flow.customer_address

Step 4.3: Order Confirmation

  1. Add Transform to generate order ID:

    • Expression: "ORD-" + string(now().Unix())[4:]
    • Save to flow.order_id
  2. 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:
  1. Add Reply Markup:
    • ✅ Confirm Order | confirm_order
    • ❌ Cancel | cancel_order

Step 4.4: Process Order

  1. Add Message Trigger for confirm_order

  2. 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()
    })
  3. Add Set Variable to clear cart:

    • var.cart = []
  4. 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:

  1. 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}}`
  1. Add Reply Markup for admin:
    • ✅ Accept | admin_accept_{{flow.order_id}}
    • ❌ Cancel | admin_cancel_{{flow.order_id}}
    • 📦 Ship | admin_ship_{{flow.order_id}}

Part 6: Order Tracking

Step 6.1: Track Command

  1. Add Command Trigger for /track

  2. Add Transform to get order ID from args:

    • Expression: ctx.Args[0] ?? ""
    • Save to flow.tracking_id
  3. 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:

  1. Add Filter to find order:

    • Input: var.orders
    • Condition: item.id == flow.tracking_id
  2. 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

CategoryNodes
TriggersCommand, Message/Callback, Startup
ActionsSend Message, Send Photo, Edit Message, Answer Callback
LogicCondition, Switch
FlowAsk, Form
DataSet Variable, Get, Filter, Map, Transform, Append
UtilityDelay

Enhancement Ideas

  1. Payment Integration: Connect to Stripe/PayPal
  2. Inventory Tracking: Deduct stock on order
  3. Discount Codes: Add promo code support
  4. Order History: /orders command
  5. Wishlist: Save products for later

Next Steps