Adding Postgres and ActiveRecord
Getting started - adding dependencies to your project
Let’s test your Twilio by first creating a test endpoint. It should look a little something like this (note it’s not fully formed - just an illustration)
get "/test/sms" do "An SMS was sent to your number " end
To send an SMS (e.g. to send an onboarding message to a user that just signed up). Modify the method to look something like this:
get "/test/sms" do client = Twilio::REST::Client.new ENV["TWILIO_ACCOUNT_SID"], ENV["TWILIO_AUTH_TOKEN"] # Include a message here message = "Hi! This is a test SMS from your Sinatra application." # this will send a message from any end point client.api.account.messages.create( from: ENV["TWILIO_FROM"], to: "+1XXXXXXXXX", body: message ) # response if eveything is OK "An SMS was sent to your number " end
Note Change the
"+1XXXXXXXXX" to your phone number.
Note When working with Twilio in trial mode you will only be able to send an SMS to your own phone number. You can send messages to other numbers if you charge up your account by adding $20 of credit. Nevertheless, you shouldn’t send an programmed SMS to someone unless you have their permission or they’ve opted into a conversation with your service. Keep in mind that Twilio supports opt out keywords allowing anyone to text STOP or CANCEL to stop receiving messages from your service. This allows them to blacklist your service permanently. Basically, use good practice and common sense when sending SMS to third-parties.
This method will work on your local machine just as it will on your remote Heroku deployment.
Load your Sinatra application locally by typing this code in your terminal window/command line using
http://localhost:4567/test/sms and see if you receive an SMS.
This endpoint allows anyone who visits that URL to send your phone an SMS. It wouldn’t be a good idea to deploy this to Heroku becasue ANYONE could begin to send you a lot of SMS messages.
You can use this code for sending an SMS in lots of great ways - just make sure to remove this endpoint before you deploy!