メニュー

Expand
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?

Cシェルを使用して新しいTwilio番号からSMSを送信する

Today we'll make Bill jump for Joy by purchasing a new Twilio number and sending an SMS directly from csh (or, more likely, tcsh). Here you'll learn how to find an available number, purchase a number, and send an SMS with Twilio without ever leaving your C Shell.

CシェルにTwilioのクレデンシャルを設定する

Click to the Twilio console to get started. Log-in to your existing account or sign up for a free account (be sure to verify your number, you'll need it later). When that is done, look for the Account Summary as seen below:

コンソール内のTwilioアカウントサマリーセクション

Using that information, substitute the account_sid and auth_token variables below with the 'ACCOUNT SID' and 'AUTH TOKEN' variables from the Twilio Console. (Click on the eyeball icon to expose your AUTH TOKEN).

シェルで下記を実行します:

set account_sid="ACXXXXXXXXXXXXXXXXXXXXX"
set auth_token="your_auth_token"

Find a Twilio Number with cURL in the C Shell

Now that our Account SID and auth token are configured, we can query Twilio's REST API to find available phone numbers. We will need to find one (and purchase it) to send an SMS from our script.

set available_number=`curl -X GET \
"https://api.twilio.com/2010-04-01/Accounts/${account_sid}/AvailablePhoneNumbers/US/Local" \
-u "${account_sid}:${auth_token}" | \
sed -n "/PhoneNumber/{s/.*<PhoneNumber>//;s/<\/PhoneNumber.*//;p;}"`
echo $available_number

上記のコマンドをペーストしてreturnキーを押したら、available_numberにTwilioが返す最初の電話番号が代入されます。

この複雑な行で何が起こっているのか調べて見ましょう

  1. set available_number= is the syntax for setting a local variable. Everything contained in the backtick ( ` ) will be executed and set to available_number.
  2. curl -X GET "https://api.twilio.com/[omitted] -u "${account_sid}:${auth_token}" We use the software package cURL and the HTTP verb GET to ask Twilio's available numbers endpoint for a free number. '-u' is the flag to provide account credentials by HTTP Basic Auth. A ${variable_name} inside of double quotes substitutes the variable names we set earlier into this command.
  3. "|" A pipe character in the shell means the results of the previous command are passed to the next command (in this case, we pass Twilio's response to sed).
  4. sed -n "/PhoneNumber/{s/.*<PhoneNumber>//;s/<\/PhoneNumber.*//;p;}" ストリーム操作パッケージsedを使用して、TwilioのXMLレスポンスから最初に見つかった<PhoneNumber>タグを展開します。
  5. && はシェル内のコマンドの連結に使用できます。
  6. echo $available_number prints the variable available_number to the shell.

(For advanced users: note that the 'US' after AvailablePhoneNumbers in the URI is the ISO Country Code.)

Note: If you're not in a trial account, it will cost money to purchase this number. Check out our pricing page for more details.

Purchase a Twilio Phone Number in the C Shell

Twilioは利用可能な電話番号を返したので、これを購入しましょう。

下記を実行することで、電話番号を購入しアカウントに追加します:

curl -X POST -F "PhoneNumber=${available_number}" \
"https://api.twilio.com/2010-04-01/Accounts/${account_sid}/IncomingPhoneNumbers" \
-u "${account_sid}:${auth_token}"

リターンキーを押すと、晴れてこの新しいTwilio電話番号の持ち主になります(もし失敗した場合、他の人にすでに購入された可能性がありますので、「利用可能な電話番号」の手順をから再度実行してください)。

勘の鋭いあなたならおそらくお気づきでしょう:

  • 今回はHTTPのPOSTメソッドを使用しています。
  • We're POSTing the PhoneNumber parameter to the IncomingPhoneNumbers endpoint, and substituting the phone number we found in the last step

If everything went okay you now own available_number and are ready for the big payoff.

Send an SMS with Twilio in C Shell

Change the your_number variable below to a verified Twilio number in your account and run the following line:

set your_number="+15555555555"
curl -X POST -F "Body=Hi there, your new phone number is working." \
-F "From=${available_number}" -F "To={$your_number}" \
"https://api.twilio.com/2010-04-01/Accounts/${account_sid}/Messages" \
-u "${account_sid}:${auth_token}"

This command sends a POST request to the Messages resource with the three required parameters: Body, From, and To. You should explore all of the legal parameters for the Messages endpoint and see how to send images or set callback URLs for updates on message delivery status, among many options.

If everything went well, though, your phone should be playing a ringtone or vibrating off your desk. Try changing the body to add emojis or sending SMSes to other numbers.

Twilio Rings Phone Bells in the C Shell

Hitting useful Twilio endpoints without leaving csh should speed up your prototyping time immensely. Command line hacking is a great way to explore the Twilio API and add communication capabilities to your shell scripts. With your shell and this article, you can now search for numbers, purchase numbers, and send SMS messages without leaving csh - just use what *NIX gives you.

応用トピック:

Paul Kamp Andrew Baker Kat King
Rate this page:

ヘルプが必要ですか?

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