メニュー

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?

PythonおよびFlaskを使用したワークフローの自動化

ビジネスを構築していく中でより抽象的なコンセプトのひとつは、そのワークフローがどのようなものかということです。

At its core, setting up a standardized workflow is about enabling your service providers (agents, hosts, customer service reps, administrators, and the rest of the gang) to better serve your customers.

To illustrate a very real-world example, today we'll build a Python and Flask webapp for finding and booking vacation properties — tentatively called Airtng.

仕組みは次のとおりです:

  1. ホストが貸別荘の一覧を作成します
  2. A guest requests a reservation for a property
  3. ホストには予約リクエストを知らせるSMSが受信されます。 ホストは予約を「承諾」または「否認」します
  4. The guest is notified whether a request was rejected or accepted

AirbnbがTwilio SMSを使用して、世界中の6千万以上の旅行者たちに対するレンタル体験を明解にしている方法を学びましょう。

ワークフローの構成要素

We'll be using the Twilio REST API to send our users messages at important junctures. Here's a bit more on our API:

        
        
        
        
        airtng_flask/__init__.py

        Load the application configuration

        airtng_flask/__init__.py

        Let's get started! Click the below button to begin.

        Automate A Workflow

        User and Session Management

        For this workflow to work, we need to have Users created in our application, and allow them to log into Airtng.

        Our User model stores a user's basic information including their phone number. We'll use that to send them SMS notifications later.

              
              
              
              
              airtng_flask/models/user.py

              The Airtng User model

              airtng_flask/models/user.py

              Next, let's look at how we define the VacationProperty model.

              The VacationProperty Model

              In order to build a vacation rentals company we need a way to create the property listings.

              VacationProperty モデルはそれを作成した User(今後このユーザーをホストと呼ぶ)に属し、descriptionimage_url の 2 つのプロパティーだけが含まれます。

              We also include a couple database relationship fields to help us link vacation properties to their hosts as well as to any reservations our users make.

                    
                    
                    
                    
                    airtng_flask/models/vacation_property.py

                    休暇物件モデル

                    airtng_flask/models/vacation_property.py

                    Next we'll take a look at how to model a reservation.

                    予約モデル

                    Reservation モデルは、このアプリケーションに対するワークフローの中心です。以下の追跡を担当します。

                    • 予約を実行した guest
                    • The vacation_property the guest is requesting (and associated host)
                    • 予約のステータス: pending(保留)、confirmed(承認)、またはrejected(否認)
                          
                          
                          
                          
                          airtng_flask/models/reservation.py

                          予約モデル

                          airtng_flask/models/reservation.py

                          Now that we have our models, let's see how a user would create a reservation.

                          Creating a Reservation

                          The reservation creation form holds only one field, the message that will be sent to the host user when reserving one of her properties.

                          The rest of the information necessary to create a reservation is taken from the user that is logged into the system and the relationship between a property and its owning host.

                          A reservation is created with a default status pending, so when the host replies with an accept or reject response, the system knows which reservation to update.

                                
                                
                                
                                
                                airtng_flask/views.py

                                Routes for the Airtng workflow

                                airtng_flask/views.py

                                Now that we have seen how we will initiate a reservation, let's look at how to notify the host.

                                Show Me How to Notify the Host

                                Notify the Host via SMS

                                When a reservation is created for a property, we want to notify the host of the reservation request.

                                We use Twilio's Rest API to send a SMS message to the host, using a Twilio phone number.

                                Now we just have to wait for the host to send an SMS response accepting or rejecting the reservation. At that point we can notify the user and host and update the reservation information accordingly.

                                      
                                      
                                      
                                      
                                      airtng_flask/models/reservation.py

                                      Notify the user and the host

                                      airtng_flask/models/reservation.py

                                      Next, let's see how to handle incoming messages from Twilio webhooks.

                                      Hook 'Em

                                      Handle Incoming Twilio Requests

                                      This method handles the Twilio request triggered by the host's SMS and does three things:

                                      1. Checks for a pending reservation from a user
                                      2. 予約のステータスを更新する
                                      3. Responds to the host and sends a notification to the user

                                      Setting Up Incoming Twilio Requests

                                      In the Twilio console, you should change the 'A Message Comes In' webhook to call your application's endpoint in the route /confirm:

                                      SMS Webhook

                                      One way to expose your machine to the world during development is using ngrok. Your URL for the SMS web hook on your phone number should look something like this:

                                      http://<subdomain>.ngrok.io/confirm
                                      

                                      Twilio からの着信リクエストには、有益なパラメーターが付いています。From 電話番号やメッセージ Body などです。

                                      We'll use the From parameter to look up the host and check if he or she has any pending reservations. If she does, we'll use the message body to check for the message 'accepted' or 'rejected'. Finally, we update the reservation status and use the SmsNotifier abstraction to send an SMS to the guest telling them the host accepted or rejected their reservation request.

                                      TwiML レスポンス

                                      In our response to Twilio, we'll use Twilio's TwiML Markup Language to command Twilio to send an SMS notification message to the host.

                                            
                                            
                                            
                                            
                                            airtng_flask/views.py

                                            おめでとうございます!

                                            You've just learned how to automate your workflow with Twilio Programmable SMS. In the next pane, we'll look at some other features Twilio makes it easy to add.

                                            他に構築できるもの

                                            次はどこでしょうか?

                                            To improve upon this you could add anonymous communications so that the host and guest could communicate through a shared Twilio phone number: Call Masking with Python and Flask.

                                            また、他のチュートリアルもお楽しみください。

                                            IVR電話ツリー

                                            Create a seamless customer service experience by building an IVR (Interactive Voice Response) Phone Tree for your company.

                                            ワンクリック通話

                                            ウェブトラフィックをボタンのクリック 1 つで、電話のトラフィックに変換します。

                                            これは役に立ちましたか?

                                            Thanks for checking out this tutorial! If you have any feedback to share with us, please hit us up on Twitter and let us know what you're building!

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

                                            ヘルプが必要ですか?

                                            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.