| name | whatsapp-context-manager |
| description | Whatsapp Context Manager for Agents |
WhatsApp Intelligent Context Manager - Skill Guide
This skill provides an AI-powered context management system for WhatsApp customer service agents, enabling instant access to customer history, sentiment analysis, and smart response suggestions.
Quick Installation
unzip whatsapp-context-manager.zip
cd whatsapp-context-manager
python install_check_whatsapp.py
python test_whatsapp.py
python examples_whatsapp.py
What Problem Does This Solve?
Without This System:
- ❌ Agents have no context when customer messages arrive
- ❌ No idea if customer is VIP or first-timer
- ❌ Can't see order status without switching systems
- ❌ Don't know if message is urgent or can wait
- ❌ Guessing what to say instead of smart suggestions
With This System:
- ✅ Complete customer context in 2 seconds
- ✅ Automatic sentiment analysis (angry/happy/neutral)
- ✅ Smart priority (critical/high/normal/low)
- ✅ Order status right there
- ✅ AI-powered response suggestions
- ✅ VIP customer detection
Basic Usage
1. Initialize the System
from whatsapp_context_manager import ContextManager
manager = ContextManager("production.db")
2. Process Incoming WhatsApp Message
context = manager.process_incoming_message(
phone="+1234567890",
message_content="Where is my order?!",
agent_id="agent_001"
)
3. Display Context to Agent
print(f"Priority: {context.priority.value}")
print(f"Sentiment: {context.sentiment.value}")
print(f"Category: {context.category}")
print(f"VIP Customer: {context.customer.is_vip}")
for insight in context.key_insights:
print(f"💡 {insight}")
for warning in context.warnings:
print(f"⚠️ {warning}")
for response in context.suggested_responses:
print(f"💬 {response}")
4. Send Reply
manager.send_message(
phone="+1234567890",
message_content="Your order #12345 is on the way!",
agent_id="agent_001"
)
What Agent Sees - Dashboard Example
┌──────────────────────────────────────────────────────┐
│ AGENT DASHBOARD │
├──────────────────────────────────────────────────────┤
│ Customer: +1234567890 │
│ Name: John Doe │
│ VIP: YES │
├──────────────────────────────────────────────────────┤
│ Priority: CRITICAL │
│ Sentiment: NEGATIVE │
│ Category: ORDER_STATUS │
├──────────────────────────────────────────────────────┤
│ KEY INSIGHTS: │
│ • 🌟 VIP Customer - Prioritize response │
│ • 📦 Active Order: #ORD-12345 - shipped │
│ • 🚚 Tracking: TRK-ABC123 │
│ • ⚡ Customer expects fast replies (~2min) │
├──────────────────────────────────────────────────────┤
│ WARNINGS: │
│ • 🚨 CRITICAL: Requires immediate attention! │
│ • 😡 Customer is very upset - handle with care │
├──────────────────────────────────────────────────────┤
│ SUGGESTED RESPONSES: │
│ 1. Let me check your order status right away. │
│ 2. Your order #ORD-12345 is shipped. │
└──────────────────────────────────────────────────────┘
Core Features
1. Automatic Sentiment Analysis
Detects customer mood from message:
context = manager.process_incoming_message(phone, "This is TERRIBLE!", agent_id)
print(context.sentiment.value)
context = manager.process_incoming_message(phone, "Thanks!", agent_id)
print(context.sentiment.value)
Sentiment Levels:
- 😡
very_negative - Angry, furious, scam
- 😟
negative - Disappointed, problem
- 😐
neutral - Questions, info requests
- 😊
positive - Thanks, happy
- 🤩
very_positive - Excellent, love it
2. Message Categorization
Automatically categorizes messages:
context = manager.process_incoming_message(phone, "Where is my package?", agent_id)
print(context.category)
context = manager.process_incoming_message(phone, "Refund please!", agent_id)
print(context.category)
Categories:
- 📦
ORDER_STATUS - Delivery, tracking, shipment
- 💳
PAYMENT - Refund, billing, transaction
- 🔴
COMPLAINT - Problem, issue, broken
- 🛍️
PRODUCT_INQUIRY - Price, stock, features
- 🆘
SUPPORT - Help, how-to, questions
- 💰
SALES - Buy, purchase, interested
- ⭐
FEEDBACK - Review, opinion
- ❓
OTHER - Uncategorized
3. Priority Calculation
Smart priority based on multiple factors:
context = manager.process_incoming_message(
phone="+1234567890",
message_content="My payment failed!!!",
agent_id="agent_001"
)
print(context.priority.value)
Priority Levels:
- 🔴
CRITICAL - Angry customer, payment issue, VIP unhappy
- 🟠
HIGH - Complaints, negative sentiment
- 🟡
NORMAL - General questions
- 🟢
LOW - Info requests, positive feedback
4. Response Suggestions
AI suggests appropriate responses:
context = manager.process_incoming_message(
phone="+1234567890",
message_content="When will my order arrive?",
agent_id="agent_001"
)
for response in context.suggested_responses:
print(response)
Advanced Features
Order Integration
Add and track customer orders:
from whatsapp_context_manager import Order
from datetime import datetime, timedelta
order = Order(
order_id="ORD-12345",
customer_id=context.customer.customer_id,
status="shipped",
amount=99.99,
items=[
{"name": "Wireless Headphones", "quantity": 1, "price": 99.99}
],
created_at=datetime.now().isoformat(),
updated_at=datetime.now().isoformat(),
tracking_number="TRK-ABC123",
estimated_delivery=(datetime.now() + timedelta(days=2)).strftime("%Y-%m-%d")
)
manager.add_order(order)
context = manager.process_incoming_message(phone, "Order status?", agent_id)
print(context.active_orders[0].tracking_number)
VIP Customer Management
Mark and manage VIP customers:
manager.update_customer_info(
phone="+1234567890",
name="John Doe",
email="john@example.com",
is_vip=True,
tags=["premium", "loyal", "high-value"],
notes="Always responds best to quick, direct answers"
)
context = manager.process_incoming_message(phone, "Hello", agent_id)
print(context.customer.is_vip)
print(context.customer.tags)
Conversation History
Access complete conversation history:
context = manager.process_incoming_message(phone, "Need help", agent_id)
for msg in context.recent_messages:
direction = "Customer" if msg.direction == "inbound" else "Agent"
print(f"{direction}: {msg.content}")
Customer Profile
Access complete customer profile:
context = manager.process_incoming_message(phone, "Hello", agent_id)
customer = context.customer
print(f"Phone: {customer.phone}")
print(f"Name: {customer.name}")
print(f"Total Messages: {customer.total_messages}")
print(f"VIP: {customer.is_vip}")
print(f"Tags: {customer.tags}")
print(f"Notes: {customer.notes}")
print(f"Last Contact: {customer.last_contact}")
print(f"Sentiment History: {customer.sentiment_history}")
Common Use Cases
Use Case 1: Order Status Inquiry
context = manager.process_incoming_message(
phone="+1234567890",
message_content="Where is my order?",
agent_id="agent_001"
)
if context.active_orders:
order = context.active_orders[0]
print(f"Order ID: {order.order_id}")
print(f"Status: {order.status}")
print(f"Tracking: {order.tracking_number}")
print(f"Est. Delivery: {order.estimated_delivery}")
print(context.suggested_responses[0])
Use Case 2: Angry Customer
context = manager.process_incoming_message(
phone="+1234567890",
message_content="This is TERRIBLE! I want a refund NOW!!!",
agent_id="agent_001"
)
print(context.priority.value)
print(context.sentiment.value)
for warning in context.warnings:
print(warning)
print(context.suggested_responses[0])
Use Case 3: Multiple Customers Priority Queue
customers = [
("+1111111111", "Can I get some info?"),
("+2222222222", "My payment failed!!!"),
("+3333333333", "I have a complaint"),
("+4444444444", "Thanks for the help!"),
]
contexts = []
for phone, message in customers:
context = manager.process_incoming_message(phone, message, "agent_001")
contexts.append((phone, context))
priority_order = {
MessagePriority.CRITICAL: 0,
MessagePriority.HIGH: 1,
MessagePriority.NORMAL: 2,
MessagePriority.LOW: 3
}
contexts.sort(key=lambda x: priority_order[x[1].priority])
Use Case 4: First-time vs Returning Customer
context = manager.process_incoming_message(
phone="+9999999999",
message_content="Hello",
agent_id="agent_001"
)
if context.customer.total_messages == 1:
print("👋 First time customer!")
else:
print(f"📊 Returning customer ({context.customer.total_messages} messages)")
Integration Examples
With WhatsApp Business API
from whatsapp_business_api import WhatsAppClient
from whatsapp_context_manager import ContextManager
wa_client = WhatsAppClient(api_key="your_key")
manager = ContextManager("production.db")
@wa_client.on_message
def handle_message(phone, message):
context = manager.process_incoming_message(
phone=phone,
message_content=message,
agent_id="auto_agent"
)
display_to_agent(context)
if context.priority == MessagePriority.CRITICAL:
notify_supervisor(context)
With Web Dashboard
from flask import Flask, jsonify
from whatsapp_context_manager import ContextManager
app = Flask(__name__)
manager = ContextManager()
@app.route('/api/message', methods=['POST'])
def process_message():
data = request.json
context = manager.process_incoming_message(
phone=data['phone'],
message_content=data['message'],
agent_id=data['agent_id']
)
return jsonify(context.to_dict())
Best Practices
1. Always Process Through System
context = manager.process_incoming_message(phone, message, agent_id)
send_reply_directly(phone, "Hello")
2. Mark VIP Customers
if customer_is_high_value(phone):
manager.update_customer_info(
phone=phone,
is_vip=True,
tags=["high-value", "premium"]
)
3. Track Orders
when_order_placed():
manager.add_order(order)
4. Use Suggested Responses
context = manager.process_incoming_message(phone, message, agent_id)
for i, response in enumerate(context.suggested_responses, 1):
print(f"{i}. {response}")
5. Monitor Priority Queue
pending_contexts = get_all_pending_messages()
pending_contexts.sort(key=lambda x: priority_order[x.priority])
Performance Tips
1. Database Management
dev_manager = ContextManager("development.db")
prod_manager = ContextManager("production.db")
test_manager = ContextManager("test.db")
2. Batch Processing
for phone, message in message_queue:
context = manager.process_incoming_message(phone, message, agent_id)
process_context(context)
3. Regular Cleanup
Security Features
- Local Storage: All data stored locally in SQLite
- No External Dependencies: Pure Python, no third-party libraries
- Data Integrity: SHA-256 checksums
- Secure Queries: Parameterized SQL, no injection risks
- Privacy: No data sent to external services
Troubleshooting
Issue: Database locked
manager1 = ContextManager("agent1.db")
manager2 = ContextManager("agent2.db")
Issue: Old data in tests
import os
if os.path.exists("test.db"):
os.remove("test.db")
Issue: No order suggestions
order = Order(...)
manager.add_order(order)
File Structure
whatsapp-context-manager/
├── whatsapp_context_manager.py # Main library
├── examples_whatsapp.py # 8 usage examples
├── test_whatsapp.py # Complete test suite
├── README_WHATSAPP.md # Full documentation
├── install_check_whatsapp.py # Installation check
├── requirements_whatsapp.txt # Dependencies (none!)
├── LICENSE_WHATSAPP # MIT License
└── .gitignore_whatsapp # Git ignore rules
Requirements
- Python 3.8 or higher
- No external dependencies!
Testing
python test_whatsapp.py
Examples
Run the examples to see the system in action:
python examples_whatsapp.py
Includes:
- Basic message processing
- Customer with active order
- Angry customer scenario
- VIP customer handling
- Conversation history
- Multiple customers priority queue
- Agent dashboard view
- Context export to JSON
Getting Help
- 📖 Read full documentation:
README_WHATSAPP.md
- 💻 Check examples:
examples_whatsapp.py
- 🧪 Run tests:
test_whatsapp.py
- 🐛 Report issues on GitHub
- ⭐ Star the repo if helpful!
Next Steps
- ✅ Install and verify:
python install_check_whatsapp.py
- ✅ Run tests:
python test_whatsapp.py
- ✅ Try examples:
python examples_whatsapp.py
- ✅ Integrate with your WhatsApp system
- ✅ Customize for your needs
License
MIT License - see LICENSE_WHATSAPP file
Author
cerbug45
Transform your WhatsApp customer service from reactive to proactive! 🚀