メニュー

Expand
ページを評価:

Twilio Ruby ヘルパーライブラリー

The twilio-ruby helper library lets you write Ruby code to make HTTP requests to the Twilio API. This library is open source, so if you find a feature missing or a bug, we encourage you to contribute back to the twilio-ruby project hosted on GitHub.

ライブラリーをインストールする

最も簡単にTwilio-RubyをRubyGemsでインストール

gem install twilio-ruby

手動インストール

もしくはtwilio-rubyのためにコピーできます。そこからライブラリをインストールします。

"Permission Denied" (権限がありません)

もしコマンドラインが"Permission Denied"のエラーを返した時上記のコマンドをsudoと共に実行して下さい。例はsudo gem install twilio-ruby

インストールをテストする

Test creating an SMS using your test credentials from https://twilio.com/console/settings, from a magic number to any number like so:

require 'twilio-ruby'

account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" # Your Test Account SID from www.twilio.com/console/settings
auth_token = "your_auth_token"   # Your Test Auth Token from www.twilio.com/console/settings

@client = Twilio::REST::Client.new account_sid, auth_token
message = @client.messages.create(
    body: "Hello from Ruby",
    to: "+12345678901",    # Replace with your phone number
    from: "+15005550006")  # Use this Magic Number for creating SMS

puts message.sid

If you get a message SID in the output, you know you've successfully created a message with your test credentials. You will neither be charged nor receive an actual SMS in the "to" number.

It's okay to hardcode your credentials when testing locally, but you should use environment variables to keep them secret before committing any code or deploying to production. Check out How to Set Environment Variables for more information.

Gemを使う

Try the Ruby quickstarts and refer to the open-source twilio-ruby specific documentation for details about the methods and classes contained in the library. Here are some simple examples of how to use the gem.

Authenticate the Client

require 'twilio-ruby'

account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" 
auth_token = "your_auth_token" 

@client = Twilio::REST::Client.new account_sid, auth_token

Create a New Record

require 'twilio-ruby'

account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' # Your Test Account SID from www.twilio.com/console/settings
auth_token = 'your_auth_token' # Your Test Auth Token from www.twilio.com/console/settings

# Initialize Twilio Client
@client = Twilio::REST::Client.new(account_sid, auth_token)

@call = @client.calls.create(
  url: 'http://demo.twilio.com/docs/voice.xml',
  to: '+14155551212',  #Replace with your phone number
  from: '+15005550006'  #User our Magic number to create test calls from
)

puts @call.sid

You will neither be charged, nor create a real call. But if you get a call SID in the output, you are good.

Get Existing Record

require 'twilio-ruby'

account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
auth_token = 'your_auth_token'

# Initialize Twilio Client
@client = Twilio::REST::Client.new(account_sid, auth_token)

@call = @client.api.calls('CA42ed11f93dc08b952027ffbc406d0868').fetch

puts @call.to

Iterate Through Records

require 'twilio-ruby'

account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
auth_token = 'your_auth_token'

# Initialize Twilio Client
@client = Twilio::REST::Client.new(account_sid, auth_token)

@client.calls.list
       .each do |call|
         puts call.direction
       end

The library automatically handles paging for you. Collections, such as calls and messages, have list and stream methods that page under the hood. With both list and stream, you can specify the number of records you want to receive (limit) and the maximum size you want each page fetch to be (page_size). The library will then handle the task for you.

list eagerly fetches all records and returns them as a list, whereas stream returns an enumerator and lazily retrieves pages of records as you iterate over the collection. You can also page manually using the page method.

For more information about these methods, view the auto-generated library docs.

例外ハンドリング

Twilio APIが400, 500番台のHTTPレスポンスを返した場合 twilio-rubyライブラリーはTwilio::REST::RequestErrorを返します。400番代のエラーは通常のエラーで(無効な番号、 SMSを送信できない番号など)あり、 適切に取り扱われるべきです。

require 'twilio-ruby'

account_sid = "{{ account_sid }}" # Your Account SID from www.twilio.com/console
auth_token = "auth_token"   # Your Auth Token from www.twilio.com/console

begin
    @client = Twilio::REST::Client.new account_sid, auth_token
    message = @client.messages.create(
        body: "Hello from Ruby",
        to: "+12345678901",    # Replace with your phone number
        from: "+12345678901")  # Replace with your Twilio number
rescue Twilio::REST::TwilioError => e
    puts e.message
end

追加ドキュメント

Twilioがどう動作するのか知りたい場合、もしくは構築に興味を持った場合、Rubyクイックスタートにトライしましょう。

残りのAPIドキュメントはRubyで、Twilio APIで取ることのできるいずれのアクション毎に書かれています。Rubyライブラリーでできることを知るためにここからスタートしましょう。

モジュールに含まれる機能の詳細をオープンソースtwilio-rubyのドキュメントでご覧いただけます。

Version 4.x of the Helper Library

The most recent version of the Ruby Helper Library is not API compatible with the previous 4.x version you may have used in previous Twilio applications. We encourage you to use the latest version possible. Refer to the Migration Guide if you find that you are using an older version.

お問い合わせ

まだ問題がありますか?

ページを評価:

ヘルプが必要ですか?

誰しもが一度は考える「コーディングって難しい」。そんな時は、お問い合わせフォームから質問してください。 または、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!

        ステップ1

        Get link

        Get a free personal referral link here

        ステップ2:

        Give $10

        Your user signs up and upgrade using link

        ステップ3

        Get $10

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