Twilo Unanswered calls Should trigger a URL - twilio

Is it possible that if a call is triggered using API to a Number and the user doesn't attend the call a url should be triggered to tell my application that call was unanswered.

While create a call request to twilio we can pass parameters to it:
status_callback and status_callback_method I am using it in rails
#client.account.calls.create(:from => 'xxxxxxx', :to => "xxxxx", :url =>"url", :method => :get, :status_callback=>"status_call_backurl", :status_callback_method=>:get)

Related

how to record inbound calls with twilio

Outbound calls in Twilio can be recorded by setting the TwiML passed to the client.calls.create() API method to have a <Conference> tag with the record attribute set to true. If recordingStatusCallback is set to a URL it'll post a notification to that URL when the recording is available.
How do you do this for inbound calls?
For my specific phone number, for Voice & Fax, I have, on twilio.com, "Accept Incoming" set to "Voice Calls", "Configure With" set to "TwiML App" and "TwiML App" set to my custom app. For that app I have the Voice Request URL set to a given URL.
When a call is incoming an HTTP POST is made to my TwiML App, which responds with an <Enqueue>. Later, a Conference Instruction Reservation is made in the client side javascript with ConferenceRecord set to true and ConferenceRecordingStatusCallback set to the same URL that we used with recordingStatusCallback for the outbound calls.
Here's what my "Conference Instruction Reservation" request payload looks like:
stdClass Object
(
[url] => https://taskrouter.twilio.com/v1/Workspaces/.../Tasks/.../Reservations/...
[method] => POST
[token] => ...
[params] => stdClass Object
(
[Instruction] => conference
[From] =>
[PostWorkActivitySid] =>
[Timeout] => 10
[To] =>
[EndConferenceOnExit] => true
[EndConferenceOnCustomerExit] => true
[ConferenceStatusCallback] => https://my.domain.tld/twilio/conference
[ConferenceStatusCallbackMethod] => POST
[ConferenceStatusCallbackEvent] => start,end,join,leave,mute,hold
[ConferenceRecord] => true
[ConferenceRecordingStatusCallback] => https://my.domain.tld/twilio/conference/recording
[ConferenceRecordingStatusCallbackMethod] => POST
[ConferenceRecordingStatusCallbackEvent] => completed
)
)
As a result of that Twilio calls the ConferenceStatusCallback URL multiple times but the ConferenceRecordingStatusCallback URL is never called.
Any ideas?
So idk why the "Conference Instruction Reservation" request approach wasn't working but calling client.calls.recordings.create() did the trick

Following a Typhoeus post in Rails/Sinatra

I have a scenario where I'm doing a post in Sinatra via Typhoeus in app.rb. It looks like this:
post "/send-data" do
...
request = Typhoeus::Request.new("http://localhost:4000/renders",
:method => :post,
:headers => { :Accept => "text/html" },
:followlocation => true,
:timeout => 100, # milliseconds
:params => params )
# Run the request via Hydra.
hydra = Typhoeus::Hydra.new
hydra.queue(request)
hydra.run
...
end
When I post to 'send-data' Typhoeus successfully does it's post and pushes the user to the view of the created record (http://localhost:4000/renders/34634646464), which is a rails app.
The problem is that the user is never redirected away from /send-data, so if you refresh the page it tries to do the post again. I guess this makes sense, but I really need the user to be redirected to the final (url) location of the record. In other words, the new record can be seen, but this method of redirecting does not actually move the user off of the sinatra app.
What would be the best way to handle this? The only one I can think of off the top of my head is to not use 'followlocation', but rather have the /send-data controller action do the redirect after getting the response location fron Typhoeus.
I tried my suggestion and it works... and does not look too bad.
request = Typhoeus::Request.new("http://localhost:4000/renders.json",
:method => :post,
:headers => { :Accept => "json" },
:timeout => 100, # milliseconds
:params => params )
hydra = Typhoeus::Hydra.new
hydra.queue(request)
hydra.run
response = request.response
redirect response.headers_hash['Location']
I did have to make a change on my rails server. The rails create action responds with json and 'Location' is it's return value. 'Location'is the location of where the newly created record resides. Then I just do a Sinatra redirect which will redirect to the new record on the rails app.

How to set status to active in rails3

I am using rails3 and I have a user model. This model has a status column. I am showing admin following table
Mary approve reject
John approve reject
Both approve and reject are links. Since clicking on approve will approve user's record it should not be a get request. It should be a post request. I was thinking to achieve a post request I should make clicking on approve or reject an ajax call.
In the ajax call I would make post call instead of get.
Is this a good strategy? Anyone has any better suggestion.
Thanks
Just pass :method => 'post' to your link_to call:
<%= link_to 'approve', approve_user_path(user), :method => 'post' %>
http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to
You're right, it shouldn't be a get request. Actually, I think it's neither a post request, because you already have the record and want to change it.
You could just pass :method => :put to link_to. Rails will make a JS handler and when the link is clicked, it will create an invisible form with action=PUT and submit it.
BUT, AJAX is a nice thing too and it's just as hard as setting the method: :remote => true
HTTP POST is used for create, and HTTP PUT is used for update. As such, you should be using doing a PUT (add :method => 'put' to your link_to) since you are updating the user record. Here's more details: http://guides.rubyonrails.org/routing.html#crud-verbs-and-actions

how can I add a new action to a controller?

I used the following in routes to add a new action to my Email controller:
map.resources :emails, :member => { :newfwd => :put}
The expected result was that newfwd_email_path(:id => 1) would generate the following urL: emails/1/newfwd
It does. But I get an error, it treats '1' as an action and 'newfwd' as an id. I want '1' to be interpreted as the id for emails, upon which the newfwd action acts.
I'm not sure what I'm doing wrong. (Note: I am using Rails 2.3.8)
Try
link_to newfwd_email_path(1), :method => :put
:id => 1 is as good as 1 ;)
you do not need to pass a hash to the newfwd_email_path method. Try
newfwd_email_path(1)
EDIT: you also need to use :method => :put to ensure that the PUT verb is used when request is received on the server and routing comes into effect.

Is it possible to use rack-rewrite for rewriting just POST requests?

That's it. I need to redirect just the POST requests. Something like:
rewrite /.*/, '/universal_POST_handler', :if => (something_cool_goes_here)
Is it possible?
The purpose of a rewriter in an application that has routing is to rewrite legacy URLs to more current URLs. Legacy URLs are URLs that were supported and that external users have come to depend on but that are no longer supported because the architecture of the application has changed.
You should use the router instead.
post '*path' => 'actions#universal',
:constraints => FancyConstraint.new
From the README:
Using the :method option you can restrict the matching of a rule by the HTTP method of a given request.
redirect GETs one way
r301 "/players", "/current_players", :method => :get
and redirect POSTs another way
r302 "/players", "/no_longer_available.html?message=No&longer&supported", :method => :post

Resources