0

This is my current code. I do not understand how to make twilio call from "FROM" number first and then once connected, to call the "TO" number. Any suggestions?

# set up a client to talk to the Twilio REST API
@client = Twilio::REST::Client.new(account_sid, auth_token)

call = @client.calls.create(
    to: options[:to],
    from: options[:from]
)
user2012677
  • 5,465
  • 6
  • 51
  • 113

2 Answers2

1

Twilio developer evangelist here.

When you create a call using the Twilio REST API it will create a call between Twilio and the To number. The From number in this case must either be a Twilio number or a number you have verified with Twilio.

Now, as an example, you can use that call to say or play messages to the call recipient. We do this by providing TwiML, or by providing a URL that will respond to a webhook and return TwiML. TwiML is a subset of XML that contains instructions for what Twilio should do with a call. For example:

@client = Twilio::REST::Client.new(account_sid, auth_token)

call = @client.calls.create(
    to: options[:to],
    from: TWILIO_NUMBER,
    twiml: "<Response><Say>Ahoy there!</Say></Response>"
)

In this case the entire call is dealt with in only one leg, between Twilio and the call recipient.

In order to connect the call recipient to another number you need to create a second call leg. You can do this with the <Dial> TwiML verb like this:

@client = Twilio::REST::Client.new(account_sid, auth_token)

call = @client.calls.create(
    to: options[:to],
    from: TWILIO_NUMBER,
    twiml: "<Response><Dial>#{options[:from]}</Dial></Response>"
)

Let me know if that helps at all.

philnash
  • 70,667
  • 10
  • 60
  • 88
  • This is super useful and solves 80+% of my similar problem very well, thank you! What happens if someone tries to run the second example twice in rapid succession, while the `TWILIO_NUMBER` is still on the call? Will a third and fourth number be added to that call, or a new one created, or will it fail? And if the first, is there a way to have your `TWILIO_NUMBER` drop off the call after the second number picks up, so you can safely use it for the next call? Thanks in advance! – icodestuff Mar 10 '23 at 09:01
  • 1
    Twilio numbers can handle more than one call at a time. If you ran the second example with different to numbers, they’d all be in different calls. – philnash Mar 10 '23 at 11:05
0

Basically, you need to point to Twilio Markup Language (TwiML). You can use the twiml parameter or a url parameter which points to a TwiML source

Example using TwiML Parameter:

Pass TwiML with Call Initiation Requests

Ruby Example

The TwiML to "Forward" a call, which is what you are trying to do can be found here:

Setting Up Call Forwarding

<Response> <Dial> +13105555555 </Dial> </Response>

Alan
  • 10,465
  • 2
  • 8
  • 9