How to Create a WhatsApp E-commerce Bot with Twilio and Go
Time to read: 5 minutes
How to Create a WhatsApp Ecommerce Bot with Twilio and Go
In today’s digital era, businesses increasingly leverage messaging platforms like WhatsApp to enhance customer engagement. As one of the most popular messaging apps, WhatsApp allows businesses to effectively utilize its chat features to reach more customers directly within the app.
In order to streamline the online shopping experience, ecommerce businesses can offer their services or products to their customers via a WhatsApp bot. Customers can interact with the bot to view available products, add and remove items from their cart, and review their cart list.
In this tutorial, you will learn how to create an interactive WhatsApp ecommerce bot for your customers using Go and the Twilio WhatsApp Business API.
How the application will work
The WhatsApp ecommerce bot will provide a seamless shopping experience, allowing customers to browse products and manage their cart using predefined commands to communicate with the application backend. These are:
- ADD TO CART: Adds an item to the user's cart
- REGISTER: Registers an account for the user, required before they can start shopping
- REMOVE FROM CART: Removes an item from their shopping cart
- VIEW CART: Views all items in their shopping cart
- VIEW PRODUCTS: Views all available products
Requirements
To complete this tutorial, you will need the following:
- Go 1.23 or higher installed on your computer
- A free or paid active Twilio account. Click here to create one, if you don't have one already.
- Access to a MySQL database
- Ngrok installed on your computer with a connected ngrok account
Create a new Go project
To get started, let's create a new Go project. To do that, open your terminal, navigate to the directory where you want to create the project, and run the commands below to set it up.
After initializing the project, open the project folder in your preferred Go IDE or code editor.
Set the required environment variables
Let's create a .env file to store the application's credentials as environment variables. To do this, inside the project's root directory, create a .env file, and add the following configuration to it.
Now, in the application environment variables above, replace the <db_username>
and <db_password>
placeholders with your MySQL database username and password, respectively.
Create the application's database schema
Next, you need to create database tables to store application data; these are: ''customer'', ''products'', and ''carts''. To do this, log in to your MySQL database server, create a new database named "ecommerce". Then, run the SQL code below in your MySQL client to create the three tables.
The SQL query above will create three tables:
- customers: This stores the customer's phone number and full name.
- products: This stores details of available products, including the product ID, name, price, and image name.
- carts: This stores customer orders by linking customer IDs to the products they have added to their cart.
Retrieve your Twilio credentials
Now, let’s retrieve your Twilio Account SID and Auth Token. To do this, log in to your Twilio Console dashboard. You will find them under the Account Info section, as shown in the screenshot below.


Copy the credentials, then replace the <twilio_account_sid>
and <twilio_auth_token>
placeholders in .env with them.
Connect to the Twilio WhatsApp Sandbox
To connect your test WhatsApp number to the Twilio WhatsApp Sandbox, navigate to Explore Products > Messaging > Try it out > Send a WhatsApp message, from the Twilio Console dashboard menu, as shown in the screenshot below.


Copy the displayed Twilio WhatsApp number and replace the <twilio_whatsapp_number>
placeholder in .env with it. Now, follow the instructions on the Twilio "Try WhatsApp" page by sending the provided join message to the Twilio WhatsApp number, as shown in the screenshot below.


Install the official Twilio Go Helper Library
Next, you need to install the Twilio Go helper library, which simplifies interactions with Twilio services such as Twilio's Business WhatsApp API (and Programmable Messaging, SMS, and Verify) using the commands below.
Install the other required dependencies
Next, let’s install the MySQL driver to connect to the database server, GoDotEnv to load environment variables into the application, and Gorilla/Mux to handle routing. Install these dependencies by running the command below.
Create the WhatsApp ecommerce bot logic
Now, let’s develop the application logic that allows customers to register an account, browse available products, add and remove items from their cart, and view their cart items via WhatsApp messages.
To implement these functionalities, inside the project’s root directory, create a file named main.go, and add the following code.
In the above code, we:
- Import all the necessary application packages, such as MySQL, Twilio, and GoDotEnv, etc.
- The
handleWhatsAppMessage()
function processes incoming WhatsApp bot commands such as "VIEW-PRODUCTS", "ADD-TO-CART", "REMOVE-FROM-CART", and "VIEW-CART" and replies with appropriate messages. - The
sendProductList()
function sends the list of available products to the user whenever they send the "VIEW-PRODUCTS" command. The product image is sent along with the product details using thesendWhatsAppMediaMessage()
function. - The
addToCart()
,removeFromCart()
, andsendCartDetails()
functions manage cart items by adding, removing, and displaying them based on the bot commands received from the customer. - The
main()
function is the application's entry point, where the environment variables are loaded, the database connection is established to the MySQL database server, and the uploads directory is made publicly accessible using thehttp.FileServer()
method. This allows the uploaded product images to be accessible by the Twilio endpoint. Finally, the server is started using thehttp.ListenAndServe()
method.
Add the application template
Next, let’s create the "add-product" interface to allow the admin to add new products to the database. To do this, inside the project’s root folder, create a new folder named templates. Inside this new folder, create a file named add-product.html and add the following code to it.
Configure the Twilio WhatsApp Webhook
To enable the application to process incoming WhatsApp messages, you need to configure the Twilio WhatsApp webhook. First, let’s make the Go application accessible over the internet using ngrok. To do so, run the command below.
This will generate a forwarding URL in your terminal. Copy it as shown in the screenshot below.


In your application .env file, replace the <forwarding-url>
placeholder with the generated forwarding URL.
Next, on the Twilio "Try WhatsApp" page, click on Sandbox Settings and configure the Sandbox with the following settings.
- When a message comes in: paste the generated forwarding URL and append "/webhook"
- Method: "POST"
Then, click the Save button to save the settings, as shown in the screenshot below.


Test the application
Finally, let’s test the WhatsApp ecommerce bot to ensure that the application functions as expected. To do this, let’s start the application development server using the command.
After starting the development server, open http://localhost:8080/add-product in your browser to add new products as shown in the screenshot below.


Next, send a message from your WhatsApp number to the Twilio WhatsApp number. Interact with the ecommerce bot to register an account, browse available products, and add or remove items from your cart by sending the commands below to the Twilio WhatsApp number:
- "REGISTER <FullName>": Send this command to register your ecommerce shopping account
- "VIEW-PRODUCTS": Use this command to view available products
- "ADD-TO-CART <Product_ID>": Use this command to add a product to your cart by replacing <Product_ID> with the actual product ID
- "REMOVE-FROM-CART <Product ID>": Use this command to remove a product from your cart
- "VIEW-CART": Use this command to view the items in your cart
The screenshot below shows how the WhatsApp ecommerce bot works.


That’s how to create a WhatsApp ecommerce bot with Twilio and Go
Building a WhatsApp ecommerce bot with Twilio and Go enables businesses to automate customer interactions, making shopping more convenient. In this tutorial, you have learned how to create an interactive WhatsApp ecommerce bot using the Twilio WhatsApp API and Go.
Popoola Temitope is a mobile developer and a technical writer who loves writing about frontend technologies. He can be reached on LinkedIn.
Related Posts
Related Resources
Twilio Docs
From APIs to SDKs to sample apps
API reference documentation, SDKs, helper libraries, quickstarts, and tutorials for your language and platform.
Resource Center
The latest ebooks, industry reports, and webinars
Learn from customer engagement experts to improve your own communication.
Ahoy
Twilio's developer community hub
Best practices, code samples, and inspiration to build communications and digital engagement experiences.