twilio python outbound dial - twilio

I am trying to use my laptop (running Twilio) to dial a land line using python 3 and have have a voice conversation. This is the simplest use case.
Here is the python code I am running on my laptop.
def test_voice_command_inline(self):
'''calls landline from laptop via twilio'''
twilio_client = twilio.rest.Client(self.twilio_account_sid,
self.twilio_auth_token)
vps_url = self.get_vps_url()
twilio_voice_call = twilio_client.calls.create(
from_=self.twilio_phone_number,
to=self.land_line_number,
url=vps_url
)
When I run this code, my land line rings, and I answer it. The call is connected. Next I get prompted by Twilio to press a key to "execute my code" (aka the webhook)
Since I already have a connected call, I don't need a webhook. I just want to start talking. However, the Twilio rest api requires me to put in a webhook url that returns TwiML to direct the call flow. And when the TwiML is finished processing, the call is hung up. I don't need a webhook, yet I am required to have one.
I tried various things in the webhook code in an attempt to let the already connected call continue. I tried returning blank TwiML from my webhook, which causes an immediate hangup. I also tried dialing the land line number from inside the webhook. Since the rest api already connected the call, trying to re-dial the number from the webhook caused a busy status (Twilio console). Looking at all the TwiML verbs I can use in the webhook, none look appropriate for my needs.
I must be missing something simple. Why can't I use the code above to make a voice call between my laptop and my land line (or any other phone)?

Twilio developer evangelist here.
When you make an outbound call using the REST API, Twilio is setting up a call between our server and the to number. The from number that you supply is just the callerId for the call.
In order to connect the call you make with the REST API to another phone, you need to return TwiML from your twiml_url that dials another number. Currently your TwiML is just an empty <Response> but it should be something like this:
<Response>
<Dial><Number>OTHER_NUMBER</Number></Dial>
</Response>
This can be written out by Python code too, if you want it to be dynamic. That's where you use the VoiceResponse that you mention. The important thing is that the second leg of the call that you are making should be controlled by TwiML returned from your URL.
Let me know if that helps at all.

Related

Is there a way to start a Twilio phone conference via an api call

I played around a lot now and still can't figure out if there is a way of starting a conference via the API.
What do I want to achieve:
I would like to set up a conference, keep all participants still listening to music and than start that conference on a button press.
After reading the documentation about the conference API, I saw you can update the conference object status:
https://www.twilio.com/docs/voice/api/conference-resource?code-sample=code-update-a-conference-to-end-it&code-language=Ruby&code-sdk-version=5.x
The update to end the conference works fine (the example):
conference = #client.conferences('CFXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
.update(status: 'completed')
But as soon as I try to use .update(status: 'in-progress') to start the conference i get:
in-progress is not a valid choice
https://www.twilio.com/docs/errors/20001
The documentation sounds like you are able to do it:
The new status of the resource. Can be: Can be: init, in-progress, or completed. Specifying completed will end the conference and hang up all participants
Do I miss something?
Yes there are more than 2 calls connected, both of them with 'startConferenceOnEnter' set to false.
Twilio developer evangelist here.
You can use the REST API to generate a call to your client.
The URL you pass to this call should point to an endpoint on your server that will return the TwiML to start the conference:
<Response>
<Dial><Conference>{insert Conference room name}</Conference></Dial>
<Response>
During this request where you return the conference call TwiML, you can also kick off a new request to start a call to the phone number you want to dial using Twilio's REST API as mentioned here using curl.
Alternatively, you could maybe do it with Twilio Studio like this.
Let me know if this helps at all!

Not able to do simple twilio call

I am not able to do simple 2 way audio call between 2 numbers using twilio.
I have attached my python (Django) code. when I run the code, call Get connected and but after accepting call it get's disconnected immediately. also, I am not able listen to anything on Laptop.
I have joined the code and twiml from twilio.
def call_m(request):
account_sid = os.environ['TWILIO_ACCOUNT_SID']
auth_token = os.environ['TWILIO_AUTH_TOKEN']
client = Client(account_sid, auth_token)
call = client.calls.create(
url='twiml_url',
to='+91985XXXXXXX',
from_='+132YYYYyYYY',
)
print("sid ",call.sid)
return render(request, 'calls/incall.html')
Here's my twiml.
<?xml version="1.0" encoding="UTF-8"?>
<Response>
</Response>
if i changed My twiml to this
<Response>
<Dial>+9198xxxxxxxx</Dial>
</Response>
then after accepting call from "To" number, It say's number is busy please call later.
I think you may have some misconceptions about how Twilio works when making calls.
When you generate a call with Twilio using the REST API that creates a call leg between the to number and Twilio (not between your laptop and the phone).
When that call is answered on the end of the to number, Twilio then makes an HTTP request to the url that you passed to the API. That URL should respond with TwiML to tell Twilio what to do with the call next.
Now, when your application responded with an empty TwiML response (<Response></Response>) that tells Twilio that there is nothing left to do in this call, so Twilio hangs up.
When your application responded with a <Dial>, like this:
<Response>
<Dial>+9198xxxxxxxx</Dial>
</Response>
my guess is that the number in the dial was the same number that you used in the to parameter to make the call. In this case, Twilio placed a call to your number in response to the REST API request and then placed another call to your number as directed by the TwiML <Dial> and that second call found that your number was busy because you are already on the line with Twilio.
If you used a different number in the <Dial> then Twilio would place a call to that number and when they answered the phone you would be connected to them.
If you want to connect two callers with their own phones, then using <Dial> like this is the way forward.
If you want to place a call from your laptop to another device, then you will need a bit more than just the REST API. In this instance you need to build an application using the Twilio Voice SDK which allows you to make calls from a web browser or iOS or Android application. If this is of interest to you then I recommend you check out the getting started guide for the JavaScript voice SDK.
The call needs TWiML to stay active, so that explains why it ends the call when you are returning an empty TwiML reponse. You must keep feeding it TwiML to keep the call up.
Specific to calling the number, there could be an issue dialing that number that Twilio support could investigate, if geographic permissions allow international calls to that destination.

