Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Incorporating SMS into your Rails app using Twilio

IndyRB
April 13, 2016

Incorporating SMS into your Rails app using Twilio

by Andrew Porterfield at IndyRB on April 13, 2016

IndyRB

April 13, 2016
Tweet

Other Decks in Technology

Transcript

  1. Did you know? • Text messages have a 98% open

    rate (vs. email’s 20%) • 90% of text messages are read within 3 minutes of delivery • Texting is the most frequently used feature on phones today (more than internet use, voice calls, email, social media, etc)
  2. Getting started • Sign up for a free at https://www.twilio.com/try-

    twilio • Get a local Twilio phone number • Create an API key • Add the twilio-ruby gem to your Gemfile • https://github.com/twilio/twilio-ruby
  3. Sending your first SMS # Creating a new client @client

    = Twilio::REST::Client.new account_sid, auth_token # Or, pre-configure in an initializer Twilio.configure do |config| config.account_sid = account_sid config.auth_token = auth_token end # Then you can create a new client without parameters @client = Twilio::REST::Client.new
  4. Sending your first SMS @client.messages.create( from: '+14159341234', # your Twilio

    number to: '+16105557069', body: ‘indyrb: Reminder that our monthly meetup is tonight @ 7pm at The Speak Easy. See http://bit.ly/1Mozn3q for more details. Reply STOP to cancel.’, )
  5. Two-factor authentication • Let’s look at Twitter’s implementation • Getting

    started with Authy • Sign up through Twilio. • Add the authy-ruby gem to your Gemfile • https://github.com/authy/authy-ruby
  6. Authy # config/initializers/authy.rb Authy.api_key = 'your-api-key' Authy.api_uri = ‘https://api.authy.com/' #

    STEP 1: Register user w/authy response = Authy::API.register_user( email: @user.email, cellphone: @user.phone_number, country_code: 1 ) @user.update(authy_id: response.id)
  7. Authy # STEP 2: Send SMS w/token Authy::API.request_sms(id: @user.authy_id) #

    STEP 3: Verify token response = Authy::API.verify( id: @user.authy_id, token: params[:token] ) if response.ok? sign_in @user redirect_to account_path else flash.now[:error] = 'Incorrect code, please try again' render 'new' end
  8. TwiML - Responding to incoming messages response = Twilio::TwiML::Response.new do

    |r| r.Message 'Hello indy.rb!' end puts response.text <?xml version="1.0" encoding="UTF-8"?> <Response> <Message>Hello indy.rb!</Message> </Response>
  9. Knock knock jokes • Predictable 4-step format • Twilio acts

    just like a browser sending a cookie back with each request • Cookie is associated with to/from phone numbers • Go ahead, try it! Text “knock knock” to 317-900-4588. • To the code!
  10. Text to give • Giving Tuesday 2015 • Two scenarios

    1. New donor 2. Existing donor • More code!