RubyとRailsを使用したSMSおよびMMS(日本未対応)通知
Ruby, Rails, and an automatic ping for all of your server administrators when something goes wrong? We hope you're as excited to build that application as we were when we wrote this tutorial.
We'll use Ruby on Rails and demonstrate how to send SMS (or MMS) alerts to a list of system administrators if something goes wrong on your server. Follow along as we demonstrate the key pieces of code to make it all work.
GitHub上からリポジトリーをクローンし、Readmeを使用してローカルの開発環境を準備すれば、この先の手順への準備は万端です。
EMCがいかにしてTwilio SMSを使用して68,000人の従業員にITアラートを送信しているか見てみましょう。
さぁ、はじめましょう! 下のボタンをクリックして、チュートリアルの次の手順に移動してください。
A List of Your Server Admins... And Whomever Else as Well
Yamlでは、サーバーのエラー発生時に通知を受け取る必要のある管理者の一覧を作成します。
データで不可欠な唯一の箇所は、各管理者のphone_number
です。
準備ができたら、Twilio RESTクライアントを構成しましょう!
Twilio RESTクライアントの構成
To send a message, we'll need to initialize the Twilio REST client which requires reading our TWILIO_ACCOUNT_SID
and TWILIO_AUTH_TOKEN
from environment variables. That link shows you how to set environment variables inside *NIX, Mac OSX and Windows - but it may vary based on your choice of shell. If you are on another platform, you'll need to consult the platform documentation.
これらの2つの値は、Twilio Consoleから取得できます。
Next, let's take a look at how the application is set up to handle exceptions.
アプリケーションの例外処理
We take advantage of the rescue_from
method to rescue any exception raised in controller actions. With those exceptions under control, we can send out a notification when something goes wrong - and get our administrators on the case, quickly.
続いて、想像力を発揮させて、カスタムな例外アラートメッセージをいかに作成するのか見てみましょう。
Creating a Custom Alert Message
ここでは、テキストメッセージを介して送信されるアラートメッセージを作成します。
You might also decide to include a picture with your alert message. Perhaps a screenshot of the application when the crash happened? A funny gif? Kid pictures?
さて、先に進んでYamlファイルからの管理者の読み込みについて見ていきましょう。
YAMLファイルから管理者の一覧を読み込む
続いて、YAMLファイルから管理者を読み込み、プライベートなsend_message
メソッドで各管理者にアラートメッセージを送信します。
ここまで、メッセージのメッセージにまつわる下準備について見てきましたので、これらの送信について見てみましょう。
テキストメッセージの送信
Twilio REST APIを使用したSMSの送信には、3つのパラメーターが必要です: from
、to
、およびbody
です。
メッセージの送信後、メッセージを送信している電話番号を出力します。
US and Canadian phone numbers can also send an image with the message as an MMS (uncomment media_url
). The rest of the world will have image links with automatically shortened links.
And it's as simple as that! You've now got a Ruby on Rails server application which uses the server to notify your server administrators when something goes wrong with the... server.
What else can you do with Twilio? Let's look into it on the next pane...
次はどこでしょうか?
Twilio を使う Ruby 開発者であれば、他のチュートリアルも調べてみてください。
Increase the security of your login system by verifying a user using his or her mobile phone in addition to a password.
Customers missing things scheduled months in advance? Build an appointment reminder application with Twilio and reduce the number of no-shows.
これは役に立ちましたか?
このチュートリアルをご覧いただき、ありがとうございます。 @twilioにツイートして、ご意見をお寄せください。 そして次に構築予定のものをぜひおしらせください!
ヘルプが必要ですか?
誰しもが一度は考える「コーディングって難しい」。そんな時は、お問い合わせフォームから質問してください。 または、Stack Overflow でTwilioタグのついた情報から欲しいものを探してみましょう。