メニュー

Rate this page:

Thanks for rating this page!

We are always striving to improve our documentation quality, and your feedback is valuable to us. How could this documentation serve you better?

SMS and MMS Marketing Notifications with Ruby and Sinatra

Ready to implement SMS and MMS marketing notifications in your Ruby and Sinatra application?

Here's how it will work at a high level:

  1. A possible customer sends an SMS to a Twilio phone number you advertise somewhere.
  2. Your application confirms that the user wants to receive SMS and MMS notifications from your company.
  3. 管理者またはマーケティングキャンペーンマネージャーは、ウェブフォームを使用して、すべての購読者に SMS/MMS メッセージで送信されるメッセージを作成します。

Walmartが「Value of the day」販促キャンペーンの一環として顧客に特売情報を毎日送信した方法をご覧ください。

ビルディングブロック

これを行うには、以下のツールを操作します。

  • TwiML and the <Message> Verb: We'll use TwiML to manage interactions initiated by the user via SMS.
  • Messages Resource: We will use the REST API to broadcast messages out to all subscribers.
  • Sinatra Framework: We'll use Sinatra to structure the application.
  • DataMapper: We'll use DataMapper to define the model and to persist it.

さぁ、はじめましょう! 下のボタンをクリックして、チュートリアルの次の手順に移動してください。

The Subscriber Model

マーケティング通知を購読者に送信するためには、正しいモデルを提供する必要があります。

  • phone_number will store where to send the notifications.
  • subscribed lets the application identify which subscribers are active (only an active subscriber will receive notifications).
        
        
        
        
        model/subscriber.rb

        The Subscriber model

        model/subscriber.rb

        Next up, let's see how to handle incoming messages.

        Handle Incoming messages

        これは、アプリケーションがメッセージを受信するたびに呼び出されるエンドポイントです。

        It checks if the command sent in the message is one of the commands that we are expecting. Depending on that it returns a message formatted in TwiML - either a confirmation or a helpful message with our legal commands.

              
              
              
              
              app.rb

              Let's see how to create new subscribers next.

              Create New Subscribers!

              Create New Subscribers

              On an incoming message, we look in the database for a matching number. If there isn't one, we create a new Subscriber. If it does, we validate the command from the message body and send an appropriate TwiML response.

              このステップで必要な作業はこれで終わりです。Subscriber モデルを作成して、メッセージをリクエストした人々を追跡しました。また、'add' コマンドを使用して、テキストメッセージ受信後にこの情報をデータベースに保存しました。

                    
                    
                    
                    
                    app.rb

                    Now let's look at how our users can manage their subscriptions.

                    購読の管理

                    We want to provide our users with two SMS commands to manage their subscription status: add and remove.

                    These commands will toggle a boolean flag for a Subscriber record in the database and will determine whether or not our user will receive messages from our marketing campaign. We don't opt them in automatically - rather, we have them confirm that they want to receive our messages.

                    これを実現するためには、着信テキストメッセージを処理するコントローラーロジックを更新して、以下の操作を実行する必要があります。

                    • If it is an add command or remove command, then create/update her or his subscription with the right status in the database.
                    • If it is a command we don't recognize, then send a message explaining our available commands.
                          
                          
                          
                          
                          app.rb

                          Next up, let's look at how to send notifications.

                          How Do I Send Notifications?

                          通知の送信

                          When we receive a form submission from the frontend, we first grab the message text and optional image URL. Second, we loop through all Subscribers and call the send_message method to send the message out.

                          When the messages are on their way, we render back the index page with a success message (and make the marketing team happy).

                                
                                
                                
                                
                                app.rb

                                Let's take an even closer look at how to send SMS or MMS notifications.

                                Zoom in on Sending MMS and SMS Messages

                                SMS または MMS 通知を送信する

                                In the method send_message we create a Twilio REST API client that can be used to send SMS and MMS messages. The client requires your Twilio account credentials (an account SID and auth token), which can be found in the console:

                                コンソールの認証情報

                                Next all we need to do is call create on the client.messages object in order to send our message. The Twilio Message API call requires a from, to and body parameter. The media_url is optional.

                                      
                                      
                                      
                                      
                                      model/subscriber.rb

                                      That's it! We've just implemented an opt-in process and an administrative interface to run an SMS and MMS marketing campaign.

                                      Next let's look at what other features you might like to deploy.

                                      他に構築できるもの

                                      次はどこでしょうか?

                                      Twilio and Ruby mix incredibly well. To prove it, here are two excellent tutorials on how to add other features:

                                      従業員目録

                                      Add an interface to your employee directory to look up contact information through your phone.

                                      自動アンケート

                                      この簡単なチュートリアルに従って操作を行い、CRM および顧客データベースと直接統合される自動調査を実装するのに必要なコードを確認してください。

                                      これは役に立ちましたか?

                                      このチュートリアルをご覧いただき、ありがとうございます。 @twilioにツイートして、ご意見をお寄せください!

                                      Jarod Reyes Jennifer Aprahamian Agustin Camino Kat King Hector Ortega Paul Kamp Andrew Baker
                                      Rate this page:

                                      ヘルプが必要ですか?

                                      We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd browsing the Twilio tag on Stack Overflow.