The Idea
What if you could search for flights just by sending a WhatsApp message? No app downloads, no sign-ups — just chat naturally. That was the premise behind my WhatsApp Flight Chatbot.
The Tech Stack
- Flask: Lightweight Python web framework to handle incoming WhatsApp messages
- Twilio WhatsApp API: The bridge between WhatsApp and my Flask server
- AviationStack API: Real-time flight data
- Hugging Face: Natural language processing to understand user intent
How It Works
When a user sends a WhatsApp message to my Twilio number, Twilio makes a POST request to my Flask webhook with the message content.
@app.route('/webhook', methods=['POST'])
def webhook():
message = request.form.get('Body')
sender = request.form.get('From')
response = process_message(sender, message)
resp = MessagingResponse()
resp.message(response)
return str(resp)The Conversation State Machine
The trickiest part of a chatbot is managing conversation state. I used a simple Redis-like in-memory dictionary to track where each user is in the conversation flow:
- State 0: Greet and ask for origin city
- State 1: Ask for destination city
- State 2: Ask for travel date
- State 3: Fetch and return flight results
user_states = {}
def process_message(sender, message):
state = user_states.get(sender, {'step': 0, 'data': {}})
if state['step'] == 0:
state['data']['origin'] = extract_city(message)
state['step'] = 1
user_states[sender] = state
return "Great! Where are you flying to?"
# ... and so onNatural Language Understanding with Hugging Face
Instead of requiring users to type exact city codes, I used a Hugging Face NER (Named Entity Recognition) model to extract location names from natural language. "I want to fly from Lahore to Dubai" becomes {origin: 'LHE', destination: 'DXB'}.
Key Challenges
- Session persistence: WhatsApp messages are stateless — each message is a separate HTTP request. You need to maintain state externally.
- Rate limits: Both Twilio and AviationStack have API rate limits. Cache results where possible.
- User experience: WhatsApp is a text-only medium. Formatting your responses with clear structure (using WhatsApp's bold, italic markers) makes a huge difference.
Watch the demo video to see it in action.