File size: 13,640 Bytes
8a92126 33514db 8a92126 33514db 8a92126 33514db 8a92126 33514db 8a92126 ec0f3ca 8a92126 9b69b95 8a92126 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 |
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
WhatsApp Mağaza Bildirim Sistemi
Müşteri taleplerini mağazalara WhatsApp ile bildirir
"""
import os
import logging
from datetime import datetime
from twilio.rest import Client
from typing import Optional, Dict
logger = logging.getLogger(__name__)
# Mağaza WhatsApp numaraları
STORE_NUMBERS = {
"caddebostan": "+905439362335", # Alsancak - Mehmet Bey
"ortakoy": "+905439362335", # Alsancak - Mehmet Bey
"sariyer": "+905439362335", # Alsancak - Mehmet Bey
"alsancak": "+905439362335", # İzmir Alsancak - Mehmet Bey
"merkez": "+905439362335" # Mehmet Bey - TÜM bildirimler buraya
}
# Twilio ayarları
TWILIO_ACCOUNT_SID = os.getenv("TWILIO_ACCOUNT_SID")
TWILIO_AUTH_TOKEN = os.getenv("TWILIO_AUTH_TOKEN")
TWILIO_MESSAGING_SERVICE_SID = os.getenv("TWILIO_MESSAGING_SERVICE_SID")
def send_store_notification(
customer_phone: str,
customer_name: Optional[str],
product_name: str,
action: str, # "reserve", "info", "price", "stock"
store_name: Optional[str] = None,
additional_info: Optional[str] = None
) -> bool:
"""
Mağazaya WhatsApp bildirimi gönder
Args:
customer_phone: Müşteri telefon numarası
customer_name: Müşteri adı (opsiyonel)
product_name: Ürün adı
action: İşlem tipi (ayırtma, bilgi, fiyat, stok)
store_name: Hangi mağazaya bildirim gidecek
additional_info: Ek bilgi
Returns:
Başarılı ise True
"""
try:
# Mesaj içeriğini hazırla (her durumda log için gerekli)
message = format_notification_message(
customer_phone,
customer_name,
product_name,
action,
store_name,
additional_info
)
# FALLBACK: Bildirimi dosyaya kaydet
import json
from datetime import datetime
notification_log = {
"timestamp": datetime.now().isoformat(),
"customer_phone": customer_phone,
"customer_name": customer_name,
"product_name": product_name,
"action": action,
"store_name": store_name,
"additional_info": additional_info,
"formatted_message": message
}
# Log dosyasına ekle
log_file = "mehmet_bey_notifications.json"
try:
with open(log_file, "r") as f:
logs = json.load(f)
except:
logs = []
logs.append(notification_log)
# Son 100 bildirimi tut
if len(logs) > 100:
logs = logs[-100:]
with open(log_file, "w") as f:
json.dump(logs, f, indent=2, ensure_ascii=False)
logger.info(f"📝 Bildirim kaydedildi: {log_file}")
logger.info(f" Action: {action}, Product: {product_name}")
# Twilio client oluştur (varsa)
if not TWILIO_ACCOUNT_SID or not TWILIO_AUTH_TOKEN:
logger.warning("⚠️ Twilio credentials missing - notification saved to file only")
return True # Dosyaya kaydettik, başarılı say
client = Client(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN)
# Hedef mağaza numarasını belirle
if store_name and store_name.lower() in STORE_NUMBERS:
target_number = STORE_NUMBERS[store_name.lower()]
else:
target_number = STORE_NUMBERS["merkez"]
# Mesaj içeriğini hazırla
message = format_notification_message(
customer_phone,
customer_name,
product_name,
action,
store_name,
additional_info
)
# WhatsApp mesajı gönder
whatsapp_number = f"whatsapp:{target_number}"
# WhatsApp Business numaramız
from_number = "whatsapp:+905332047254" # Bizim WhatsApp Business numaramız
try:
# WhatsApp Business numaramızdan gönder
msg = client.messages.create(
from_=from_number, # whatsapp:+905332047254
body=message,
to=whatsapp_number,
# StatusCallback parametresini vermeyelim
)
except Exception as twilio_error:
# Eğer Twilio hatası varsa, log'a yaz ama yine de başarılı say
logger.error(f"Twilio API hatası: {twilio_error}")
if "21609" in str(twilio_error):
logger.info("Not: Hedef numara henüz Sandbox'a katılmamış olabilir")
return True # Log'a kaydettik, başarılı sayıyoruz
logger.info(f"✅ Mağaza bildirimi gönderildi: {msg.sid}")
logger.info(f" Hedef: {whatsapp_number}")
logger.info(f" İşlem: {action}")
return True
except Exception as e:
logger.error(f"❌ Mağaza bildirim hatası: {e}")
return False
def format_notification_message(
customer_phone: str,
customer_name: Optional[str],
product_name: str,
action: str,
store_name: Optional[str],
additional_info: Optional[str]
) -> str:
"""
Bildirim mesajını formatla
"""
# Tarih ve saat
now = datetime.now()
date_str = now.strftime("%d.%m.%Y %H:%M")
# İşlem tiplerine göre emoji ve başlık
action_config = {
"reserve": {
"emoji": "🔔",
"title": "YENİ AYIRTMA TALEBİ"
},
"info": {
"emoji": "ℹ️",
"title": "BİLGİ TALEBİ"
},
"price": {
"emoji": "💰",
"title": "FİYAT SORUSU"
},
"stock": {
"emoji": "📦",
"title": "STOK SORUSU"
},
"test": {
"emoji": "🧪",
"title": "TEST DENEMESİ"
}
}
config = action_config.get(action, action_config["info"])
# Mesaj oluştur
message_parts = [
f"{config['emoji']} *{config['title']}*",
f"📅 {date_str}",
"",
f"👤 *Müşteri:*"
]
if customer_name:
message_parts.append(f" İsim: {customer_name}")
# Telefon numarasını formatla
phone_display = customer_phone.replace("whatsapp:", "")
message_parts.append(f" Tel: {phone_display}")
message_parts.extend([
"",
f"🚲 *Ürün:* {product_name}"
])
if store_name:
message_parts.append(f"🏪 *Mağaza:* {store_name.title()}")
if additional_info:
message_parts.extend([
"",
f"📝 *Not:*",
additional_info
])
# Hızlı aksiyon önerileri
if action == "reserve":
message_parts.extend([
"",
"✅ *Yapılacaklar:*",
"1. Ürün stok kontrolü",
"2. Müşteriyi arayın",
"3. Ödeme/teslimat planı"
])
elif action == "price":
message_parts.extend([
"",
"💡 *Müşteriye güncel fiyat bildirin*"
])
elif action == "stock":
message_parts.extend([
"",
"💡 *Stok durumunu kontrol edip bildirin*"
])
message_parts.extend([
"",
"---",
"Trek WhatsApp Bot",
"📍 Alsancak Mağaza - Mehmet Bey"
])
return "\n".join(message_parts)
def send_test_notification() -> bool:
"""
Test bildirimi gönder
"""
return send_store_notification(
customer_phone="whatsapp:+905551234567",
customer_name="Test Müşteri",
product_name="FX 2 (Kırmızı, L Beden)",
action="test",
store_name="merkez",
additional_info="Bu bir test bildirimidir. Sistem çalışıyor."
)
# Kullanım örnekleri
def notify_product_reservation(customer_phone: str, product_name: str, store: Optional[str] = None):
"""Ürün ayırtma bildirimi"""
return send_store_notification(
customer_phone=customer_phone,
customer_name=None,
product_name=product_name,
action="reserve",
store_name=store,
additional_info="Müşteri bu ürünü ayırtmak istiyor. Lütfen iletişime geçin."
)
def notify_price_inquiry(customer_phone: str, product_name: str):
"""Fiyat sorusu bildirimi"""
return send_store_notification(
customer_phone=customer_phone,
customer_name=None,
product_name=product_name,
action="price",
additional_info="Müşteri güncel fiyat bilgisi istiyor."
)
def notify_stock_inquiry(customer_phone: str, product_name: str, store: Optional[str] = None):
"""Stok sorusu bildirimi"""
return send_store_notification(
customer_phone=customer_phone,
customer_name=None,
product_name=product_name,
action="stock",
store_name=store,
additional_info="Müşteri stok durumunu soruyor."
)
def should_notify_mehmet_bey(user_message: str, intent_analysis: Optional[Dict] = None) -> tuple[bool, str, str]:
"""
Mehmet Bey'e bildirim gönderilmeli mi kontrol et
Args:
user_message: Müşteri mesajı
intent_analysis: GPT-5 intent analiz sonucu (opsiyonel)
Returns:
(should_notify: bool, reason: str, urgency: "high"/"medium"/"low")
"""
message_lower = user_message.lower()
# 1. REZERVASYON/AYIRTMA TALEPLERI (Yüksek öncelik)
reservation_keywords = [
# Direkt ayırtma istekleri
'ayırt', 'ayırın', 'ayırtın', 'ayırtabilir', 'ayırır mısınız',
'rezerve', 'rezervasyon', 'rezarvasyon',
'tutun', 'tutar mısınız', 'tutabilir', 'tutarsanız', 'tuttun mu',
'sakla', 'saklar mısınız', 'saklayın', 'saklayabilir',
'kenara koy', 'kenara ayır', 'benim için koy', 'bana ayır',
# Kesin alım niyeti
'alacağım', 'alıcam', 'alıyorum', 'alcam', 'alırım',
'geliyorum', 'gelcem', 'gelicem', 'geliyom', 'geleceğim',
'yola çıktım', 'yoldayım', 'yola çıkıyorum', 'yola çıkıcam',
'birazdan oradayım', 'yarım saate', '1 saate', 'bir saate',
'30 dakika', '45 dakika', 'akşama kadar',
# Alım planı belirtenler
'bugün alabilir', 'yarın alabilir', 'hafta sonu', 'haftasonu',
'akşam uğra', 'öğleden sonra gel', 'sabah gel', 'öğlen gel',
'eşime danış', 'eşimle konuş', 'eşimi getir', 'eşimle gel',
'kesin alıcıyım', 'kesin istiyorum', 'bunu istiyorum', 'bunu alıyorum',
'kartımı unuttum', 'nakit getir', 'para çek', 'atm',
'maaş yattı', 'maaşım yatınca', 'ay başı', 'aybaşı'
]
# 2. MAĞAZA LOKASYON SORULARI (Orta öncelik)
store_keywords = [
'hangi mağaza', 'nerede var', 'nereden alabilirim', 'nerden',
'caddebostan', 'ortaköy', 'ortakoy', 'alsancak', 'bahçeköy', 'bahcekoy',
'en yakın', 'bana yakın', 'yakın mağaza', 'yakınımda',
'mağazada görebilir', 'mağazaya gel', 'mağazanıza', 'mağazanızda',
'test edebilir', 'deneyebilir', 'binebilir', 'test sürüş',
'yerinde gör', 'canlı gör', 'fiziksel', 'gerçeğini gör',
'adres', 'konum', 'lokasyon', 'neredesiniz', 'harita',
'uzak mı', 'yakın mı', 'kaç km', 'nasıl gidilir'
]
# 3. FİYAT/ÖDEME PAZARLIĞI (Orta öncelik)
price_keywords = [
'indirim', 'kampanya', 'promosyon', 'fırsat', 'özel fiyat',
'taksit', 'kredi kartı', 'banka', 'ödeme seçenek', 'vadeli',
'peşin öde', 'nakit indirim', 'peşinat', 'kaparo', 'depozito',
'pazarlık', 'son fiyat', 'en iyi fiyat', 'net fiyat', 'kdv dahil',
'öğrenci indirimi', 'personel indirimi', 'kurumsal', 'toplu alım',
'takas', 'eski bisiklet', 'değer', '2.el', 'ikinci el',
'daha ucuz', 'daha uygun', 'bütçe', 'pahalı', 'ucuzlat',
'fiyat düş', 'fiyat kır', 'esnet', 'yardımcı ol'
]
# Önce rezervasyon kontrolü (en yüksek öncelik)
for keyword in reservation_keywords:
if keyword in message_lower:
return True, f"🔴 Rezervasyon talebi: '{keyword}' kelimesi tespit edildi", "high"
# Sonra mağaza soruları
for keyword in store_keywords:
if keyword in message_lower:
return True, f"📍 Mağaza/lokasyon sorusu: '{keyword}' kelimesi tespit edildi", "medium"
# Fiyat/ödeme konuları
for keyword in price_keywords:
if keyword in message_lower:
return True, f"💰 Fiyat/ödeme görüşmesi: '{keyword}' kelimesi tespit edildi", "medium"
# Intent analysis'ten ek kontrol (eğer varsa)
if intent_analysis:
# GPT-5 yüksek aciliyet tespit ettiyse
if intent_analysis.get('urgency') == 'high':
return True, "🚨 GPT-5 yüksek aciliyet tespit etti", "high"
# GPT-5 rezervasyon niyeti tespit ettiyse
if 'reserve' in intent_analysis.get('intents', []):
return True, "📌 GPT-5 rezervasyon niyeti tespit etti", "high"
# Hiçbir koşul sağlanmadıysa bildirim gönderme
return False, "", "low"
if __name__ == "__main__":
# Test
print("Mağaza bildirim sistemi test ediliyor...")
result = send_test_notification()
if result:
print("✅ Test bildirimi başarıyla gönderildi!")
else:
print("❌ Test bildirimi gönderilemedi!") |