Page 1 of 1

Ruby Copy the code require

Posted: Sun Dec 22, 2024 7:10 am
by hasinam2206
Add the Twilio gem as a dependency: Bash Copy the code bundle add twilio-ruby This command adds the latest version of the library to the Gemfile. We will need to store credentials in the project, so add the dotenv gem as well : Bash Copy the code bundle add dotenv Once these two dependencies are configured, we need to use one file for our code and another for our credentials.

Create a file called send-sms.rb and another one called .env . Bash Copy the code touch send-sms.rb .env Open the .env file . We will store the credentials we need to 11-digit phone number format philippines call the API, as well as the Messaging Service SID we just created. Copy the lines below and enter the requested details for each of the credentials.

Image

Now let's move on to writing the code. Open the send-sms.rb file . Start by requesting "dotenv/load", which loads the variables listed in the .env file into the environment, and "twilio-ruby", the Twilio Ruby library.'dotenv/load' require 'twilio-ruby' Get the variables we need from the environment: Ruby Copy the code account_sid = ENV.

Using a command ENV.fetch like this will throw an error if the variable is not present in the environment, which is useful for identifying bugs quickly. To send a message, you need a From address, managed by our Messaging Service, a Destination number, and the message you want to send.