How to reference "from" field in TWIML to make a simple call b/w two phones - ruby

Trying to follow this tutorial:
https://www.twilio.com/docs/quickstart/ruby/rest/call-request
This code only dials the to number for me:
#call = #client.calls.create(
:from => '+14159341234', # From your Twilio number
:to => '+18004567890', # To any number
# Fetch instructions from this URL when the call connects
:url => 'http://twimlets.com/holdmusic?Bucket=com.twilio.music.ambient'
)
For me, this code never dials the from phone. Just the "to:" number gets called, when answered, it plays music. The "from" number doesn't never rings. I'm guessing I have to write TWIML to dial the first number (to), but I don't see any reference to the variable "to" in TWIML, is there a sample twiml that will simply connect two phones?

You're dealing with three numbers in this picture. One is your Twilio number from where the call is made '+14159341234', then, you have the two numbers you'd like to call and connect.
Let's be clear, you won't hear your Twilio number ring, it's at Twilio and it's kind of virtual.
The code you have so far, when you run it, uses your Twilio number :from => '+14159341234' to make a call :to => '+18004567890'.
So, what happens when people at :to => '+18004567890' answer? Twilio's system (the platform) makes a request to :url => 'http://twimlets.com/holdmusic?Bucket=com.twilio.music.ambient' from where is served some XML, some TWIML. You can actually see what is served if you go with your browser to http://twimlets.com/holdmusic?Bucket=com.twilio.music.ambient
To do what you want to do, to dial another number instead of playing music, you need to change that url to some place from where you serve this kind of TWIML:
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Say>Hello. Please wait.</Say>
<Dial>+12223334444</Dial>
</Response>
If you don't have your public server from where you can serve XML, you can use a native TwiML bin, that you create in your Twilio account console here: https://www.twilio.com/console/runtime/twiml-bins .
Once you create your TwiML bin, under properties for the bin, you'll have a URL to put in place of http://twimlets.com/holdmusic?Bucket=com.twilio.music.ambient, something like https://handler.twilio.com/twiml/EH7e58b64f8488ff8c022bf83c910fb49b.
P.S. You might want to also google twilio click to call and/or twilio conference as other way to connect two phones.

Related

List Payouts of stripe Account

According to Stripe Payout API
I need to retrieve all payouts of a destination, So i made this request:
def all_payouts(external_account)
return Stripe::Payout.list(
:destination => external_account
)
end
from the API I tried to send the external_account(like "ba_XXXX") but it keeps returning
No such external account: ba_XXXX while testing it using Postman, I checked the external_account but it is exists on stripe.
Any help?
Since this is a common enough question and easy to miss that you solved it in the comments, here's the correct code to do this.
When trying to list payouts on a connected account, you have to make the API request authenticating as this account. You would pass the platform's Secret API key along with the connected account's id in the Stripe-Account header. In Ruby, the code would look like this:
payouts = Stripe::Payout.list(
{:destination => external_account},
{:stripe_account => "acct_XXXXXX"},
)

Change "from" field of SMS text message for recipient

