RubyとSinatraを使用したSMSおよびMMS通知
Today we'll use Ruby and the Sinatra framework to demonstrate how to send SMS alerts to a list of system administrators when something goes wrong with your server. We'll cover all the key details in depth so you can easily incorporate this important feature into your own application.
GitHubからサンプルアプリケーションをクローンして、アプリケーションのREADME.mdを参照してアプリケーションのローカルでの実行方法を確認してください。
EMCがいかにしてTwilio SMSを使用して68,000人の従業員にITアラートを送信しているか見てみましょう。
はじめましょう!
Click the button below to begin the tutorial.
Twilio RESTクライアントの構成
To send messages we'll need to create a Twilio REST client which requires reading a TWILIO_ACCOUNT_SID
and TWILIO_AUTH_TOKEN
from environment variables.
アカウントSIDと認証トークンの値は、Twilio Consoleから取得できます:
Click the eyeball icon to expose your Auth Token in a form you can copy and paste.
Our Twilio REST Client is now ready. Let's take a closer look at that administrator list, shall we?
サーバー管理者(と、他に追加する必要のある人)の一覧
Here we create a yaml list of people who should be notified if a server error occurs.
データで不可欠な唯一の箇所は、各管理者のphone_number
です。
続いて、アプリケーションの例外の処理方法を見てみましょう。
アプリケーションの例外処理
We use Sinatra Errors Handlers and send out the notifications when an exception occurs.
続いて、各管理者に通知を送信します。
管理者一覧の各エントリーに対して通知をトリガーする
In our Notifier
module, we read the administrators from our YAML file and send alert messages to each one of them with the method send_sms
.
続いて、テキストメッセージの送信方法を見てみましょう。
テキストメッセージの送信
Twilio REST APIを使用したSMSの送信には、3つのパラメーターが必要です: from
、to
、およびbody
です。
US and Canadian phone numbers can also send an image with the message. Other countries will have an automatically shortened URL added to the message body.
このように至ってシンプルです!
We've just implemented an automated server notification system that can push out alerts if anything goes wrong.
Next, let's look at what other features the Twilio Ruby SDK makes simple to integrate.
次はどこでしょうか?
We've got a lot of other excellent Ruby code we'd love to share with you... but we'll keep our links to just two:
パスワードに加えてユーザーの携帯電話を検証することで、ログインシステムのセキュリティーを強化します。
SMS・MMSメッセージは ユーザーにより強く結びつき、e-mailよりも高いレートで開封され、tweetよりもインパクトがあります。
これは役に立ちましたか?
Thanks for checking out this tutorial! Tweet @twilio with what you thought, what you're thinking, and most importantly... what you're going to build.
ヘルプが必要ですか?
誰しもが一度は考える「コーディングって難しい」。そんな時は、お問い合わせフォームから質問してください。 または、Stack Overflow でTwilioタグのついた情報から欲しいものを探してみましょう。