Level up your Twilio API skills in TwilioQuest, an educational game for Mac, Windows, and Linux. Download Now


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?


Ready to implement SMS and MMS marketing notifications and see what potentially very high engagement rates will do for you?

Excellent! Today we're going to take you through implementing marketing notifications in Python and the Flask Microframework.


  1. A potential customer sends an SMS to a Twilio phone number you advertise online, on TV, in print, on a billboard, or so on.
  2. Your application confirms that the user wants to receive SMS and MMS notifications from your application and is added to the subscriber list.
  3. An administrator or marketing campaign manager crafts a message that is sent to subscribers via SMS/MMS message.

Walmartが「Value of the day」販促キャンペーンの一環として顧客に特売情報を毎日送信した方法をご覧ください。



  • TwiML and the <Message> verb: We'll use TwiML, the Twilio Markup Language, to manage interactions initiated by the user via SMS.
  • Messages Resource: We will use the REST API to broadcast messages to all subscribers.


Click the button below to move on to begin the tutorial.

Continue the Path to Marketing Notifications

The Subscriber Model


  • phone_number stores where to send the notifications.
  • subscribed identifies which subscribers are active (only active subscribers will receive notifications).

        Subscriber Python class


        Next up: how to handle incoming messages.

        Look At Incoming Messages


        This is the endpoint that will be called every time our application receives a message.

        1. We check if a user is in our database. If the user is not, encourage them to 'subscribe' and return TwiML through the helper library.
        2. If the user was registered, check that the command has a valid 'subscribe' or 'unsubscribe' in the body.
        3. If the command is valid, set the user's status to subscribed or not subscribed, respectively, and return appropriate TwiML through the helper library.

        So now we've created a Subscriber model to keep track of subscribers, unsubscribers, and new customers.


              Next, let's take a look at how to reply to received messages.

              The Marketing Reply

              Replying to Received Messages

              We ask Twilio to reply with an SMS by creating a twiml.Response and calling the message method on the created object.


                    Create a TwiML Response containing our message


                    Now let's look at how to manage subscriptions.


                    ユーザーの購読状態の管理に、subscribeunsubscribe という2つのSMSコマンドを提供することにします。 

                    These commands will toggle a boolean for their Subscriber record in the database. On the business side, this boolean will determine whether or not they receive messages from our marketing campaign. To respect our user's desires, don't opt them in automatically - rather, they have to confirm that they want to receive our messages.


                    • If it is a subscribe or unsubscribe command, then create/update their subscription with the right status in the database.
                    • If it is a command we don't recognize, send them a message with the available commands.

                          Validate and apply a subscriber's command


                          Next up, let's look at sending the marketing notifications.

                          Hit The Phones

                          Sending Marketing Notifications

                          On the server side, we retreive the message text and potential image URL. Next we loop through all Subscribers and call the method send_message on our TwilioServices domain object to send messages.

                          When the messages are on their way, we redirect back to the marketing form with a custom flash message containing feedback about the messaging attempt.


                                Webhook to send message to all active subscribers


                                Let's take an even closer look at how we are sending SMS (or MMS) notifications.

                                Show Me How It's Done


                                In the method send_message, we create a Twilio REST client that can be used to send SMS and MMS messages. The client requires your Twilio account credentials (an account SID and auth token), which can be found in the Twilio Console.

                                Next all we need to do is call create on the object in order to send our message. The Twilio Message API call requires a from_, to and a body parameters-- the media_url is optional.


                                      Use the Twilio Python Client to send a message


                                      That's it! We've just implemented an opt-in process and an administrative interface to run an SMS and MMS marketing campaign with Python and Flask. Now all you need is killer content to share with your users via text or MMS.

                                      We'll leave that part to you. However, we will suggest a few other awesome features for your application on the next pane.


                                      We've got lots of Python and Flask guides on the site, but here are a couple of our favorites:

                                      アポイント リマインダー

                                      Automate the process of reaching out to your customers in advance of an upcoming appointment to drive down your no-show rate.


                                      Convert web traffic into phone calls with the click of a button on your company's web site.


                                      Thanks for checking out this tutorial! Tweet us @twilio to let us know what you thought or what you're building next.

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


                                      誰しもが一度は考える「コーディングって難しい」。そんな時は、お問い合わせフォームから質問してください。 または、Stack Overflow でTwilioタグのついた情報から欲しいものを探してみましょう。