Introduction
In today's digital age, businesses are constantly looking for ways to streamline their operations and improve customer experience. nFuse's approach to B2B ordering through WhatsApp shows how simple messaging platforms can revolutionize business processes. In this tutorial, we'll build a basic WhatsApp ordering system using Python and the Twilio API, which mimics the core functionality that nFuse uses to help small retailers process orders.
This tutorial will teach you how to create a messaging-based ordering system that can receive orders via WhatsApp, process them, and send confirmations back to customers. By the end, you'll understand how messaging platforms can be used for business operations without complex e-commerce infrastructure.
Prerequisites
To follow this tutorial, you'll need:
- A computer with Python installed (version 3.6 or higher)
- A Twilio account with a WhatsApp sandbox environment set up
- Basic understanding of Python programming concepts
- Access to a web browser and internet connection
Step-by-Step Instructions
1. Set Up Your Development Environment
First, we need to install the required Python packages. Open your terminal or command prompt and run:
pip install twilio flask
This installs the Twilio library for handling WhatsApp messages and Flask for creating a web server to receive messages.
2. Create a Twilio Account and Set Up WhatsApp Sandbox
Visit Twilio's WhatsApp page and sign up for a free account. After signing up, you'll need to set up the WhatsApp sandbox environment:
- Go to the Twilio Console
- Navigate to Messaging > WhatsApp > Sandbox
- Follow the instructions to verify your phone number
- Take note of your Account SID and Auth Token - you'll need these later
3. Create the Main Python Application
Create a new file called app.py and add the following code:
from flask import Flask, request, redirect
from twilio.twiml.messaging_response import MessagingResponse
import os
app = Flask(__name__)
# Store orders in memory (in production, use a database)
orders = []
@app.route('/webhook', methods=['GET', 'POST'])
def webhook():
# Get the message the user sent to our WhatsApp number
incoming_msg = request.values.get('Body', '').lower()
# Create a TwiML response
resp = MessagingResponse()
msg = resp.message()
# Process the message
if 'order' in incoming_msg:
# Extract order details (simplified example)
order_details = incoming_msg.replace('order', '').strip()
order_id = len(orders) + 1
orders.append({
'id': order_id,
'details': order_details,
'status': 'received'
})
msg.body(f'Thank you! Your order #{order_id} for "{order_details}" has been received.')
elif 'status' in incoming_msg:
# Check order status
order_id = incoming_msg.replace('status', '').strip()
if order_id and order_id.isdigit():
order = next((o for o in orders if o['id'] == int(order_id)), None)
if order:
msg.body(f'Order #{order_id} status: {order["status"]}')
else:
msg.body('Order not found.')
else:
msg.body('Please provide a valid order number.')
else:
# Provide help message
msg.body('Welcome to our ordering service!\n\nSend: "order [item]" to place an order\nSend: "status [number]" to check order status')
return str(resp)
if __name__ == '__main__':
app.run(debug=True)
This code creates a basic Flask web server that listens for incoming WhatsApp messages. It processes messages to place orders and check order status, demonstrating how nFuse's system might work at a fundamental level.
4. Set Up Your Webhook URL
Since Twilio needs to send messages to your application, you need to make it accessible on the internet. We'll use a tool called ngrok to create a public URL for our local server:
- Download ngrok from https://ngrok.com/
- Start ngrok with:
ngrok http 5000 - Copy the HTTPS URL provided by ngrok (it will look like https://abc123.ngrok.io)
5. Configure Twilio Webhook
Return to your Twilio console and configure the webhook:
- Go to Messaging > WhatsApp > Sandbox
- Under "Webhook URL", enter your ngrok URL followed by /webhook:
https://abc123.ngrok.io/webhook - Set the HTTP method to POST
- Save your changes
6. Test Your Ordering System
Now you can test your system:
- Open WhatsApp on your phone
- Go to the Twilio sandbox number (it's listed in your Twilio console)
- Send "help" to see the available commands
- Try sending "order coffee" to place an order
- Send "status 1" to check the status of your order
7. Enhance Your System (Optional)
To make your system more realistic, you can add features like:
- Database integration to store orders permanently
- Inventory management
- Order confirmation emails
- Integration with payment systems
For example, to add a simple database feature:
import sqlite3
# Add to your Flask app
@app.route('/webhook', methods=['GET', 'POST'])
def webhook():
# ... existing code ...
if 'order' in incoming_msg:
# ... existing order processing ...
# Save to database
conn = sqlite3.connect('orders.db')
c = conn.cursor()
c.execute('CREATE TABLE IF NOT EXISTS orders (id INTEGER PRIMARY KEY, details TEXT, status TEXT)')
c.execute('INSERT INTO orders (details, status) VALUES (?, ?)', (order_details, 'received'))
conn.commit()
conn.close()
# ... rest of code ...
# ... rest of code ...
Summary
In this tutorial, we've built a basic WhatsApp ordering system that demonstrates how nFuse's technology works at its core. We've created a Flask application that listens for WhatsApp messages, processes order requests, and sends responses back to customers.
This simple implementation shows how businesses can leverage messaging platforms to reduce the complexity and cost of traditional e-commerce systems. While our example is basic, it illustrates the fundamental concept behind nFuse's approach - using familiar communication channels to streamline business operations.
Remember, in a production environment, you'd need to add security measures, proper database integration, error handling, and user authentication. But this foundation gives you a practical understanding of how messaging-first platforms can transform business processes.



