help@rskworld.in +91 93305 39277
RSK World
  • Home
  • Development
    • Web Development
    • Mobile Apps
    • Software
    • Games
    • Project
  • Technologies
    • Data Science
    • AI Development
    • Cloud Development
    • Blockchain
    • Cyber Security
    • Dev Tools
    • Testing Tools
  • Blog
  • About
  • Contact

Theme Settings

Color Scheme
Display Options
Font Size
100%
Back to Project
RSK World
slack-bot-assistant
RSK World
slack-bot-assistant
Slack Bot Assistant - Python + Slack API + SQLite + PHP Dashboard + Bot Commands + Automation
slack-bot-assistant
  • static
  • templates
  • .env.example73 B
  • .gitignore61 B
  • LICENSE1.2 KB
  • README.md1.9 KB
  • RELEASE_NOTES.md1.3 KB
  • app.py1.7 KB
  • bot_logic.py4.3 KB
  • database.py1.8 KB
  • models.py2.2 KB
  • requirements.txt58 B
bot_logic.py
bot_logic.py
Raw Download
Find: Go to:
# Project: E-commerce Chatbot
# Developed by: Molla Samser
# Team: Rima Khatun (Designer & Tester)
# Website: https://rskworld.in
# Email: support@rskworld.in
# Year: 2026
# "Empowering E-commerce with AI"

import os
from openai import OpenAI
from models import Product, Order, User
from flask import current_app

# Note: In a real app, API key should be in .env
api_key = os.getenv("OPENAI_API_KEY")
if not api_key:
    # Use a dummy key if not set, logic will handle error in try block
    api_key = "sk-placeholder"
client = OpenAI(api_key=api_key)

def get_bot_response(user_message, history):
    # Sentiment and Language analysis
    sentiment = analyze_sentiment(user_message)
    language_note = "Respond in Hindi if the user asks in Hindi, otherwise respond in English."
    
    # System prompt to define bot personality
    system_prompt = f"You are a helpful e-commerce assistant for RSK World's store. Your tone should be {sentiment}. " \
                    f"{language_note} You help with product searches, recommendations, order tracking, and checking loyalty points."

    # Process loyalty points intent
    if "points" in user_message.lower() or "loyalty" in user_message.lower():
        return handle_loyalty_points(user_message)

    # Process specific intents locally before AI if needed
    if "order" in user_message.lower() and any(char.isdigit() for char in user_message):
        return handle_order_tracking(user_message)

    try:
        response = client.chat.completions.create(
            model="gpt-4o",
            messages=[
                {"role": "system", "content": system_prompt},
                *history,
                {"role": "user", "content": user_message}
            ]
        )
        return response.choices[0].message.content
    except Exception as e:
        return f"I'm sorry, I'm having trouble connecting right now. But I can tell you that RSK World is always here to help! (Error: {str(e)})"

def analyze_sentiment(message):
    lowered = message.lower()
    negative_words = ['angry', 'bad', 'horrible', 'slow', 'hate', 'broke']
    if any(word in lowered for word in negative_words):
        return "extremely empathetic and apologetic"
    return "friendly and professional"

def handle_order_tracking(message):
    # Simple order number extraction logic
    import re
    order_match = re.search(r'ORD-\d+', message.upper())
    if order_match:
        order_num = order_match.group(0)
        order = Order.query.filter_by(order_number=order_num).first()
        if order:
            status = order.status
            # Add Hindi translation if needed
            if any("\u0900" <= c <= "\u097f" for c in message):
                return f"आपका ऑर्डर {order_num} अभी **{status}** स्थिति में है। यह {order.created_at.strftime('%d %b, %Y')} को दिया गया था।"
            return f"Order {order_num} is currently: **{status}**. It was placed on {order.created_at.strftime('%B %d, %Y')}."
        else:
            return f"I couldn't find an order with the number {order_num}. Please double-check your receipt!"
    return "I think you're asking about an order. Could you please provide the order number (e.g., ORD-123)?"

def handle_loyalty_points(message):
    # In a real app, we'd get the logged-in user email
    default_email = "samser@rskworld.in"
    user = User.query.filter_by(email=default_email).first()
    if user:
        if any("\u0900" <= c <= "\u097f" for c in message):
            return f"नमस्ते {user.name}, आपके पास **{user.loyalty_points}** लॉयल्टी पॉइंट्स हैं! आप इन्हें अपनी अगली खरीदारी पर इस्तेमाल कर सकते हैं।"
        return f"Hello {user.name}, you currently have **{user.loyalty_points}** loyalty points! You can redeem them on your next purchase."
    return "I couldn't find your loyalty account. Please make sure you are logged in."

def search_products(query):
    # Database search logic
    products = Product.query.filter(Product.name.contains(query) | Product.description.contains(query)).all()
    return [p.to_dict() for p in products]
89 lines•4.3 KB
python

About RSK World

Founded by Molla Samser, with Designer & Tester Rima Khatun, RSK World is your one-stop destination for free programming resources, source code, and development tools.

Founder: Molla Samser
Designer & Tester: Rima Khatun

Development

  • Game Development
  • Web Development
  • Mobile Development
  • AI Development
  • Development Tools

Legal

  • Terms & Conditions
  • Privacy Policy
  • Disclaimer

Contact Info

Nutanhat, Mongolkote
Purba Burdwan, West Bengal
India, 713147

+91 93305 39277

hello@rskworld.in
support@rskworld.in

© 2026 RSK World. All rights reserved.

Content used for educational purposes only. View Disclaimer