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.
Related
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.
I am working on application using RhoMobile 4.0. I want to know, how to connect to web service in rhomobile 4.0 without rhoconnect. Need to do http post and get.
Using RhoMobile, you can using the usual AJAX methods to reach a webservice to get or post data.
Additionally you can use the RhoMobile Network API from Ruby or from JavaScript to call a webservice, registering a callback.
For example in Ruby you can have this code in a controller:
def getData
#Perform an HTTP GET request.
getProps = Hash.new
getProps['url'] = "http://<my_url>/Json/Server/GetDada?username=admin&password=pass"
getProps['headers'] = {"Content-Type" => "application/json"}
Rho::Network.get(getProps, url_for(:action => :get_callback))
render :action => :transferring
end
def get_callback
if #params['status'] == "ok"
get_result = Rho::JSON.parse(#params['body'])
puts "**** Parsed it" #{#params['body']}"
# Do something with the data
end
end
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.
So I almost have a pingback sender ready for my rails app (people post links to content and donate to them). Almost.
I've borrowed heavily from the code here:
http://theadmin.org/articles/2007/12/04/mephisto-trackback-library/
I modified the slightly for my purposes:
require 'net/http'
require 'uri'
class Trackback
#data = { }
def initialize(link_id)
link = Link.find(link_id)
site = Link.website
if link.nil?
raise "Could not find link"
end
if link.created_at.nil?
raise "link not published"
end
#data = {
:title => link.name,
:excerpt => link.description,
:url => "http:://www.MyApp.org/links/#{link.to_param}/donations/new",
:blog_name => "My App"
}
end
def send(trackback_url)
u = URI.parse trackback_url
res = Net::HTTP.start(u.host, u.port) do |http|
http.post(u.request_uri, url_encode(#data), { 'Content-Type' => 'application/x-www-form-urlencoded; charset=utf-8' })
end
RAILS_DEFAULT_LOGGER.info "TRACKBACK: #{trackback_url} returned a response of #{res.code} (#{res.body})"
return res
end
private
def url_encode(data)
return data.map {|k,v| "#{k}=#{v}"}.join('&')
end
end
Looks like I'm sending links successfully to my wordpress blog but when I look at the link displayed on the trackback I get this: http://www.theurl.com/that/my/browser/iscurrentlypointing/at/http:://www.MyApp.org/links/#{link.to_param}/donations/new"
All I want is the second half of this long string. Don't know why the current location on my browser is sneaking in there.
I've tried this on two of my blogs so it doesn't seem to be problem related to my wordpress installation.
UPDATE: Okay this is a little odd: I checked the page source and it shows the correct link. When I click on it, however, I get directed to the weird link I mentioned above. Is this a Wordpress Issue?
Whoops! Looks like it was just a syntax error. A sneaky double colon
This line
url => "http:://www.MyApp.org/links/#{link.to_param}/donations/new"
Should of course be like this
url => "http://www.MyApp.org/links/#{link.to_param}/donations/new",
i am getting the following url information and need to parse it within rails. i already checked request and params but it is not there.
the "#" character seems to f*ck up things.
here's the url:
http://foo.bar.me/whoo#access_token=131268096888809%7C2.5BRBl_qt4xJ08n88ycbpZg__.3600.1276880400-100001151606930%7C0kJ1K-qoGBbDoGbLx6s4z5UEaxM.
thanks for any pointers.
You won't be able to access the part after the '#' character as the browser doesn't send it to the server. You can use it on the client side with javascript though.
It seems that you're trying to use the javascript based authentication which is not what you really want.
I didn't have any problems using this oauth2 library. Then you only need to check for params[:code] within your callback action.
UPDATE:
This is a simplified version of the code I used in my experiments with the new facebook graph API:
# Accessible as facebook_url:
# routes.rb: map.facebook '/facebook', :controller => 'facebook', :action => 'index'
def index
oauth2 = OAuth2::Client.new(FB_API_KEY, FB_API_SECRET, :site => 'https://graph.facebook.com')
if current_user.facebook_token
# The user is already authenticated
fb = OAuth2::AccessToken.new(oauth2, current_user.facebook_sid)
result = JSON.parse(fb.get('/me'))
elsif params[:code]
# Here we get the access token from facebook
fb = oauth2.web_server.get_access_token(params[:code], :redirect_uri => facebook_url)
result = JSON.parse(fb.get('/me'))
current_user.facebook_id = result["id"]
current_user.facebook_token = fb.token.to_s
current_user.save
else
# The user is visiting this page for the first time. We redirect him to facebook
redirect_to oauth2.web_server.authorize_url(:redirect_uri => facebook_url, :scope => 'read_stream,publish_stream,offline_access')
end
end
You don't really need anything else for it to work.