I want the recipient of my SMS text message to see the name of my business, rather than the phone number. How is this possible? I am currently using Twilio, but I am open to suggestions involving other services.
For those of you who have looked at #philnash s answer and still having problems, please read this. I'm using the twilio-ruby gem, and am in the UK.
I see a lot of places state that:
all you need to do is replace the from parameter.
This is not true!
Step 1
Contact Twilio and have them enabled it on your account.
Step 2
The second step is to make sure that to number you are using has the correct country code, 0779XXXXXXX and 779XXXXXXX are enough to send a text message when specifying a from number that has been previously purchased. The purchased number has a country of origin and therefore Twilio know to prefix it with the correct country code. However, an alphanumeric sender has no from number, and therefore no country of origin.
Using 0779XXXXXXX as the To param. Returns the error:
The 'To' number 0779XXXXXXX is not a valid phone number.
While technically correct, it isn't incorrect, it's just missing the country code.
Using 779XXXXXXX as the To param. Returns this error: The 'To' phone number: +1779XXXXXXX, is not currently reachable using the 'From' phone number: Foo via SMS. +1 is the USA phone prefix. The USA doesn't have an option to send as alphanumeric. This is how I stumbled onto the fix.
TLDR;
1. Contact Twilio to get them to enable it on your account
2. Make sure you prefix with the country code.
I have found three services (not Twilio) that appear to address the question:
Nexmo
Plivo
smsmessagesender
The above services support dynamic alphanumeric sender ID for outbound SMS, so you should be able to send a combination of 11 alpha-numeric characters as the sender ID of your message. Note the emphasis on outbound SMS. It appears that receiving incoming SMS on an alphanumeric sender ID is not possible.
If anyone has more experience with these services, please share.
Edit: Note that the availability of these services also depends on the laws of the jurisdiction where the services are to be used.
Twilio developer evangelist here.
I know this is a good few months since you asked this, but I wanted to update the possible answers for completeness. When you asked this in November 2014 it was not possible to send SMS messages from Twilio using an alphanumeric sender ID.
However, since May 2015 it has been possible to do so. All you need to do is replace the phone number as the From parameter of the API call to send a message with the text you want the sender ID to be. This currently works when sending SMS messages to 145 countries.
If I was doing this in Ruby, it would look like:
account_sid = YOUR_TWILIO_ACCOUNT_SID
auth_token = YOUR_TWILIO_AUTH_TOKEN
recipient_phone_number = RECIPIENT_PHONE_NUMBER
client = Twilio::REST::Client.new(account_sid, auth_token)
client.messages.create(
from: "ALPHANUMERICID",
to: recipient_phone_number,
body: "Hello World!"
)
Edit
Please note, that to use alphanumeric sender IDs on your Twilio account you need to have your account enabled. To do so, please contact twilio support with your account SID to request access.
As FullStack said, you can consider using another SMS API for this.
Nexmo, where I work, offers a high quality messaging service including the support of dynamic sender id. You can use any alphanumeric string to describe your application or brand where supported by carriers (where there are no restrictions on alphanumeric sender ids).
Some countries, such as the United States, don't allow alphanumeric sender ids. Other countries allow it, but have special restrictions that we take into account & work around to ensure maximum delivery.
You can find the full list of alpha-numeric sender ID compatible countries, along with carrier restrictions, here.
<?php
$url = 'https://rest.nexmo.com/sms/json?' . http_build_query([
'api_key' => API_KEY,
'api_secret' => API_SECRET,
'to' => YOUR_NUMBER,
'from' => "AppName",
'text' => 'Hello'
]);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);

I don't understand the Twilio URL Parameter on Voice

I understand the purpose of this for more complex functionality, but I'm trying to make a simple app that does nothing more than forwards to a phone number. I don't want to hard code the forward via a Twimlet because I want to build a web app that dynamically allows me to assign the numbers. Can someone tell me what I'd put for URL parameter in this situation?
get '/forward-call' do
#client = Twilio::REST::Client.new
#call = #client.account.calls.create(
{:to => "+15127778888",
:from => "+15122222222",
:url => "What in the foo should I put here?"})
end
Twilio developer evangelist here.
Does your /forward-call endpoint get called when someone makes a call to your Twilio number?
If so, you don't want to be calling the REST API right now, you want to respond to the webhook with some TwiML. In your case, you'd want something like this:
get '/forward-call' do
content_type 'text/xml'
"<Response>
<Dial><Number>#{NUMBER_TO_FORWARD_TO}</Number></Dial>
</Response>"
end
Does that help at all? Let me know if I can help out any more at all.

twilio, sinatra ruby force hangup and then callback

