|
1 | | -# twilio-ruby |
2 | | - |
3 | | -[][github-actions] |
4 | | -[](https://rubygems.org/gems/twilio-ruby) |
5 | | -[](https://twil.io/learn-open-source) |
6 | | - |
7 | | -## Documentation |
8 | | - |
9 | | -The documentation for the Twilio API can be found [here][apidocs]. |
10 | | - |
11 | | -The individual releases [here][refdocs]. |
12 | | - |
13 | | -## Versions |
14 | | - |
15 | | -`twilio-ruby` uses a modified version of [Semantic Versioning](https://semver.org) for all changes. [See this document](VERSIONS.md) for details. |
16 | | - |
17 | | -### Supported Ruby Versions |
18 | | - |
19 | | -This library supports the following Ruby implementations: |
20 | | - |
21 | | -* Ruby 2.4 |
22 | | -* Ruby 2.5 |
23 | | -* Ruby 2.6 |
24 | | -* Ruby 2.7 |
25 | | -* Ruby 3.0 |
26 | | -* Ruby 3.1 |
27 | | -* Ruby 3.2 |
28 | | - |
29 | | -* JRuby 9.2 |
30 | | -* JRuby 9.3 |
31 | | -* JRuby 9.4 |
32 | | - |
33 | | -### Migrating from 5.x |
34 | | - |
35 | | -[Upgrade Guide][upgrade] |
36 | | - |
37 | | -## Installation |
38 | | - |
39 | | -To install using [Bundler][bundler] grab the latest stable version: |
40 | | - |
41 | | -```ruby |
42 | | -gem 'twilio-ruby', '~> 7.5.2' |
43 | | -``` |
44 | | - |
45 | | -To manually install `twilio-ruby` via [Rubygems][rubygems] simply gem install: |
46 | | - |
47 | | -```bash |
48 | | -gem install twilio-ruby -v 7.5.2 |
49 | | -``` |
50 | | - |
51 | | -To build and install the development branch yourself from the latest source: |
52 | | - |
53 | | -```bash |
54 | | -git clone git@github.com:twilio/twilio-ruby.git |
55 | | -cd twilio-ruby |
56 | | -make install |
57 | | -``` |
58 | | - |
59 | | -> **Info** |
60 | | -> If the command line gives you an error message that says Permission Denied, try running the above commands with sudo. |
61 | | -> |
62 | | -> For example: `sudo gem install twilio-ruby` |
63 | | -
|
64 | | -### Test your installation |
65 | | - |
66 | | -To make sure the installation was successful, try sending yourself an SMS message, like this: |
67 | | - |
68 | | -```rb |
69 | | -require "twilio-ruby" |
70 | | - |
71 | | -# Your Account SID and Auth Token from console.twilio.com |
72 | | -account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" |
73 | | -auth_token = "your_auth_token" |
74 | | - |
75 | | -@client = Twilio::REST::Client.new account_sid, auth_token |
76 | | -message = @client.messages.create( |
77 | | - body: "Hello from Ruby", |
78 | | - to: "+12345678901", # Text this number |
79 | | - from: "+15005550006", # From a valid Twilio number |
80 | | -) |
81 | | - |
82 | | -puts message.sid |
83 | | -``` |
84 | | - |
85 | | -> **Warning** |
86 | | -> 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](https://www.twilio.com/blog/2017/01/how-to-set-environment-variables.html) for more information. |
87 | | -
|
88 | | -## Usage |
89 | | - |
90 | | -### Authenticate the Client |
91 | | - |
92 | | -```ruby |
93 | | -require 'twilio-ruby' |
94 | | - |
95 | | -# Your Account SID and Auth Token from console.twilio.com |
96 | | -account_sid = 'ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' |
97 | | -auth_token = 'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy' |
98 | | - |
99 | | -# Initialize the Twilio Client with your credentials |
100 | | -@client = Twilio::REST::Client.new account_sid, auth_token |
101 | | -``` |
102 | | - |
103 | | -### Use An API Key |
104 | | - |
105 | | -```ruby |
106 | | -require 'twilio-ruby' |
107 | | - |
108 | | -# Your Account SID from console.twilio.com |
109 | | -account_sid = 'ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' |
110 | | -# API Key from twilio.com/console/project/api-keys |
111 | | -api_key_sid = 'zzzzzzzzzzzzzzzzzzzzzz' |
112 | | -api_key_secret = 'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy' |
113 | | - |
114 | | -# set up a client to talk to the Twilio REST API using an API Key |
115 | | -@client = Twilio::REST::Client.new api_key_sid, api_key_secret, account_sid |
116 | | -``` |
117 | | - |
118 | | -### Specify a Region and/or Edge |
119 | | - |
120 | | -To take advantage of Twilio's [Global Infrastructure](https://www.twilio.com/docs/global-infrastructure), specify the target Region and/or Edge for the client: |
121 | | - |
122 | | -```ruby |
123 | | -# set up a client to talk to the Twilio REST API over a specific region and edge |
124 | | -@client = Twilio::REST::Client.new account_sid, auth_token, nil, 'au1' |
125 | | -@client.edge = 'sydney' |
126 | | - |
127 | | -# you may also specify the region and/or edge after client creation |
128 | | -@client = Twilio::REST::Client.new account_sid, auth_token |
129 | | -@client.region = 'au1' |
130 | | -@client.edge = 'sydney' |
131 | | -``` |
132 | | - |
133 | | -This will result in the `hostname` transforming from `api.twilio.com` to `api.sydney.au1.twilio.com`. |
134 | | - |
135 | | -### Make a Call |
136 | | - |
137 | | -```ruby |
138 | | -@client.calls.create( |
139 | | - from: '+14159341234', |
140 | | - to: '+16105557069', |
141 | | - url: 'http://example.com' |
142 | | -) |
143 | | -``` |
144 | | - |
145 | | -### Send an SMS |
146 | | - |
147 | | -```ruby |
148 | | -@client.messages.create( |
149 | | - from: '+14159341234', |
150 | | - to: '+16105557069', |
151 | | - body: 'Hey there!' |
152 | | -) |
153 | | -``` |
154 | | - |
155 | | -### List your SMS Messages |
156 | | - |
157 | | -```ruby |
158 | | -@client.messages.list(limit: 20) |
159 | | -``` |
160 | | - |
161 | | -### Fetch a single SMS message by Sid |
162 | | - |
163 | | -```ruby |
164 | | -# put the message sid you want to retrieve here: |
165 | | -message_sid = 'SMxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' |
166 | | -@client.messages(message_sid).fetch |
167 | | -``` |
168 | | - |
169 | | -### Iterate through records |
170 | | - |
171 | | -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. |
172 | | - |
173 | | -`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. |
174 | | - |
175 | | -For more information about these methods, view the [auto-generated library docs](https://www.twilio.com/docs/libraries/reference/twilio-ruby). |
176 | | - |
177 | | -```rb |
178 | | -require 'twilio-ruby' |
179 | | - |
180 | | -account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' |
181 | | -auth_token = 'your_auth_token' |
182 | | - |
183 | | -@client = Twilio::REST::Client.new(account_sid, auth_token) |
184 | | - |
185 | | -@client.calls.list |
186 | | - .each do |call| |
187 | | - puts call.direction |
188 | | - end |
189 | | -``` |
190 | | - |
191 | | -### Enable Debug logging |
192 | | - |
193 | | -In order to enable debug logging, pass in a 'logger' instance to the client with the level set to at least 'DEBUG' |
194 | | - |
195 | | -```ruby |
196 | | -@client = Twilio::REST::Client.new account_sid, auth_token |
197 | | -myLogger = Logger.new(STDOUT) |
198 | | -myLogger.level = Logger::DEBUG |
199 | | -@client.logger = myLogger |
200 | | - |
201 | | -@client = Twilio::REST::Client.new account_sid, auth_token |
202 | | -myLogger = Logger.new('my_log.log') |
203 | | -myLogger.level = Logger::DEBUG |
204 | | -@client.logger = myLogger |
205 | | -``` |
206 | | - |
207 | | -### Handle Exceptions {#exceptions} |
208 | | - |
209 | | -If the Twilio API returns a 400 or a 500 level HTTP response, the `twilio-ruby` |
210 | | -library will throw a `Twilio::REST::RestError`. 400-level errors are normal |
211 | | -during API operation (`“Invalid number”`, `“Cannot deliver SMS to that number”`, |
212 | | -for example) and should be handled appropriately. |
213 | | - |
214 | | -```rb |
215 | | -require 'twilio-ruby' |
216 | | - |
217 | | -account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' |
218 | | -auth_token = 'your_auth_token' |
219 | | - |
220 | | -@client = Twilio::REST::Client.new account_sid, auth_token |
221 | | - |
222 | | -begin |
223 | | - messages = @client.messages.list(limit: 20) |
224 | | -rescue Twilio::REST::RestError => e |
225 | | - puts e.message |
226 | | -end |
227 | | -``` |
228 | | - |
229 | | -### Debug API requests |
230 | | - |
231 | | -To assist with debugging, the library allows you to access the underlying request and response objects. This capability is built into the default HTTP client that ships with the library. |
232 | | - |
233 | | -For example, you can retrieve the status code of the last response like so: |
234 | | - |
235 | | -```ruby |
236 | | -require 'rubygems' # Not necessary with ruby 1.9 but included for completeness |
237 | | -require 'twilio-ruby' |
238 | | - |
239 | | -# Your Account SID and Auth Token from console.twilio.com |
240 | | -account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' |
241 | | -auth_token = 'your_auth_token' |
242 | | - |
243 | | -@client = Twilio::REST::Client.new(account_sid, auth_token) |
244 | | - |
245 | | -@message = @client.messages.create( |
246 | | - to: '+14158675309', |
247 | | - from: '+14258675310', |
248 | | - body: 'Ahoy!' |
249 | | -) |
250 | | - |
251 | | -# Retrieve the status code of the last response from the HTTP client |
252 | | -puts @client.http_client.last_response.status_code |
253 | | -``` |
254 | | - |
255 | | -### Customize your HTTP Client |
256 | | - |
257 | | -`twilio-ruby` uses [Faraday][faraday] to make HTTP requests. You can tell `Twilio::REST::Client` to use any of the Faraday adapters like so: |
258 | | - |
259 | | -```ruby |
260 | | -@client.http_client.adapter = :typhoeus |
261 | | -``` |
262 | | - |
263 | | -To use a custom HTTP client with this helper library, please see the [advanced example of how to do so](./advanced-examples/custom-http-client.md). |
264 | | - |
265 | | -To apply customizations such as middleware, you can use the `configure_connection` method like so: |
266 | | - |
267 | | -```ruby |
268 | | -@client.http_client.configure_connection do |faraday| |
269 | | - faraday.use SomeMiddleware |
270 | | -end |
271 | | -``` |
272 | | - |
273 | | -### Get started With Client Capability Tokens |
274 | | - |
275 | | -If you just need to generate a Capability Token for use with Twilio Client, you can do this: |
276 | | - |
277 | | -```ruby |
278 | | -require 'twilio-ruby' |
279 | | - |
280 | | -# put your own account credentials here: |
281 | | -account_sid = 'ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' |
282 | | -auth_token = 'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy' |
283 | | - |
284 | | -# set up |
285 | | -capability = Twilio::JWT::ClientCapability.new account_sid, auth_token |
286 | | - |
287 | | -# allow outgoing calls to an application |
288 | | -outgoing_scope = Twilio::JWT::ClientCapability::OutgoingClientScope.new 'AP11111111111111111111111111111111' |
289 | | -capability.add_scope(outgoing_scope) |
290 | | - |
291 | | -# allow incoming calls to 'andrew' |
292 | | -incoming_scope = Twilio::JWT::ClientCapability::IncomingClientScope.new 'andrew' |
293 | | -capability.add_scope(incoming_scope) |
294 | | - |
295 | | -# generate the token string |
296 | | -@token = capability.to_s |
297 | | -``` |
298 | | - |
299 | | -There is a slightly more detailed document in the [Capability][capability] section of the wiki. |
300 | | - |
301 | | -## OAuth Feature for Twilio APIs |
302 | | -We are introducing Client Credentials Flow-based OAuth 2.0 authentication. This feature is currently in beta and its implementation is subject to change. |
303 | | - |
304 | | -API examples [here](https://github.com/twilio/twilio-ruby/blob/main/examples/public_oauth.rb) |
305 | | - |
306 | | -### Generate TwiML |
307 | | - |
308 | | -To control phone calls, your application needs to output [TwiML][twiml]. |
309 | | - |
310 | | -You can construct a TwiML response like this: |
311 | | - |
312 | | -```ruby |
313 | | -require 'twilio-ruby' |
314 | | - |
315 | | -response = Twilio::TwiML::VoiceResponse.new do |r| |
316 | | - r.say(message: 'hello there', voice: 'alice') |
317 | | - r.dial(caller_id: '+14159992222') do |d| |
318 | | - d.client 'jenny' |
319 | | - end |
320 | | -end |
321 | | - |
322 | | -# print the result |
323 | | -puts response.to_s |
324 | | -``` |
325 | | - |
326 | | -This will print the following (except for the whitespace): |
327 | | - |
328 | | -```xml |
329 | | -<?xml version="1.0" encoding="UTF-8"?> |
330 | | -<Response> |
331 | | - <Say voice="alice">hello there</Say> |
332 | | - <Dial callerId="+14159992222"> |
333 | | - <Client>jenny</Client> |
334 | | - </Dial> |
335 | | -</Response> |
336 | | -``` |
337 | | - |
338 | | -## Docker Image |
339 | | - |
340 | | -The `Dockerfile` present in this repository and its respective `twilio/twilio-ruby` Docker image are currently used by Twilio for testing purposes only. |
341 | | - |
342 | | -## Getting help |
343 | | - |
344 | | -If you need help installing or using the library, please check the [Twilio Support Help Center](https://support.twilio.com) first, and [file a support ticket](https://twilio.com/help/contact) if you don't find an answer to your question. |
345 | | - |
346 | | -If you've instead found a bug in the library or would like new features added, go ahead and open issues or pull requests against this repo! |
347 | | - |
348 | | -[apidocs]: https://www.twilio.com/docs/api |
349 | | -[twiml]: https://www.twilio.com/docs/api/twiml |
350 | | -[libdocs]: https://www.twilio.com/docs/libraries/reference/twilio-ruby/ |
351 | | -[refdocs]: https://twilio.github.io/twilio-ruby |
352 | | -[capability]: https://github.com/twilio/twilio-ruby/wiki/JWT-Tokens |
353 | | -[wiki]: https://github.com/twilio/twilio-ruby/wiki |
354 | | -[bundler]: https://bundler.io |
355 | | -[rubygems]: https://rubygems.org |
356 | | -[gem]: https://rubygems.org/gems/twilio |
357 | | -[github-actions]: https://github.com/twilio/twilio-ruby/actions/workflows/test-and-deploy.yml |
358 | | -[upgrade]: https://github.com/twilio/twilio-ruby/wiki/Ruby-Version-5.x-Upgrade-Guide |
359 | | -[issues]: https://github.com/twilio/twilio-ruby/issues |
360 | | -[faraday]: https://github.com/lostisland/faraday |
| 1 | +# Test Release must not be used |
0 commit comments