JavaおよびServletを使用したワークフローの自動化
ビジネスを構築していく中でより抽象的なコンセプトのひとつは、そのワークフローがどのようなものかということです。
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 Java and Servlets webapp for finding and booking vacation properties — tentatively called Airtng.
仕組みは次のとおりです:
- ホストが貸別荘の一覧を作成します
- A guest requests a reservation for a property
- ホストには予約リクエストを知らせるSMSが受信されます。 ホストは予約を「承諾」または「否認」します
- 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:
Ready to go? Boldly click the button right after this sentence.
User and Session Management
このワークフローが機能するためには、ユーザーモデルとログインを許可することが必要です。
続いて、Vacation Property(貸別荘)のモデルについて見ていきましょう。
休暇物件モデル
In order to build a true vacation rental company we'll need a way to create a property rental listing.
VacationProperty
モデルはそれを作成した User
(今後このユーザーをホストと呼ぶ)に属し、description
と imageUrl
の 2 つのプロパティーだけが含まれます。
The model has two associations implemented: there are many reservations in it and many users can make those reservations.
続いて、完全に実装されたReservationモデルについて見てみましょう。
予約モデル
Reservation
モデルは、このアプリケーションに対するワークフローの中心です。以下の追跡を担当します。
- 予約を実行した
guest
- The
vacation property
the guest is requesting (and associated host) - 予約のステータス:
pending
(保留)、confirmed
(承認)、またはrejected
(否認)
続いて、新しいアプリケーションが新規予約を作成する方法をご紹介します。
予約を作成する
The reservation creation form holds only one field: the message that will be sent to the host when reserving one of her properties.
The rest of the information necessary to create a reservation is taken from the logged in user and the relationship between a property and its owner. Our base generic Repository
is in charge of handling entity insertion.
A reservation is created with a default status pending.
This lets our application easily react to a host rejecting or accepting a reservation request.
新しい予約リクエストが送信されたときにhost
に通知する方法を次に見てみましょう。
ホストに通知する
物件に対して予約が作成されたら、誰かが予約を行ったことをホストに通知する必要があります。
We use an abstraction called SmsNotifier
which under the hood uses another abstraction called Sender
. Here is where we use Twilio's Rest API to send an SMS message to the host using your Twilio phone number. That's right - it's as simple as that to send a SMS with Twilio.
次のステップでは、予約の承認または否認をするホストの処理方法をお見せします。 続けましょう。
Handle Incoming Messages
ReservationConfirmation
サーブレットをもう少し詳しく見てみましょう。このサーブレットは、着信 Twilio リクエストを処理し、以下の 3 つの操作を行います。
- 受信ユーザーからの保留中の予約を確認する
- 予約のステータスを更新する
- ホストに応答し、ゲストに通知を送信する
着信Twilioリクエスト
In the Twilio console, you should change the 'A Message Comes In' webhook to call your application's endpoint in the route /reservation-confirmation:
One way to expose your machine to the world during development is to use ngrok. Your URL for the SMS web hook on your phone number should look something like this:
http://<subdomain>.ngrok.io/reservation-confirmation
An incoming request from Twilio comes with some helpful parameters, including the From
phone number and the message Body
.
From
パラメーターを使用してホストを検索し、保留中の予約の有無を確認します。 予約がある場合は、メッセージ本文を「承認」または「否認」の確認に使用します。
最後に、予約ステータスを更新して、SMSを送信してguestに知らせるためにSmsNotifier
アブストラクションを使用します。
続いて、Twilioに応答する方法を見てみましょう。
TwiML レスポンス
最後にレスポンスとしてTwilioのTwiMLを使用して、TwilioのサーバーにhostにSMS通知Sメッセージを送信するよう指示を出します。
We'll be using the Message
verb to instruct Twilio's server that it should send the message.
Congrats! You've just learned how to automate your workflow with Twilio SMS.
Next, let's look at some other interesting features you might want to add to your application.
次はどこでしょうか?
Like Twilio? Like Java? You're in the right place. Here are a couple other excellent tutorials:
サーバー停止状態が発生した場合に SMS 経由ですべての管理者に警告するサーバー通知システムを構築します。
ウェブトラフィックをボタンのクリック 1 つで、電話のトラフィックに変換します。
これは役に立ちましたか?
Thanks for checking this tutorial out! Tweet to us @twilio with what you're building!
ヘルプが必要ですか?
誰しもが一度は考える「コーディングって難しい」。そんな時は、お問い合わせフォームから質問してください。 または、Stack Overflow でTwilioタグのついた情報から欲しいものを探してみましょう。