Menu

Rate this page:

Thanks for rating this page!

We are always striving to improve our documentation quality, and your feedback is valuable to us. How could this documentation serve you better?

Instant Lead Alerts with Python and Flask

You probably already have landing pages or product detail views in your web application which you're using to generate some excellent leads for your business. Would you like to let the sales team know when you've got a new qualified lead?

In this tutorial, we'll use Twilio Programmable SMS to send a message when a new lead is found for a Python and Flask application.

We'll be implementing instant lead alerts for a fictional real estate company.

We'll create a landing page for a new house on the market and notify a real estate agent instantly when a potential customer requests information.

Learn how Porch uses Twilio SMS to send home contractors instant alerts when they are selected for a new project.

Let's see how it works! Click the below button to begin.

Let's Qualify Some Leads

Populate a Landing Page

To display a landing page for our house, we first need to add some data so the customers can see if it's a good fit.

For demonstration purposes we've hard-coded a dictionary containing information we need.

        
        
        
        
        lead_alerts/views.py

        Let's look at how to render the page next.

        Render a House Page

        Render a Landing Page

        In our template we insert data about the house and in a sidebar we'll include a form for our home-shopping user.

        Here an interested shopper would enter their contact information to request more info about the home.

              
              
              
              
              lead_alerts/templates/index.html

              Home landing page template

              lead_alerts/templates/index.html

              Next, let's see how to initialize our Twilio Client for use later on.

              Next

              Initialize the Twilio REST API Client

              Now we need to create an authenticated Twilio REST API client that we can use anytime we need to send a text message.

              We initialize it with our Twilio Account Credentials stored as environment variables. You can find the Auth Token and Account SID in the console:

              console credentials

                    
                    
                    
                    
                    lead_alerts/services/twilio_service.py

                    Wrap the Twilio REST Client to simplify message sending

                    lead_alerts/services/twilio_service.py

                    Next up, let's see how to handle an incoming lead.

                    Leads Coming in Hot

                    Handle the POST Request

                    This code handles the HTTP POST request triggered by the user from our landing page.

                    It uses our TwilioService class to send an SMS message to the real estate agent's phone number, which is also set as a environment variable. We include the lead's name, phone number, and inquiry directly in the body of the text message sent to the agent.

                    Now the agent has all the information they need to follow up on the lead!

                          
                          
                          
                          
                          lead_alerts/views.py

                          That's it! We've just implemented an application to instantly route leads to sales people using text messages.

                          Next, let's look at some other easy to add features for your application.

                          What Else Can I Build?

                          Where to Next?

                          We love Python here at Twilio - here are some other Python tutorials we've written:

                          Appointment Reminders

                          Learn to implement appointment reminders on your web app with Twilio, and cut down on the no-shows.

                          Two-Factor Authentication with Authy

                          Learn to implement two-factor authentication (2FA) on your web app with Twilio-powered Authy.

                          Did this help?

                          Thanks for checking out this tutorial! Let us know what you've built - or what you're building - on Twitter.

                          Agustin Camino Jose Oliveros Paul Kamp Andrew Baker Kat King
                          Rate this page:

                          Need some help?

                          We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd browsing the Twilio tag on Stack Overflow.