
-
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
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
-
Create a Telegram bot via @BotFather
-
Save the bot token
-
WooCommerce REST API must be enabled
-
Get your Consumer Key and Consumer Secret
Install required libraries:
pip install python-telegram-bot woocommerce flask
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)
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()
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)
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)
-
Respond to Telegram commands (
/latest_orders
) -
Notify on stock status changes
-
Add custom filters for big orders, VIP clients, etc.
-
Run Flask app on cloud (Heroku, Fly.io, etc.)
-
Use ngrok for testing locally with webhooks:
ngrok http 5000
requirements.txt
bot/config.py
bot/woo.py
bot/telegram_bot.py
webhook/server.py
run.py
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
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.)