Programmable SMS クイックスタート for Node.js
Ahoy there! All messaging transmitted using Twilio’s messaging channels is treated as Application-to-Person (A2P) messaging and subject to Twilio’s Messaging Policy. For detailed information on policy rules to ensure you remain compliant while using Twilio’s services, please see our Acceptable Use Policy.
With just a few lines of code, your Node.js application can send and receive text messages with Twilio Programmable SMS.
This Node.js SMS Quickstart will teach you how to do this using our Communications REST API and the Twilio Node.js helper library.
このクイックスタートでは、下記のことを学んでいきます:
- Twilioにサインアップして、SMS機能を持ったはじめてのTwilio電話番号を入手する
- メッセージを送受信できるように開発環境をセットアップする
- 最初のSMSを送信する
- テキストメッセージを受信する
- 受信メッセージに対してSMSで返信する
ビデオを見ながらの入門がお好みですか? YouTube上のNode.js SMSクイックスタートビデオ(英語)をご覧ください。
Twilioにサインアップし電話番号を入手する
すでにTwilioアカウントとSMS機能を持ったTwilio電話番号をお持ちなら、すでに準備万端です! どうぞ次のステップまで読み飛ばしてください。
You can sign up for a free Twilio trial account here.
- When you sign up, you'll be asked to verify your personal phone number. This helps Twilio verify your identity and also allows you to send test messages to your phone from your Twilio account while in trial mode.
- Once you verify your number, you'll be asked a series of questions to customize your experience.
- Once you finish the onboarding flow, you'll arrive at your project dashboard in the Twilio Console. This is where you'll be able to access your Account SID, authentication token, find a Twilio phone number, and more.
If you don't currently own a Twilio phone number with SMS functionality, you'll need to purchase one. After navigating to the Buy a Number page, check the "SMS" box and click "Search."
すると、利用可能な電話番号と、その機能の一覧が表示されます。 お好みに合った番号が見つかったら「購入」をクリックすると、それがアカウントに追加されます。
すでに他のNode.jsクイックスタートを終えていて、Twilio Node.jsヘルパーライブラリーがインストールされている場合は、このステップを読み飛ばしてすぐに最初のテキストメッセージを送信できます。
To send your first SMS, you’ll need to have Node.js and the Twilio Node.js module installed.
Node.jsをインストールする
ターミナルを開いて下記のコマンドを入力することで、すでにNode.jsがすでにPCにインストール済みかどうか確認できます:
node --version
下記のように表示されます:
$ node --version
v8.9.1
If you don't have Node.js installed, head over to nodejs.org and download the appropriate installer for your system. Once you've installed Node, return to your terminal and run the command above once again. If you don't see the installed node version, you may need to relaunch your terminal.
Twilio Node.jsモジュールをインストールする
Install the Twilio Node helper library using npm:
npm install twilio
これによってカレント・ディレクトリー内のNode.jsスクリプトから使用できるように twilio
モジュールがインストールされます。
Node.jsでSMSメッセージを送信する
これでNode.jsとTwilio Node.jsライブラリーがインストールできたので、今しがた購入したTwilio電話番号から、1回のAPIリクエストでSMSを送信できます。 send_sms.js
という名前の新規ファイルを作成して開き、下記のコードサンプルを打ち込むかペーストします。
メッセージの送信前に、このファイルを少々編集する必要があります:
プレースホルダーのクレデンシャル値を置き換える
Swap the placeholder values for accountSid
and authToken
with your personal Twilio credentials. Go to https://www.twilio.com/console and log in. On this page, you’ll find your unique Account SID and Auth Token, which you’ll need any time you send messages through the Twilio client like this.
send_sms.js
を開き、accountSid
およびauthToken
に対応する値をご使用のアカウントの一意な値に置き換えます。
ご注意ください: 初めのうちは認証情報をハードコーディングしても構いませんが、本番環境にデプロイするにあたっては、環境変数を使用してこれらが漏洩しないようにしてください。 追加情報については、環境変数の設定方法を参照してください。
「From」電話番号を置き換える
数分前に購入した、SMS機能対応の電話番号を覚えていますか? どうぞ、既存のfrom
番号をご自身の番号に置き換えてください。 また、E.164形式が使用されていることも確認します。
[+][country code][phone number including area code]
「To」電話番号を置き換える
Replace the to
phone number with your mobile phone number. This can be any phone number that can receive text messages, but it’s a good idea to test with your own phone so you can see the magic happen! As above, you should use E.164 formatting for this value.
変更を保存し、ターミナルからこのスクリプトを実行します:
node send_sms.js
一丁上がり! ほどなくして、Twilio電話番号から携帯電話にSMSが受信されるはずです。
あなたのお客様は米国またはカナダにお住まいですか? もう1行コードを追加すれば、MMSメッセージの送信も可能です。 方法を確認するには、MMS送信のガイドを参照してください。
トライアルアカウントをご使用の場合、Twilioで検証済みの電話番号に送信先が制限されます。電話番号はTwilioコンソール内の検証済み発信者番号にて検証できます。
Expressでメッセージの受信と返信を行う
When your Twilio number receives an incoming message, Twilio will send an HTTP request to a server you control. This callback mechanism is known as a webhook. When Twilio sends your application a request, it expects a response in the TwiML XML format telling it how to respond to the message. Let's see how we would build this in Node.js using Express.
カレントディレクトリーのコマンドラインで、下記のコマンドを実行します:
npm install express
server.js
という名前のファイルを作成して下記のコードを使い、受信メッセージを処理できるサーバーを作成します。
下記コマンドでサーバーを実行します:
node server.js
ポート1337でのサーバーの起動が確認できるでしょう。
Before Twilio can send your application webhook requests, you'll need to make your application accessible over the Internet. While you can do that in any number of ways, we recommend using the Twilio CLI during local development. We'll show you how to set that up next so your app can receive messages.
Install the Twilio CLI
The suggested way to install twilio-cli
on macOS is to use Homebrew. If you don’t already have it installed, visit the Homebrew site for installation instructions and then return here.
Once you have installed Homebrew, run the following command to install twilio-cli
:
brew tap twilio/brew && brew install twilio
The suggested way to install twilio-cli
is by using Scoop, a command-line installer for Windows. If you don’t already have it installed, visit the Scoop site for installation instructions and then return here.
Note PowerShell will need to be run as an administrator to avoid common permission issues when installing via Scoop.
- Add the
twilio-cli
Bucket:
scoop bucket add twilio-scoop https://github.com/twilio/scoop-twilio-cli
- Install the app:
scoop install twilio
twilio-cli
can be installed using the Advanced Package Tool (apt
) on most distributions such as Debian, Ubuntu, and Mint.
To do so, run the following commands in your terminal:
wget -qO- https://twilio-cli-prod.s3.amazonaws.com/twilio_pub.asc \
| sudo apt-key add -
sudo touch /etc/apt/sources.list.d/twilio.list
echo 'deb https://twilio-cli-prod.s3.amazonaws.com/apt/ /' \
| sudo tee /etc/apt/sources.list.d/twilio.list
sudo apt update
sudo apt install -y twilio
For other installation methods, see the Twilio CLI Quickstart.
Run twilio login
to get the Twilio CLI connected to your account. Visit https://www.twilio.com/console, and you’ll find your unique Account SID and Auth Token to provide to the CLI.
認証トークンは、目玉アイコンをクリックすると表示されます:
Now, you can use the CLI to connect your phone number to your Node.js app.
WebhookのURLを設定する
Now, you need to configure your Twilio phone number to call your webhook URL whenever a new message comes in. Just run this CLI command, replacing the phone number with your Twilio phone number:
twilio phone-numbers:update "+15017122661" --sms-url="http://localhost:1337/sms"
What's happening here?
We're using the Twilio CLI to set the SMS webhook URL for your phone number. Twilio will make a request to this URL whenever a new SMS message is received. The CLI is also using ngrok to create a tunnel to allow Twilio to reach your local development server (aka "localhost").
You can also use the Twilio console to set a webhook in your web browser, but you will have to start up ngrok yourself.
アプリケーションをテストする
Make sure you are running on the command line (in separate tabs) both node server.js
and your twilio
command.
上記の両方のサーバーの実行により、いよいよお楽しみの準備が整いました。 新しいExpressアプリケーションをテストしましょう!
携帯電話からこのWebhookで構成されたTwilio電話番号にSMSを送信します。 ngrokコンソールでHTTPリクエストが確認できるはずです。 Expressアプリケーションはテキストメッセージを処理して、SMSとして返信されてきます。
関連トピック
Node.jsを使用したSMSおよびMMSの送受信の基本について理解できたところで、下記の資料について調べてみるのも良いかもしれません。
- REST API documentation
- TwiMLリファレンス・ドキュメント
- Node.js用の完全なサンプルアプリケーションを含むチュートリアル
- Twilioを使用して、Nodeで30秒以内でテキストを送信する
- Node.js、Express、そしてTwilioでテキストメッセージを受信、返信する
楽しいプログラミングを!
ヘルプが必要ですか?
We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd by visiting Twilio's Stack Overflow Collective or browsing the Twilio tag on Stack Overflow.