C#およびASP.NET MVCを使用した到着予定通知
Uber、TaskRabbit、そしてInstacartといった企業は、私たち顧客がどこにいてもすぐに物品を注文したいという前提に立った産業を築き上げました。 これらのサービスが機能するうえでの肝はなんでしょうか? それは物事が変化した時に顧客に通知することです。
UberはTwilioのSMSを使用して、顧客のカーシェアリングのリクエストに関する情報を更新し続けています。 さらに詳しく。
In this tutorial, we'll build a notification system for a fake on-demand laundry service Laundr.io in C# and the ASP.NET MVC.
さぁ、はじめましょう! 下のボタンをクリックして始めましょう。
Trigger a Notifications
取り扱うケースは次の 2 つです。
- 配達スタッフが届ける洗濯物を回収する(
/Pickup
) - Delivery person is arriving at the customer's house (
/Deliver
)
運用アプリでは、おそらく、GPS を使用して、配達員が物理的に顧客の家の近くに来ると、2 番目の通知をトリガーします。
(In this case we'll just use a button!)
Next, let's look at how we set up the Twilio REST API Client to push out notifications.
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:
Twilio REST Client ready, routes in place? Let's now look at how we handle notification triggers.
Handle a Notification Trigger
This code handles a HTTP POST
request triggered by a delivery person in the field.
It uses our INotificationServices
instance to send an SMS message to the customer's phone number, which we have registered in our database. Simple!
Now let's look closer at how we send out the SMS.
Send an SMS (or MMS)
This code demonstrates how we actually send the SMS.
Something missing? An image of the clothes, perhaps?
You can add an optional mediaUrl
as well. Glad you pointed that out!
var mediaUrls = new List<Uri> () { new Uri("http://lorempixel.com/image_output/fashion-q-c-640-480-1.jpg") }; var to = new PhoneNumber(phoneNumber); await MessageResource.CreateAsync( to, from: new PhoneNumber(_twilioNumber), body: message, statusCallback: new Uri(statusCallback), client: _client, mediaUrl: mediaUrls);
In addition to the required parameters, we can pass a `status_callback
` url. Twilio will then POST
there to let us know when the status of our outgoing message changes.
Let's look at the status callback in greater detail.
Handle a Twilio Status Callback
Twilio will make a POST
request to this resource 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 can take over. This is a great place to add logic that would resend the message upon failure, or send out an automated survey a few minutes after a customer receives an order.
That's a wrap (fold?)! We've just implemented an on-demand notification service that alerts our customers when their order is picked up and arriving.
Now let's look at other easy to add features for your application.
次はどこでしょうか?
We love ASP.NET and C# here at Twilio. Here are a couple other tutorials we've written:
ビジネスに重要なワークフローを自動化し、応答率を上げましょう。 このチュートリアルでは、民泊会社向けのスケーリングスケーリング可能な自動SMSワークフローの構築方法を学びます。
匿名状態でTwilio VoiceおよびSMSに接続して、エンドユーザーのプライバシーを保護します。 オンデマンドで使い捨て可能な電話番号を作成し、2者のユーザーが個人情報を交換することなくコミュニケーションできるようにする方法を学びます。
これは役に立ちましたか?
このチュートリアルをお読みいただき、ありがとうございます。 Twitter上で構築したもの(あるいはしているもの)をお知らせください。
ヘルプが必要ですか?
誰しもが一度は考える「コーディングって難しい」。そんな時は、お問い合わせフォームから質問してください。 または、Stack Overflow でTwilioタグのついた情報から欲しいものを探してみましょう。