Node.jsおよびExpressを使用した到着予定通知
Uber、TaskRabbit、そしてInstacartといった企業は、私たち顧客がどこにいてもすぐに物品を注文したいという前提に立った産業を築き上げました。
The key to those services? Instant customer notifications when something changes.
UberはTwilioのSMSを使用して、顧客のカーシェアリングのリクエストに関する情報を更新し続けています。 さらに詳しく。
On this tutorial we'll build a notification system for a fake on-demand laundry service Laundr.io in Node.js and Express.
さぁ、はじめましょう! 下のボタンをクリックしてください。
通知のトリガー
This delivery driver's screen will show two buttons that allow him or her to trigger notifications: one for picking up an order and one for delivering it. Those buttons will be wired to the appropriate route:
- 配達スタッフが届ける洗濯物を回収する(
/pickup
) - Delivery person arrives at the customer's house (
/delive
r )
本番アプリケーションでの2番目の通知は、GPSを使用して配達員が地理的に顧客のそばに近づいたときにトリガーされます。 (今回はボタンだけで十分です。)
On the server we'll use the Twilio REST API Client to actually send out the notifications. Let's go there next.
Twilio REST クライアントの設定
ここでは、必要なときにいつでもテキスト・メッセージを送信するために使用できるTwilio REST APIクライアントを作成します。
We initialize it with our Twilio Account Credentials stored as environment variables. You can find the Auth Token and Account SID in the console:
次は: 通知のトリガーの処理方法
Handle a Notification Trigger
This code demonstrates how to handle a HTTP POST
request triggered by the delivery person.
It uses the sendSmsNotification
method of our Order
model to send an SMS message to the customer's phone number (which we have registered in our database). Simple!
Next, let's take a closer look at how we send the SMS.
メッセージの送信
SMSまたはMMSの実際の送信方法を示します。
Picture worth 1,000 words? Improve the message by adding some optional media with mediaUrl
:
'mediaUrl' : 'http://lorempixel.com/image_output/fashion-q-c-640-480-1.jpg'
必須のパラメーター(およびオプションのメディア)に加えて、statusCallback
URLを渡してメッセージが配信されたかどうか知ることができます。
メッセージ配信におけるステータス更新は興味深いものです - 次はこれを見てみましょう。
Handle an Incoming Twilio Callback
Twilio will make a POST
request to this controller each time our message status changes to one of the following: queued
, failed
, sent
, delivered
, or undelivered
.
We then update this notificationStatus
on the Order
and business logic dictates what we'd do next. This is an excellent place to add logic that would resend the message if it failed, or send out an automated survey after the customer receives their clothes and a delivery SMS.
これで以上です! 注文が回収されたり到着したりした時に顧客にアラートを発するオンデマンド通知サービスを実装しました。
ここで、Twilioによって実装が簡単になる、他の機能についても見ていきましょう。
関連トピック
Node and Twilio are like two peas in a pod. Here are some other tutorials where we demonstrate interesting features:
ビジネスに重要なワークフローを自動化し、応答率を上げましょう。 このチュートリアルでは、民泊会社向けのスケーリング可能な自動SMSワークフローの構築方法を学びます。
匿名状態でTwilio VoiceおよびSMSに接続して、エンドユーザーのプライバシーを保護します。 オンデマンドで使い捨て可能な電話番号を作成し、2者のユーザーが個人情報を交換することなくコミュニケーションできるようにする方法を学びます。
これは役に立ちましたか?
このチュートリアルをお読みいただき、ありがとうございます。 Twitter上で構築したもの(あるいはしているもの)をお知らせください。
ヘルプが必要ですか?
誰しもが一度は考える「コーディングって難しい」。そんな時は、お問い合わせフォームから質問してください。 または、Stack Overflow でTwilioタグのついた情報から欲しいものを探してみましょう。