The only reference to SMS on the readme file is regarding sending SMS messages.
# send an sms
#client.account.sms.messages.create(
:from => '+14159341234',
:to => '+16105557069',
:body => 'Hey there!'
)
I'm wondering if the twilio-ruby gem provides visibility to SMS responses? I want to do some keyword response logic like the example they give here in PHP.
For others who may have this issue, I found this example application from this question to be very useful. What you need to do is:
Setup your Twilio Number SMS URL to Post to a certain controller in your application (e.g., myapp.com/twilio/process-sms)
Add a route to match that URL to the correct action in your Twilio controller
Write some simple logic like below to process responses/messages to your number according to your custom business logic
Respond, if necessary, using a .xml.erb file like the one below
class TwilioController < ApplicationController
def process_sms
#city = params[:FromCity].capitalize
#state = params[:FromState]
#from = params[:From]
u = User.find_by_phone_number(#from)
#user = u.name
b = params[:Body]
if b.downcase.include?("question")
#type = "Question"
#question = u.questions.build(:description => b)
#question.save!
render 'new_question.xml.erb', :content_type => 'text/xml'
elsif b.downcase.include?("contact")
#type = "Contact"
#contact = u.contacts.build(:name => b)
#contact.save!
render 'new_contact.xml.erb', :content_type => 'text/xml'
else
#type = "Not sure"
render 'not_sure.xml.erb', :content_type => 'text/xml'
end
The .build will create the object and the .save will save the object. Then you just return to Twilio the TWIML that you want to respond to the user. such as:
app/views/twilio/new_contact.xml.erb
<Response>
<Sms>We added a new contact for you.</Sms>
</Response>
When you get an SMS on your Twilio number, Twilio will make an HTTP request to your server. You can respond to the incoming SMS by responding to the request with XML, like this:
<Response>
<Sms>This is my response</Sms>
</Response>
You can either generate the XML response yourself, or the helper libraries contain methods that help you return XML to the client. I would suggest taking a look at the Twilio Ruby SMS quickstart for a simple example, and then going from there.
Related
I am integrating twilio click to call into my rails project.
Everything works fine however the url: in my twilio controller cannot be found on heroku. However, it can be found if you navigate to it in a browser. The phone dials but the voice says "Sorry a problem has occurred, good bye." If I change the url to an external xml file it works fine, just doesn't recognize this particular one. So I'm lead to believe that the controller etc works fine.
twillio_controller.rb
def call
#full_phone = current_user.phone
#partial_phone = #full_phone.last(-1)
#connected_number = "+61" + #partial_phone
#client = Twilio::REST::Client.new ##twilio_sid, ##twilio_token
# Connect an outbound call to the number submitted
#call = #client.calls.create(
:from => ##twilio_number,
:to => #connected_number,
:url => 'http://besttradies.herokuapp.com/mytradies/connect.xml', # Fetch instructions from this URL when the call connects
)
#msg = { :message => 'Phone call incoming!', :status => 'ok' }
end
def connect
# Our response to this request will be an XML document in the "TwiML"
# format. Our Ruby library provides a helper for generating one
# of these documents
response = Twilio::TwiML::Response.new do |r|
r.Say 'If this were a real click to call implementation, you would be connected to an agent at this point.', :voice => 'alice'
end
render text: response.text
end
The OP solved in the comments above:
Figured it out. Routes for connect needed to be POST and I also had to
add skip_before_action :verify_authenticity_token to the twilio
controller as it was behind membership doors.
I have a twilio app that is making phone calls. I put the guts of it in a Worker, and now cannot get the API to recognize the url I am passing as a valid url for my TwiML response. Code is below. Any ideas? Also note that I have tried both #{root_path}connect and #{root_url}connect
Worker
#numbers.each do |dial|
if (dial.phone_number =~ /[\(\)0-9\- \+\.]{10,11}/).nil?
raise Exception, "bad phone number"
end
call = client.account.calls.create(
:from => my_number,
:to => dial.phone_number,
:url => "#{root_path}connect"
)
controller
def connect
response = Twilio::TwiML::Response.new do |r|
r.Say 'The Time Has come to take over the world Pinky', :voice => 'alice'
end
render text: response.text
end
routes
root :to => 'call_logs#index'
resources :call_logs, only: [:create, :index] do
collection { post :call_score_range,:call_warrants_with_date_range,:connect }
end
Twilio developer evangelist here.
I think your problem is that the worker has no concept of Rails' routes. Routes are only available by default in controllers and views, so you are probably not passing a URL to Twilio.
I can't see where you call your worker from in the first place, but one idea would be to pass the URL you want to send to the API into the worker from where it is created in a controller.
I hope this helps. Please let me know if there's anything else I can do for you.
Hello I am designing an online application that uses mobile money as a means of payment. But I need assistance on how I can pass my order form data to the API of the mobile money service providers who complete the transaction.
Try httparty, post method.
An existed example could be
options = {:type => auto}
payload = options.to_json
query = {
:request_id => request_id,
......................
:user_id => user
}
response = post("/tbc/submit_job.json", :body => payload, :query => query)
You can use httparty to customize yours.
I am using Twilio in my rails 3.1.3 app and I have got everything basically set up, i.e. a controller for sms and xml builders for the views depending on the response. The only thing I can't figure out is how to keep track of the conversation. The Twilio docs are pretty bad for using anything other than PHP to do this. I have tried using the Rails session hash, session[:variable], but it doesn't seem to be saving the session, as I tried redirecting and printing it out and got nothing. Below is the code of the controller.
def receive
# Check for session variable and redirect if necessary
#sms_state = session[:sms_state]
if #sms_state == 'confirmation'
redirect_to 'confirm'
end
if condition
#sms_state = 'confirmation'
session[:sms_state] = #sms_state
render :action => "view.xml.builder", :layout => false
else
#sms_state = 'new_state'
session[:sms_state] = #sms_state
render :action => "error.xml.builder", :layout => false
end
end
# method that should be called after user deals with first part
def confirm
if condition
#sms_state = session[:sms_state] = nil
render :action => "confirm_view.xml.builder", :layout => false
else
#sms_state = 'confirmation'
session[:sms_state] = #sms_state
render :action => "error.xml.builder", :layout => false
end
end
I have now set up a database table to track the current conversation state depending on the phone number contacting my app. The only thing now that I need to do is set an expiration for this conversation, just like a session or cookie. I am not sure how to do this or if its even possible.
This depends on how you define "conversation", but in general, you are better off using some sort of persistance (would recommend database over a file), and build the structure in accordance with your definition of a conversation.
Suppose the conversation is defined as text messages between two 10-digit phone numbers without a time limit, you can setup a db with a sender and recipient attributes, so if you need to output something in a user interface, you can look for sender and recipient phone numbers, and display all messages coming to them or going from them.
SMS is different from a phone call, since you can set cookies for the session of a phone call. SMS is done when either delivered or sent. When you receive an SMS to a phone number or short code, Twilio will make a request to the URL you provided for SMS, and your app can then respond. If you receive another response, it's a brand new request, so you have to construct the notion of "conversation".
Does anybody have any example code on how this would work? Seems like it should be pretty straightforward, but the Twilio documentation is sparse for SMS/Rails.
I have a simple "post" model with a "body" column and "from" column. I just want to display the SMS messages in a list. The closest thing I got to work was something like this:
#posts_controller.rb
class PostsController < ApplicationController
def new
#post = Post.new(:body=>"?",:from=>"?")
#post.save
end
end
#twilio sms url: ...myappurl/posts/new
This creates a new post, but the 'from' and 'body' values are "?", obviously. How do I pass the Twilio SMS 'From' and 'Body' values into the rails controller?
Any ideas or a nudge in the right direction? Thanks!
Just solved it! It was as simple as I thought it was.
In my posts_controller.rb file:
def twilio_create
#post = Post.new(:body => params[:Body], :from => params[:From])
#post.save
end
This effectively pulls the Body and From params from Twilio. The same can be applied for other params (SmsMessageSid, AccountSid, etc).
You can see the full list of parameters sent with Twilio's request here.