Skip to content

πŸ›’ Woocommerce telegram bot (Python) .A Telegram bot for WooCommerce allows you to connect your online store to Telegram, enabling features like order notifications, customer interaction, and even product sales directly through the messaging app.

License

Notifications You must be signed in to change notification settings

mscbuild/woocommerce-telegram

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

11 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

woocommerce-telegram (Python)

image

πŸ”§ Use Case Example

  • Let’s say you want to notify users via Telegram when:

  • A new order is placed

  • A product is back in stock

  • A customer leaves a review

πŸ—‚οΈ Project Structure

woocommerce-telegram-bot/
β”‚
β”œβ”€β”€ bot/
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ config.py
β”‚   β”œβ”€β”€ telegram_bot.py         # Bot logic + command handlers
β”‚   β”œβ”€β”€ woo.py                  # WooCommerce API interface
β”‚
β”œβ”€β”€ webhook/
β”‚   β”œβ”€β”€ __init__.py
β”‚   └── server.py               # Flask server for webhook
β”‚
β”œβ”€β”€ requirements.txt
β”œβ”€β”€ run.py                      # Main entry point
└── README.md

🧱 Overview of What You Need

1. Telegram Bot

  • Create a Telegram bot via @BotFather

  • Save the bot token

2. WooCommerce Store

  • WooCommerce REST API must be enabled

  • Get your Consumer Key and Consumer Secret

3. Python Environment

Install required libraries:

pip install python-telegram-bot woocommerce flask

πŸ§ͺ Example Use Case: Telegram Bot for New Orders

Step 1: Basic Telegram Bot Setup (Python)

from telegram import Bot

TELEGRAM_BOT_TOKEN = "YOUR_TELEGRAM_BOT_TOKEN"
CHAT_ID = "YOUR_CHAT_ID"  # Your Telegram user ID or group ID

bot = Bot(token=TELEGRAM_BOT_TOKEN)

def send_telegram_message(message):
bot.send_message(chat_id=CHAT_ID, text=message)

Step 2: WooCommerce API Integration

from woocommerce import API

wcapi = API(
    url="https://yourstore.com",
    consumer_key="ck_xxx",
    consumer_secret="cs_xxx",
    version="wc/v3"
)

def get_latest_orders():
    return wcapi.get("orders").json()

Step 3: Notify on New Order (Polling or Webhook)

Option A: Polling (Not real-time, simpler)

import time

last_order_id = None

while True:
    orders = get_latest_orders()
    if orders:
        latest = orders[0]
        if latest['id'] != last_order_id:
            msg = f"πŸ›’ New Order!\nCustomer: {latest['billing']['first_name']}\nTotal: {latest['total']}"
            send_telegram_message(msg)
            last_order_id = latest['id']
    time.sleep(60)

Option B: Webhook from WooCommerce to Flask (Real-time, better)

1.Create a webhook in WooCommerce:

  • Topic: Order Created

  • Delivery URL: e.g., https://yourdomain.com/webhook

  • Secret: Use something unique

  • Flask app to receive webhook:

from flask import Flask, request

app = Flask(__name__)

@app.route("/webhook", methods=["POST"])
def wc_webhook():
    data = request.json
    msg = f"πŸ›’ New Order!\nCustomer: {data['billing']['first_name']}\nTotal: {data['total']}"
    send_telegram_message(msg)
    return "OK", 200

if __name__ == "__main__":
    app.run(port=5000)

🧩 Optional Features

  • Respond to Telegram commands (/latest_orders)

  • Notify on stock status changes

  • Add custom filters for big orders, VIP clients, etc.

πŸ“¦ Packaging It

  • Run Flask app on cloud (Heroku, Fly.io, etc.)

  • Use ngrok for testing locally with webhooks:

ngrok http 5000

πŸ“ File Contents

  • requirements.txt
  • bot/config.py
  • bot/woo.py
  • bot/telegram_bot.py
  • webhook/server.py
  • run.py

πŸš€ How to Run Locally

1.Install dependencies:

pip install -r requirements.txt

2.Start the bot:

python run.py

3.Run webhook server (for real-time WooCommerce updates):

python webhook/server.py

4.(Optional) Use ngrok to expose webhook to WooCommerce:

ngrok http 5000
  • Then set this https://xxxx.ngrok.io/webhook as the webhook URL in your WooCommerce admin panel under:

  • WooCommerce β†’ Settings β†’ Advanced β†’ Webhooks

πŸ’¬ Available Telegram Commands

Command Description
/start Greet the user
/latest_order Fetch latest order details

You can add more like /stock_status, /customer_info, etc.

β„–β„– Next you can set up Docker or deployment (Heroku, Fly.io, etc.)

About

πŸ›’ Woocommerce telegram bot (Python) .A Telegram bot for WooCommerce allows you to connect your online store to Telegram, enabling features like order notifications, customer interaction, and even product sales directly through the messaging app.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages