Skip to content

Instantly share code, notes, and snippets.

@kylefritz
Last active August 29, 2015 14:14
Show Gist options
  • Select an option

  • Save kylefritz/89b35c841da280dc652a to your computer and use it in GitHub Desktop.

Select an option

Save kylefritz/89b35c841da280dc652a to your computer and use it in GitHub Desktop.
Ruby Basics, Methods, Controller actions
class MeController < ApplicationController
def show
if session[:messages].blank?
session[:messages] = []
end
if session[:message_count].blank?
session[:message_count] = 0
end
# byebug
if params[:message].present?
# must have in gemfile & bundle: gem 'twilio-ruby', '~> 3.12'
client = Twilio::REST::Client.new(
'AXXXXXXX', # sid
'cXXXXXXX') # auth token
client.messages.create(
from: '+15555555555', # put your twilio outgoing number here
to: "+1#{params[:phone]}", # assumes 10 digits, no dashes or spaces
body: params[:message]
)
puts "sent message #{params[:message]} to #{params[:phone]}"
session[:message_count] = session[:message_count] + 1
session[:messages].push(params[:message])
end
end
end
<% if params[:message].present? %>
<h5>Sent message: <%= params[:message] %></h5>
<% end %>
<h2>Send a message</h2>
<form>
<p><input type="text" name="phone" placeholder="e.g. 5551231235" /></p>
<p><textarea name="message" rows="8" cols="40"></textarea></p>
<!--
<select name="carrier">
<option>att</option>
<option>sprint</option>
<option>verizon</option>
<option>tmobile</option>
</select>
-->
<p><input type="submit" value="Send!"></p>
</form>
<p>Messages Sent: <%= session[:message_count] %></p>
<ul>
<% session[:messages].each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment