|
|
|
""" |
|
WhatsApp Trek Chatbot Enhanced Features |
|
1. Ürün Karşılaştırma |
|
2. Bütçe Önerileri |
|
3. Fiyat Hesaplamaları |
|
WhatsApp için basitleştirilmiş versiyon - Görsel AI ve profil sistemi yok |
|
""" |
|
|
|
import re |
|
from datetime import datetime |
|
|
|
class WhatsAppProductComparison: |
|
"""WhatsApp için ürün karşılaştırma sistemi""" |
|
|
|
def __init__(self, products_data): |
|
self.products = products_data |
|
|
|
def round_price(self, price_str): |
|
"""Fiyatı yuvarlama formülüne göre yuvarla""" |
|
try: |
|
price_float = float(price_str) |
|
|
|
if price_float > 200000: |
|
return str(round(price_float / 5000) * 5000) |
|
|
|
elif price_float > 30000: |
|
return str(round(price_float / 1000) * 1000) |
|
|
|
elif price_float > 10000: |
|
return str(round(price_float / 100) * 100) |
|
|
|
else: |
|
return str(round(price_float / 10) * 10) |
|
except (ValueError, TypeError): |
|
return price_str |
|
|
|
def find_products_by_name(self, product_names): |
|
"""İsimlere göre ürünleri bul""" |
|
found_products = [] |
|
used_products = set() |
|
|
|
for name in product_names: |
|
for product in self.products: |
|
product_id = product[2] |
|
if product_id not in used_products and name.lower() in product[2].lower(): |
|
found_products.append(product) |
|
used_products.add(product_id) |
|
break |
|
return found_products |
|
|
|
def create_comparison_whatsapp(self, product_names): |
|
"""WhatsApp için karşılaştırma metni oluştur""" |
|
products = self.find_products_by_name(product_names) |
|
|
|
if len(products) < 2: |
|
return "❌ Karşılaştırma için en az 2 ürün gerekli." |
|
|
|
|
|
comparison_text = "📊 **ÜRÜN KARŞILAŞTIRMASI**\n\n" |
|
|
|
for i, product in enumerate(products, 1): |
|
name, item_info, full_name = product |
|
|
|
|
|
stock_status = item_info[0] if len(item_info) > 0 else "Bilgi yok" |
|
price_raw = item_info[1] if len(item_info) > 1 and item_info[1] else "Fiyat yok" |
|
product_link = item_info[2] if len(item_info) > 2 else "" |
|
|
|
|
|
if price_raw != "Fiyat yok": |
|
price = self.round_price(price_raw) |
|
price_display = f"{price} TL" |
|
else: |
|
price_display = price_raw |
|
|
|
comparison_text += f"**{i}. {full_name}**\n" |
|
comparison_text += f"📦 Stok: {stock_status}\n" |
|
comparison_text += f"💰 Fiyat: {price_display}\n" |
|
|
|
|
|
if len(item_info) > 6 and item_info[6]: |
|
comparison_text += f"🖼️ Resim: {item_info[6]}\n" |
|
|
|
if product_link: |
|
comparison_text += f"🔗 Link: {product_link}\n" |
|
|
|
comparison_text += "\n" + "─" * 25 + "\n\n" |
|
|
|
return comparison_text |
|
|
|
def get_similar_products(self, product_name, category_filter=None): |
|
"""Benzer ürünleri bul""" |
|
similar_products = [] |
|
base_name = product_name.lower().split()[0] |
|
|
|
for product in self.products: |
|
product_full_name = product[2].lower() |
|
if base_name in product_full_name and product_name.lower() != product_full_name: |
|
if category_filter: |
|
if category_filter.lower() in product_full_name: |
|
similar_products.append(product) |
|
else: |
|
similar_products.append(product) |
|
|
|
return similar_products[:3] |
|
|
|
class WhatsAppBudgetRecommendations: |
|
"""WhatsApp için bütçe önerileri""" |
|
|
|
def __init__(self, products_data): |
|
self.products = products_data |
|
|
|
def get_budget_recommendations(self, budget_min, budget_max): |
|
"""Bütçeye uygun öneriler""" |
|
suitable_products = [] |
|
|
|
for product in self.products: |
|
if product[1][0] == "stokta" and product[1][1]: |
|
try: |
|
price = float(product[1][1]) |
|
if budget_min <= price <= budget_max: |
|
suitable_products.append(product) |
|
except (ValueError, TypeError): |
|
continue |
|
|
|
return suitable_products[:5] |
|
|
|
def format_budget_recommendations_whatsapp(self, products, budget_min, budget_max): |
|
"""WhatsApp için bütçe önerilerini formatla""" |
|
if not products: |
|
if budget_min == budget_max: |
|
return f"❌ {budget_min:,.0f} TL bütçenize uygun stokta ürün bulunamadı." |
|
else: |
|
return f"❌ {budget_min:,.0f}-{budget_max:,.0f} TL bütçenize uygun stokta ürün bulunamadı." |
|
|
|
if budget_min == budget_max: |
|
text = f"💰 **{budget_min:,.0f} TL BÜTÇENİZE EN YAKIN ÖNERİLER**\n\n" |
|
else: |
|
text = f"💰 **{budget_min:,.0f}-{budget_max:,.0f} TL BÜTÇENİZE UYGUN ÖNERİLER**\n\n" |
|
|
|
for i, product in enumerate(products, 1): |
|
name, item_info, full_name = product |
|
price = item_info[1] if len(item_info) > 1 else "Fiyat yok" |
|
product_link = item_info[2] if len(item_info) > 2 else "" |
|
|
|
text += f"**{i}. {full_name}**\n" |
|
|
|
try: |
|
price_float = float(price) |
|
price_formatted = f"{price_float:,.0f}" |
|
except: |
|
price_formatted = price |
|
text += f"💰 Fiyat: {price_formatted} TL\n" |
|
|
|
|
|
if len(item_info) > 6 and item_info[6]: |
|
text += f"🖼️ Resim: {item_info[6]}\n" |
|
|
|
if product_link: |
|
text += f"🔗 Link: {product_link}\n" |
|
|
|
text += "\n" |
|
|
|
return text |
|
|
|
class WhatsAppCategoryRecommendations: |
|
"""WhatsApp için kategori bazlı öneriler""" |
|
|
|
def __init__(self, products_data): |
|
self.products = products_data |
|
|
|
def get_category_products(self, category_keywords): |
|
"""Kategoriye göre ürünleri bul""" |
|
category_products = [] |
|
|
|
for product in self.products: |
|
if product[1][0] == "stokta": |
|
product_name = product[2].lower() |
|
for keyword in category_keywords: |
|
if keyword.lower() in product_name: |
|
category_products.append(product) |
|
break |
|
|
|
return category_products[:100] |
|
|
|
def format_category_recommendations(self, category_name, products): |
|
"""Kategori önerilerini formatla""" |
|
if not products: |
|
return f"❌ {category_name} kategorisinde stokta ürün bulunamadı." |
|
|
|
text = f"🚲 **{category_name.upper()} KATEGORİSİ ÖNERİLERİ**\n\n" |
|
|
|
for i, product in enumerate(products, 1): |
|
name, item_info, full_name = product |
|
price = item_info[1] if len(item_info) > 1 else "Fiyat yok" |
|
|
|
|
|
try: |
|
price_float = float(price) |
|
price_formatted = f"{price_float:,.0f}" |
|
except: |
|
price_formatted = price |
|
|
|
text += f"**{i}. {full_name}**\n" |
|
text += f"💰 Fiyat: {price_formatted} TL\n\n" |
|
|
|
return text |
|
|
|
|
|
whatsapp_product_comparison = None |
|
whatsapp_budget_recommendations = None |
|
whatsapp_category_recommendations = None |
|
|
|
def initialize_whatsapp_features(products_data): |
|
"""WhatsApp enhanced özellikleri başlat""" |
|
global whatsapp_product_comparison, whatsapp_budget_recommendations, whatsapp_category_recommendations |
|
|
|
whatsapp_product_comparison = WhatsAppProductComparison(products_data) |
|
whatsapp_budget_recommendations = WhatsAppBudgetRecommendations(products_data) |
|
whatsapp_category_recommendations = WhatsAppCategoryRecommendations(products_data) |
|
|
|
def handle_whatsapp_comparison_request(user_message): |
|
"""WhatsApp karşılaştırma talebini işle""" |
|
try: |
|
if "karşılaştır" in user_message.lower() or "compare" in user_message.lower(): |
|
|
|
words = user_message.lower().split() |
|
potential_products = [] |
|
|
|
|
|
known_models = ["émonda", "madone", "domane", "marlin", "fuel", "powerfly", "fx", "checkpoint", "procaliber", "supercaliber"] |
|
|
|
|
|
message_lower = user_message.lower() |
|
if "marlin" in message_lower: |
|
import re |
|
marlin_numbers = re.findall(r'marlin\s*(\d+)', message_lower) |
|
if len(marlin_numbers) >= 2: |
|
|
|
for num in marlin_numbers: |
|
potential_products.append(f"marlin {num}") |
|
elif len(marlin_numbers) == 1: |
|
potential_products.append(f"marlin {marlin_numbers[0]}") |
|
else: |
|
potential_products.append("marlin") |
|
|
|
|
|
for word in words: |
|
for model in known_models: |
|
if model != "marlin" and model in word: |
|
potential_products.append(model) |
|
|
|
|
|
potential_products = list(dict.fromkeys(potential_products)) |
|
|
|
if len(potential_products) >= 2 and whatsapp_product_comparison: |
|
comparison_result = whatsapp_product_comparison.create_comparison_whatsapp(potential_products) |
|
return comparison_result |
|
|
|
return None |
|
except Exception as e: |
|
print(f"WhatsApp Comparison error: {e}") |
|
return None |
|
|
|
def handle_whatsapp_price_query(user_message): |
|
"""WhatsApp fiyat sorgusu - 'ne kadar', 'fiyat', 'kaç para' gibi""" |
|
try: |
|
user_lower = user_message.lower() |
|
|
|
|
|
price_query_keywords = ["ne kadar", "fiyat", "kaç para", "fiyatı", "kaça", "para"] |
|
if not any(keyword in user_lower for keyword in price_query_keywords): |
|
return None |
|
|
|
|
|
product_keywords = ["madone", "émonda", "domane", "marlin", "fuel", "powerfly", "fx", "checkpoint", "procaliber", "supercaliber", "ds", "verve", "rail"] |
|
found_product = None |
|
|
|
for keyword in product_keywords: |
|
if keyword in user_lower: |
|
found_product = keyword |
|
break |
|
|
|
if found_product and whatsapp_budget_recommendations: |
|
|
|
matching_products = [] |
|
for product in whatsapp_budget_recommendations.products: |
|
if product[1][0] == "stokta" and found_product in product[2].lower(): |
|
matching_products.append(product) |
|
|
|
if matching_products: |
|
|
|
main_product = matching_products[0] |
|
name, item_info, full_name = main_product |
|
price = item_info[1] if len(item_info) > 1 else "Fiyat yok" |
|
|
|
try: |
|
price_float = float(price) |
|
price_formatted = f"{price_float:,.0f}" |
|
except: |
|
price_formatted = price |
|
|
|
|
|
series_products = matching_products[:3] |
|
|
|
response = f"🚲 **{found_product.upper()} SERİSİ FİYATLARI**\\n\\n" |
|
|
|
for i, product in enumerate(series_products, 1): |
|
name, item_info, full_name = product |
|
price = item_info[1] if len(item_info) > 1 else "Fiyat yok" |
|
|
|
try: |
|
price_float = float(price) |
|
price_formatted = f"{price_float:,.0f}" |
|
except: |
|
price_formatted = price |
|
|
|
response += f"**{i}. {full_name}**\\n" |
|
response += f"💰 Fiyat: {price_formatted} TL\\n\\n" |
|
|
|
return response |
|
|
|
return None |
|
except Exception as e: |
|
print(f"WhatsApp Price Query error: {e}") |
|
return None |
|
|
|
def handle_whatsapp_budget_request(user_message): |
|
"""WhatsApp bütçe talebini işle""" |
|
try: |
|
message_lower = user_message.lower() |
|
|
|
|
|
category_keywords = ["yol", "dağ", "şehir", "elektrikli", "gravel", "marlin", "madone", "émonda", "domane", "fuel", "powerfly", "fx", "ds", "checkpoint"] |
|
has_category = any(keyword in message_lower for keyword in category_keywords) |
|
|
|
if has_category: |
|
return None |
|
|
|
|
|
import re |
|
numbers = re.findall(r'\d+', user_message) |
|
|
|
if numbers and whatsapp_budget_recommendations: |
|
budget_value = int(numbers[0]) * 1000 |
|
|
|
|
|
all_products = [] |
|
for product in whatsapp_budget_recommendations.products: |
|
if product[1][0] == "stokta" and product[1][1]: |
|
try: |
|
price = float(product[1][1]) |
|
price_diff = abs(price - budget_value) |
|
all_products.append((product, price_diff)) |
|
except: |
|
continue |
|
|
|
|
|
all_products.sort(key=lambda x: x[1]) |
|
closest_products = [product[0] for product in all_products[:5]] |
|
|
|
if closest_products: |
|
return f"💰 {budget_value:,} TL bütçenize en yakın öneriler:\n\n" + whatsapp_budget_recommendations.format_budget_recommendations_whatsapp( |
|
closest_products, budget_value, budget_value |
|
) |
|
|
|
return None |
|
except Exception as e: |
|
print(f"WhatsApp Budget error: {e}") |
|
return None |
|
|
|
def handle_whatsapp_category_request(user_message, phone_number=None): |
|
"""WhatsApp kategori önerisi talebini işle""" |
|
try: |
|
user_lower = user_message.lower() |
|
|
|
|
|
budget_value = None |
|
import re |
|
numbers = re.findall(r'\d+', user_message) |
|
if numbers: |
|
budget_value = int(numbers[0]) * 1000 |
|
|
|
|
|
categories = { |
|
"dağ bisikleti": ["dağ", "dag", "offroad", "mountain", "marlin", "fuel", "procaliber", "supercaliber"], |
|
"yol bisikleti": ["yol", "road", "hız", "yarış", "émonda", "madone", "domane", "speed"], |
|
"şehir bisikleti": ["şehir", "sehir", "city", "urban", "fx", "ds", "dual sport", "verve"], |
|
"elektrikli bisiklet": ["elektrikli", "electric", "e-bike", "ebike", "powerfly", "rail", "fuel exe", "domane+", "fx+", "ds+", "verve+", "townie"], |
|
"gravel bisiklet": ["gravel", "çakıl", "checkpoint"] |
|
} |
|
|
|
for category_name, keywords in categories.items(): |
|
if any(keyword in user_lower for keyword in keywords): |
|
if whatsapp_category_recommendations: |
|
|
|
products = whatsapp_category_recommendations.get_category_products(keywords) |
|
|
|
|
|
if budget_value is not None: |
|
products_with_price_diff = [] |
|
for product in products: |
|
if product[1][0] == "stokta" and product[1][1]: |
|
try: |
|
price = float(product[1][1]) |
|
price_diff = abs(price - budget_value) |
|
products_with_price_diff.append((product, price_diff)) |
|
except: |
|
continue |
|
|
|
|
|
products_with_price_diff.sort(key=lambda x: x[1]) |
|
products = [product[0] for product in products_with_price_diff[:5]] |
|
else: |
|
products = products[:5] |
|
|
|
if products: |
|
response = whatsapp_category_recommendations.format_category_recommendations( |
|
category_name, products |
|
) |
|
|
|
|
|
if budget_value is not None: |
|
response = f"💰 {budget_value:,} TL bütçenize en yakın öneriler:\n\n" + response |
|
|
|
return response |
|
|
|
return None |
|
except Exception as e: |
|
print(f"WhatsApp Category error: {e}") |
|
return None |