From 9f03f99706f03b0cb74a3d67127c01769a71e193 Mon Sep 17 00:00:00 2001 From: Adam Folta Date: Mon, 9 Jan 2023 09:46:26 -0500 Subject: [PATCH] Add twilio integration for sms --- .gitignore | 2 ++ Gemfile | 3 +++ Gemfile.lock | 14 ++++++++++-- app/controllers/sms_controller.rb | 31 ++++++++++++++++++++++++++ config/credentials/development.yml.enc | 1 + config/routes.rb | 4 +++- 6 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 app/controllers/sms_controller.rb create mode 100644 config/credentials/development.yml.enc diff --git a/.gitignore b/.gitignore index 861425b..ac07c0f 100644 --- a/.gitignore +++ b/.gitignore @@ -28,3 +28,5 @@ # Ignore master key for decrypting credentials and more. /config/master.key + +/config/credentials/development.key diff --git a/Gemfile b/Gemfile index f0ff262..568456f 100644 --- a/Gemfile +++ b/Gemfile @@ -39,6 +39,9 @@ gem 'geocoder' # Debugging gem 'pry' +# SMS +gem 'twilio-ruby', '~> 5.74.0' + # Use Active Storage variants [https://guides.rubyonrails.org/active_storage_overview.html#transforming-images] # gem "image_processing", "~> 1.2" diff --git a/Gemfile.lock b/Gemfile.lock index 42b7f35..7d790cb 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -78,6 +78,10 @@ GEM irb (>= 1.5.0) reline (>= 0.3.1) erubi (1.12.0) + faraday (2.7.2) + faraday-net_http (>= 2.0, < 3.1) + ruby2_keywords (>= 0.0.4) + faraday-net_http (3.0.2) geocoder (1.8.1) globalid (1.0.0) activesupport (>= 5.0) @@ -86,7 +90,7 @@ GEM io-console (0.6.0) irb (1.6.2) reline (>= 0.3.0) - jwt (2.6.0) + jwt (2.5.0) loofah (2.19.1) crass (~> 1.0.2) nokogiri (>= 1.5.9) @@ -98,7 +102,7 @@ GEM marcel (1.0.2) method_source (1.0.0) mini_mime (1.1.2) - minitest (5.16.3) + minitest (5.17.0) msgpack (1.6.0) net-imap (0.3.4) date @@ -153,8 +157,13 @@ GEM rake (13.0.6) reline (0.3.2) io-console (~> 0.5) + ruby2_keywords (0.0.5) thor (1.2.1) timeout (0.3.1) + twilio-ruby (5.74.0) + faraday (>= 0.9, < 3.0) + jwt (>= 1.5, <= 2.5) + nokogiri (>= 1.6, < 2.0) tzinfo (2.0.5) concurrent-ruby (~> 1.0) websocket-driver (0.7.5) @@ -176,6 +185,7 @@ DEPENDENCIES puma (~> 5.0) rack-cors rails (~> 7.0.4) + twilio-ruby (~> 5.74.0) tzinfo-data RUBY VERSION diff --git a/app/controllers/sms_controller.rb b/app/controllers/sms_controller.rb new file mode 100644 index 0000000..611e87d --- /dev/null +++ b/app/controllers/sms_controller.rb @@ -0,0 +1,31 @@ +class SmsController < ApplicationController + require "twilio-ruby" + + def initialize + @client = Twilio::REST::Client.new account_sid, auth_token + end + + def create + @client.messages.create( + from: phone_number, + to: params[:phone_number], + body: params[:body], + ) + end + + def messages + @client.messages.list(limit: 20) + end + + def account_sid + Rails.application.credentials.twilio[:account_sid] + end + + def auth_token + Rails.application.credentials.twilio[:auth_token] + end + + def phone_number + Rails.application.credentials.twilio[:phone_number] + end +end diff --git a/config/credentials/development.yml.enc b/config/credentials/development.yml.enc new file mode 100644 index 0000000..14be68a --- /dev/null +++ b/config/credentials/development.yml.enc @@ -0,0 +1 @@ +I7i1TvjGGlQpAZxmSMUegPfoa1lLg0gQKUsV7ciW349OpK16A5gjkhNRbnQsiny5N8JwSZMj6Y5fLnlFihoY6s4ZQAubfcQ5CWkev4MR7gxasoJICLE1ZU43pg0+mMPLzmN7PrtLOYhevl2IXMJjlhhr1yGX3+iKFlMh8pvraLbgUl5nKlv09yulKaIJhulD--jc/oxRKc+M8tvwYJ--yrPr2uifKIhdCrD3xI/HeA== \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index f9a6387..8c74fd0 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -16,5 +16,7 @@ delete "/trips/:id" => "trips#destroy" - post "/restaurants-lookup" => "restaurants_lookup#new" + post "/restaurants-lookup" => "restaurants_lookup#create" + + post "/sms" => "sms#create" end