DevXStudio
Back to blog
AI & Chatbots Jul 5, 2026 6 min read

Building a WhatsApp Flight Search Chatbot with Python and Twilio

How I built a conversational flight-search chatbot on WhatsApp using Flask, Twilio, the AviationStack API, and Hugging Face for natural language understanding.

PythonFlaskTwilioAIWhatsApp
AI

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.

python
@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
python
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 on

Natural 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

  1. Session persistence: WhatsApp messages are stateless — each message is a separate HTTP request. You need to maintain state externally.
  2. Rate limits: Both Twilio and AviationStack have API rate limits. Cache results where possible.
  3. 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.