Forwarding call recordings in Twilio for Zoho

I use Twilio and Zoho Phonebridge for inbound and outbound calling to customers. I use Zoho's built in IVR which works well, but has gaps. The main gap is when I get a voicemail, I need to login to Twilio to get it and that's a time sink. I would like Twilio to email (or text, for all I care) those messages to me, transcribed or not. Problem is, I cannot modify the code provided by Zoho, so I need to have the function carry out in parallel. I'm not sure where to start. I can do code snippets easily enough, but I seems I need to replace the "Voice Configuration - Request URL", which kills the IVR.
Any help?

Connect two persons through Twilio client

I have a task with this description:
Implement the call button:
When call icon is clicked, first Twilio calls the phone number #1 (Admin)
Once admin picked up the phone, the number #2 (provider) is called.
Both connected.
At this moment I figured out how to call through browser to phone numbers (like Admin can call to the provider in browser).
But I can't find any information, how to connect people through Twilio accordingly the task.
Is there any way to implement this solution?
I do not completely understand if you want to click a phone number on the website or if you want to completely connect two phone numbers automatically.
Scenario 1: User calls a number of your Twilio account
You setup a callback URL for that number and setup a web endpoint which generates a response similar to the following (XML, TwiML):
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Dial callerId="${callerId}">${targetNumber}</Dial>
</Response>
In my example I use TypeScript/JavaScript templating to replace the callerId and targetNumber variables according to our systems logic.
Secenario 2: You want to connect two phone numbers via Twilio
In this scenario your software first makes sure that Twilio calls your Admin. This can be done via a REST call or Twilio's API. There are so many options depending on which programming language you use and if you want to use a library from Twilio. But the basic idea is documented here:
https://www.twilio.com/docs/voice/make-calls
And you would always end up making a REST call against /2010-04-01/Accounts/{AccountSid}/Calls to initiate the call.
In the request you again specify a URL where Twilio then can read back XML / TwiML to understand what it should do with the call one it is connected. And again here you can use almost the same TwiML as above:
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Say>Just a second you are going to be connected to your provider.</Say>
<Dial callerId="${callerId}">${targetNumber}</Dial>
</Response>
In the above example I also added a <Say> statement such that the administrator knows, that they are going to be connected and need to be patient until the call is finally connected.
Important Notes:
In our application scenarios we are attempting to hide the phone numbers of the connected parties. The purpose is that the caller which dials into our system should have the opportunity to remain completely anonymous if desired. So we specify with callerId which caller id we want to send with outbound phone call. Bear in mind that this must be a phone number which you do own (is a number rented via Twilio or is a phone number you registered with Twilio).

Twilio: Dial out from conference

I have an incoming phone call that is waiting in a conference. While they are waiting, our application looks for an appropriate rep based on some business logic to connect to them too. If we are unable to find a rep, we would like to forward the call to a general number. It's possible that this number will dial to voicemail.
Normally, I would dial out, and when the phone is picked up I would connect the call to the conference. The issue with that workflow is the person in the conference will not hear the call being dialed out, and it also presents challenges with voicemails (I need the inbound call to know if they are leaving a voicemail).
How can I either remove the inbound caller from the conference and dial out to the generic number, or call out from the conference?
I found from here: https://twilio.radicalskills.com/projects/conference-calling-voice/3.html (under "modify live calls") that using the original call Sid you should be able to remove them and send new twiml instructions. Whoopie!
edit: I was asked to expand on my answer. Here is what I'm doing:
The caller is already waiting in a conference, I need to "remove" them and dial out.
Using the Rest API I send a Redirect to the original call sid of the call that was put into a conference.
The Redirect url returns a twiml dial app to the number I want the original call to be connected to.
That's it :)

Resources