メニュー

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?

Workflow Automation with C# and ASP.NET MVC

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

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 C# and ASP.NET MVC 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.Web/Domain/Reservations/Notifier.cs

        ホストに通知する

        AirTNG.Web/Domain/Reservations/Notifier.cs

        Ready to go? Boldly click the button right after this sentence.

        Boldly Click Me to Move On

        Authenticate Users

        For this use case to work we have to handle authentication. We will rely on ASP.NET Identity for this purpose.

        Each User will need to have a phone_number that we will use later to send SMS notifications.

              
              
              
              
              AirTNG.Web/Models/IdentityModels.cs

              ユーザーモデル

              AirTNG.Web/Models/IdentityModels.cs

              Next let's take a look at the Vacation Property model.

              The Vacation Property Model

              Our rental application will obviously require listing properties.

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

              A VacationProperty can have many Reservations.

                    
                    
                    
                    
                    AirTNG.Web/Models/VacationProperty.cs

                    休暇物件モデル

                    AirTNG.Web/Models/VacationProperty.cs

                    Next, let's see what our Reservation model looks like.

                    Reservation Model

                    Reservation モデルは、このアプリケーションに対するワークフローの中心です。

                    以下の追跡を担当します。

                    • 関連付けられている VacationProperty
                    • The User who owns that vacation property (the host). Through this property the user will have access to the host phone number indirectly.
                    • 予約の Status
                          
                          
                          
                          
                          AirTNG.Web/Models/Reservation.cs

                          予約モデル

                          AirTNG.Web/Models/Reservation.cs

                          Now that our models are ready, let's have a look at the controller that will create reservations.

                          予約を作成する

                          The reservation creation form holds only a single field: the message that will be sent to the host when one of her properties is reserved. The rest of the information needed to create a reservation is taken from the VacationProperty itself.

                          A reservation is created with a default status ReservationStatus.Pending. That way when the host replies with an accept or reject response the application knows which reservation to update.

                                
                                
                                
                                
                                AirTNG.Web/Controllers/ReservationsController.cs

                                The Reservations Controller

                                AirTNG.Web/Controllers/ReservationsController.cs

                                Next, let's see how we will send SMS notifications to the vacation rental host.

                                Message the Host!

                                ホストに通知する

                                When a reservation is created we want to notify the owner of the property that someone is interested.

                                This is where we use Twilio C# Helper Library to send a SMS message to the host, using our Twilio phone number. As you can see, sending SMS messages using Twilio takes just a few lines of code.

                                Next we just have to wait for the host to send an SMS response accepting or rejecting the reservation. Then we can notify the guest and host that the reservation information has been updated.

                                      
                                      
                                      
                                      
                                      AirTNG.Web/Domain/Reservations/Notifier.cs

                                      ホストに通知する

                                      AirTNG.Web/Domain/Reservations/Notifier.cs

                                      Now's let's peek at how we're handling the host's responses.

                                      Handle Incoming Messages

                                      The Reservations/Handle controller controller handles our incoming Twilio request and does four things:

                                      1. Check for the guest's pending reservation
                                      2. Update the status of the reservation
                                      3. Respond to the host
                                      4. Send notification to the guest
                                            
                                            
                                            
                                            
                                            AirTNG.Web/Controllers/ReservationsController.cs

                                            Handle a host response

                                            AirTNG.Web/Controllers/ReservationsController.cs

                                            Let's have closer look at how Twilio webhooks are configured to enable incoming requests to our application.

                                            着信Twilioリクエスト

                                            In the Twilio console, you must setup the sms web hook to call your application's end point in the route Reservations/Handle.

                                            SMS Webhook

                                            One way to expose your development machine to the outside world is using ngrok. The url for the sms web hook on your number would look like this:

                                            http://<subdomain>.ngrok.io/Reservations/Handle
                                            

                                            An incoming request from Twilio comes with some helpful parameters, such as a from phone number and the message body.

                                            We'll use the from parameter to look for the host and check if he/she has any pending reservations. If he/she does, we'll use the message body to check for 'accept' and 'reject'.

                                            In the last step, we'll use Twilio's TwiML as a response to Twilio to send an SMS message to the guest.

                                                  
                                                  
                                                  
                                                  
                                                  AirTNG.Web/Controllers/ReservationsController.cs

                                                  Accept or reject a reservation

                                                  AirTNG.Web/Controllers/ReservationsController.cs

                                                  Now that we know how to expose a webhook to handle Twilio requests, let's see how we generate the TwiML needed.

                                                  TwiMLをいくつか生成する

                                                  TwiML レスポンス

                                                  After updating the reservation status, we must notify the host that he/she has successfully confirmed or rejected the reservation. We also have to return a friendly error message if there are no pending reservations.

                                                  If the reservation is confirmed or rejected we send an additional SMS to the guest to deliver the news.

                                                  TwiML の動詞 Message を使用して、SMS メッセージを送信する必要があることを Twilio のサーバーに指示します。

                                                        
                                                        
                                                        
                                                        
                                                        AirTNG.Web/Controllers/ReservationsController.cs

                                                        Respond to a message

                                                        AirTNG.Web/Controllers/ReservationsController.cs

                                                        Congratulations! You've just learned how to automate your workflow with Twilio SMS.

                                                        Next, lets see what else we can do with the Twilio C# SDK.

                                                        他に構築できるもの

                                                        次はどこでしょうか?

                                                        If you're a .NET developer working with Twilio you know we've got a lot of great content here on the Docs site. Here are just a couple ideas for your next tutorial:

                                                        IVR: フォンツリー

                                                        IVR(interactive voice response:自動音声応答)システムで、発信者を適切な担当者および情報に簡単にルーティングできます。 

                                                        自動アンケート

                                                        音声通話または SMS テキストメッセージを介して調査を実施し、ユーザーから構造化データを迅速に収集します。

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

                                                        Thanks for checking out this tutorial! Tweet to us @twilio with what you're building!

                                                        Agustin Camino Jose Oliveros Paul Kamp Andrew Baker 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.