This Sinatra application uses Twilio to connect incoming phone calls to other phone numbers based on where the caller lives. When a user dials in, we look up some information based upon their assumed location and trigger actions inside our application.

あなたのビジネスで、顧客をその地域の事業所にルーティングしたり、カスタマーサービスとのやりとり後に発信者をアンケートに案内するためにこの機能を使用できるでしょう。 サンプルアプリケーションは発信者を米国議員の事務所に転送します。

コミュニケーションは変化への大きな力になり得ます。 シビックエンゲージメントのためのツールがインターネットで接続されたモバイル社会でますます普及するにつれて、我々はシビックエンゲージメントの勃興を目の当たりにしてきました。 2016年11月、Emily Ellsworthは電話活動という大きなアイディアで議員の注目を獲得するための秘訣をいくつか公開しました。


Let’s get started!



For this application, we’ll be using the Twilio Ruby Helper Library to help us interact with the Twilio API. Our first step is to set up a Twilio account and the Sinatra application itself.


We’ve provided a sample set of data that can be loaded into your local database for testing and development. In our dataset, we’ve mapped states to senators, and we’ve mapped the senators to a few Twilio phone numbers for testing rather than actual senator phone numbers. Please note: this data set will likely be out of date by the time you use it, so we recommend you roll your own if you want to get the application production-ready.


        The last piece of the configuration puzzle is to create a webhook endpoint for our application to accept inbound calls. Once your database is configured and your app is up and running, go to the Twilio phone number you wish to use and configure the Voice URL to point to your application. In our sample code, the route is `/callcongress/welcome`.

        Twilio Webhookの構成

        We recommend using ngrok to expose your local development environment to Twilio.



        これで、Twilio番号が全着信通話について/welcomeエンドポイントにHTTPリクエストを送信するよう構成されました。 このTwilioのリクエストには、いくつかの便利なパラメーターが含まれています。 今回のユースケースでは、発信者の住んでいる州を推測するのに最適なため、fromStateがもっとも重要になります。





              Since the fromState parameter comes from the user’s phone rather than their actual geolocation, we want to make sure that we direct the caller to their state of residence. Let’s break down how we build out a response that both welcomes the caller and asks for confirmation of their state of residence.


                    <Say>で歓迎メッセージを読み上げることから始めましょう。 それから<Gather>を使用してユーザーに12をプッシュボタンで押してもらい、住んでいる州を確認します。

                    Once Twilio gathers this information, it will POST the caller’s input to our route specified on the action parameter so that we can better route the user through our application.



                    If for some reason the inbound request to Twilio doesn’t contain a fromState value, we need to get a little more information from the caller before we proceed.


                          This code should look familiar to you. If we don’t detect a fromState we utilize <Gather> as we <Say> a message that asks for the caller’s zip code. This time we accept 5 digits (the length of a zip code) and trigger a state lookup by zip code.

                          Let's start connecting



                          Similar to the previous route, we <Say> a brief message to tell the caller that they’re being connected to a senator. Then, we <Dial> the first senator, making sure to add an action that will route the caller back to our application when the first call ends.


                                The action attribute is great for redirecting a call in progress and is the backbone of our call forwarding use case.

                                However, it’s important to note that the action will only execute after the dialed party (in our case, the caller’s senator) ends the call. Twilio will continue to forward the original caller but they must stay on the line throughout the entire process.


                                Forward to the Next Senator and End the Call

                                Once the first senator ends the call with our user, the caller is forwarded to their second state senator. Just as we did in the previous route, we’ll include an action attribute that redirects the caller to a final bit of TwiML.


                                      2番目の議員との通話が終了すると、エンドユーザーに電話への短いお礼メッセージが再生されます。 それから、<Hangup>によって通話を終了します。



                                      一丁上がり! ngrokで開発サーバーを開始し、Twilio番号にダイヤルし、そして議員にルーティングできるはずです!

                                      But wait... this isn’t actually your senator’s phone number, remember? We seeded our sample application’s database with some placeholder phone numbers with lightweight TwiML endpoints, so there’s still some work to be done to flesh out this application before it’s production-ready.

                                      If your production case matches our demo's, ProPublica’s API grants access to a wealth of government data, including senators’ states and phone numbers. You may find yourself inspired to build out even more functionality for your civically engaged users.

                                      Interested in building something even bigger? See how twilio.org is helping people use messaging, voice, and video to advance their causes by connecting people and resources around the world.

                                      Whatever your use case, we hope you feel empowered to use what you've learned here to seamlessly forward your users' calls.



                                      ウェブトラフィックをボタンのクリック 1 つで、電話のトラフィックに変換します。




                                      このチュートリアルをご覧いただき、ありがとうございます。 ご意見やご感想などございましたら、ぜひお聞かせください。 Twitterをフォローして、どんなものを構築されているかお聞かせください!

                                      Hector Ortega Kat King Paul Kamp


                                      誰しもが一度は考える「コーディングって難しい」。そんな時は、お問い合わせフォームから質問してください。 または、Stack Overflow でTwilioタグのついた情報から欲しいものを探してみましょう。



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

                                            Sending your feedback...
                                            🎉 Thank you for your feedback!
                                            Something went wrong. Please try again.

                                            Thanks for your feedback!

                                            Refer us and get $10 in 3 simple steps!


                                            Get link

                                            Get a free personal referral link here


                                            Give $10

                                            Your user signs up and upgrade using link


                                            Get $10

                                            1,250 free SMSes
                                            OR 1,000 free voice mins
                                            OR 12,000 chats
                                            OR more