Download the free source of a Telegram bot that takes the customer’s name, product and phone number step by step and saves the order in a CSV file, with a full guide.
A step-by-step order conversation that turns Telegram into a simple order channel.
Follow these steps in order to get the bot running on your system or server.
pip install requests.TOKEN and run python bot.py./start in Telegram and complete an order; check the orders.csv file.This is the same code as in the ZIP file. You can copy it right from here. Just remember to replace the TOKEN value with your own bot token.
# -*- coding: utf-8 -*-
# ------------------------------------------------------------
# Order-taking Telegram bot | Built by Filtor (filtori.com)
# ------------------------------------------------------------
# This bot runs a step-by-step conversation with the customer,
# takes the order and saves it in the orders.csv file:
# 1) customer name
# 2) the product or service they want
# 3) phone number
# At the end it sends the customer an order summary.
#
# Note: a Telegram bot must run on a server that has access
# to api.telegram.org.
#
# Full guide:
# https://filtori.com/en/telegram-bot-source/order-bot/
# Custom version + panel and payment? +98 912 287 4862
# Our Telegram: https://t.me/filtori
# ------------------------------------------------------------
import csv
import time
from datetime import datetime
import requests
TOKEN = "PASTE_YOUR_BOT_TOKEN_HERE"
BASE = f"https://api.telegram.org/bot{TOKEN}"
# The file where orders are saved (created next to this file)
ORDERS_FILE = "orders.csv"
# ------------------------------------------------------------
# Temporary conversation memory:
# for each user we keep track of which step of the order they are on.
# Note: this memory is in RAM; if the bot restarts it is cleared.
# For a real project it is better to use a database (Filtor can help).
# ------------------------------------------------------------
sessions = {}
def send_message(chat_id, text):
url = f"{BASE}/sendMessage"
try:
requests.post(url, json={"chat_id": chat_id, "text": text}, timeout=15)
except requests.RequestException as error:
print("Error sending message:", error)
def get_updates(offset):
url = f"{BASE}/getUpdates"
params = {"offset": offset, "timeout": 30}
try:
return requests.get(url, params=params, timeout=40).json()
except requests.RequestException as error:
print("Error fetching messages:", error)
return {"ok": False, "result": []}
def save_order(data):
"""Save one complete order to the CSV file."""
is_new_file = False
try:
with open(ORDERS_FILE, "r", encoding="utf-8"):
pass
except FileNotFoundError:
is_new_file = True
with open(ORDERS_FILE, "a", newline="", encoding="utf-8") as file:
writer = csv.writer(file)
if is_new_file:
writer.writerow(["Date", "Name", "Product", "Phone"])
writer.writerow(
[
datetime.now().strftime("%Y-%m-%d %H:%M"),
data["name"],
data["product"],
data["phone"],
]
)
def handle(chat_id, text):
"""Step-by-step order conversation logic."""
if text == "/start":
sessions[chat_id] = {"step": "name", "data": {}}
send_message(chat_id, "Hi 👋 To place an order, please write your full name:")
return
if chat_id not in sessions:
send_message(chat_id, "To start an order, send the /start command.")
return
session = sessions[chat_id]
step = session["step"]
if step == "name":
session["data"]["name"] = text
session["step"] = "product"
send_message(chat_id, "Thanks ✅ Now write which product or service you want:")
elif step == "product":
session["data"]["product"] = text
session["step"] = "phone"
send_message(chat_id, "Great 👌 Finally, write your phone number so we can coordinate with you:")
elif step == "phone":
session["data"]["phone"] = text
data = session["data"]
save_order(data)
send_message(
chat_id,
"Your order was placed successfully ✅\n\n"
f"👤 Name: {data['name']}\n"
f"🛍️ Product: {data['product']}\n"
f"📞 Phone: {data['phone']}\n\n"
"We will contact you soon. Thanks for choosing Filtor 🙏",
)
del sessions[chat_id]
def main():
print("Filtor order bot is running... (Ctrl+C to stop)")
offset = 0
while True:
updates = get_updates(offset)
if not updates.get("ok"):
time.sleep(2)
continue
for update in updates.get("result", []):
offset = update["update_id"] + 1
message = update.get("message")
if not message:
continue
chat_id = message["chat"]["id"]
text = message.get("text", "")
if text:
handle(chat_id, text)
time.sleep(0.5)
if __name__ == "__main__":
main()
Each completed order is saved as a row in the orders.csv file next to the bot, with the date, name, product and phone number. You can open it in Excel.
No. In this sample version the conversation memory is in RAM and is cleared on restart. For a stable version, a database should be used — the Filtor team can help.
Yes, in a professional version. The Filtor team builds a custom order bot with a database, admin panel and payment gateway for your business.
Each completed order is saved as a row in the orders.csv file next to the bot, with the date, name, product and phone number. You can open it in Excel.
No. In this sample version the conversation memory is in RAM and is cleared on restart. For a stable version, a database should be used — the Filtor team can help.
Yes, in a professional version. The Filtor team builds a custom order bot with a database, admin panel and payment gateway for your business.
The Filtor team builds a custom order-taking Telegram bot with a database, admin panel and payment gateway, tailored to your business.