I want to call my Twilio number which hangs up immediately and then calls me back. This is similar (but not identical) to a previous question of mine.
The problem lies with forcing Twilio to hang up.
The ruby code is:
get '/callback' do
to = params['From']
from = 'my Twilio number'
"<Response><Hangup/></Response>"
sleep 5
# set up a client to talk to the Twilio REST API
#client = Twilio::REST::Client.new account_sid, auth_token
#call = #client.account.calls.create(
:from => from, # From your Twilio number
:to => to, # To any number
# Fetch instructions from this URL when the call connects
:url => 'https://dl.dropboxusercontent.com/u/85088004/twilio/twilio.xml'
)
end
This produces a message: "We are sorry but a system error has occurred". The problem lies with the hangup instruction. I have tried as above and also
<Response><Hangup/></Response> #without enclosing double or single quotes
and
Twilio::TwiML::Response.new do |r|
r.Hangup
end.text
Neither produces the desired result of hangup.
What's wrong?
Many thanks in advance!
Definitely go with #Kevin's advice and get in touch with Twilio support. But one comment I would like to add. At the end of your /callback action, the last value is the #call variable you are assigning when you create a call, Ruby will be trying to return this value as the result of the HTTP request.
Ruby returns the last value evaluated at the end of a method. In this case, that value is what Sinatra responds to the HTTP Get request with by default.
Judging by the literal string you have <Response><Hangup/></Response>, I'm guessing you are not using a view, and expect that to be the result of the get request. You should place that at the very end of your method as below. I've also added a content_type which just sets the response header to say the response is XML.
get '/callback' do
to = params['From']
from = 'my Twilio number'
# set up a client to talk to the Twilio REST API
#client = Twilio::REST::Client.new account_sid, auth_token
#call = #client.account.calls.create(
:from => from, # From your Twilio number
:to => to, # To any number
# Fetch instructions from this URL when the call connects
:url => 'https://dl.dropboxusercontent.com/u/85088004/twilio/twilio.xml')
#ruby returns the last value in a method:
content_type 'text/xml'
"<Response><Hangup/></Response>"
end
Make sure that you have your Twilio Voice Callback set to a Get request as well (the default is post).
I've also removed the sleep 5 that you had in there. I can see where you are going with it. Respond with the hangup TwiML, wait a few seconds, then make a new call. Sadly, Sinatra doesn't quite work that way. It will not respond to the HTTP request until the end of the method. So you're just going to have it sit and wait for 5 seconds with no reason.
This does pose a bit of an issue, as you tell Twilio to make an outbound call to the number that you are about to hangup on, so you basically have a race condition. Does the call disconnect first, or does the outbound start ringing to a busy number.
There are a number of ways of doing this, but I'd suggest a thread. It's a little tricky, but this SO answer goes into some detail.
You should check your Twilio Debugger at twilio.com/user/account/debugger. This will give more information about the failure.

How to get notified when SMS Status changes from 'Queued' to 'Sent'?

Hello,
I am trying to learn Twilio API.
When I [send SMS through php][1] script.. twilio returns a response object with status = 'queued'. Now I want to get notified when the status changes to 'sent'. Is this possible with Twilio??? and if yes then could any body advise me on how to implement his.
And how to add 'StatusCallback' url
$sms = $client->account->sms_messages->create(
// the number we are sending from, must be a valid Twilio number
"000-000-0000",
// the number we are sending to - Any phone number
"0000000000",
// the sms body
"Hey Friend, Monkey Party at 6PM. Bring Bananas!"
);
You're on the right track looking to the StatusCallback. When using the Twilio PHP Library any optional parameters can be set using an array as the last argument.
<?php
$sms = $client->account->sms_messages->create(
"1235551234",
"1235554321",
"Hey Friend, Monkey Party at 6PM. Bring Bananas!",
array('StatusCallback' => 'http://example.com/sms/status.php')
);
When the message is sent (or if it fails) the data will be passed to the StatusCallback url.

Resources