haikal/whatsapp_bot.py
Marwan Alwali 152518ebdc update
2024-12-19 20:04:22 +03:00

67 lines
2.7 KiB
Python

import re
import arabic_reshaper
from bidi.algorithm import get_display
from pywa import WhatsApp
# Initialize the WhatsApp bot
bot = WhatsApp(
phone_id="00966535521547",
token="c446eba6-fcfe-437c-923a-a554c25578dd"
)
# Pre-defined responses in English and Arabic
responses = {
"greet": {
"en": "Hello! How can I assist you with Haikal Car Inventory?",
"ar": get_display(arabic_reshaper.reshape("مرحباً! كيف يمكنني مساعدتك في نظام هيكل لإدارة السيارات؟"))
},
"sell_process": {
"en": "To sell a car: Create a Sell Order, add a customer, confirm payment, generate an invoice, and deliver the car.",
"ar": get_display(arabic_reshaper.reshape("لبيع السيارة: قم بإنشاء طلب بيع، إضافة العميل، تأكيد الدفع، إنشاء الفاتورة، وتسليم السيارة."))
},
"inventory_check": {
"en": "You can check available cars in the inventory. Do you want details on any specific car?",
"ar": get_display(arabic_reshaper.reshape("يمكنك التحقق من السيارات المتوفرة في المخزون. هل تريد تفاصيل عن سيارة معينة؟"))
},
"bye": {
"en": "Goodbye! Let me know if you need further help.",
"ar": get_display(arabic_reshaper.reshape("وداعاً! أخبرني إذا كنت بحاجة إلى مزيد من المساعدة."))
},
"unknown": {
"en": "I'm sorry, I didn't understand that. Can you rephrase?",
"ar": get_display(arabic_reshaper.reshape("عذراً، لم أفهم ذلك. هل يمكنك إعادة صياغة السؤال؟"))
}
}
# Function to classify user input
def classify_message(message):
message = message.lower()
if re.search(r"hello|hi|مرحب|السلام", message):
return "greet"
elif re.search(r"sell|بيع", message):
return "sell_process"
elif re.search(r"inventory|cars|المخزون|السيارات", message):
return "inventory_check"
elif re.search(r"bye|وداع|إلى اللقاء", message):
return "bye"
else:
return "unknown"
# Send response based on the intent
@bot.on_message()
def handle_message(message):
user_input = message.body.strip()
intent = classify_message(user_input)
# Detect language and respond
if re.search("[\u0600-\u06FF]", user_input): # Arabic characters
response = responses[intent]["ar"]
else: # Default to English
response = responses[intent]["en"]
message.reply(response)
# Start the bot
if __name__ == "__main__":
print("Starting the Haikal Car Inventory WhatsApp Bot...")
bot.run()