Not able to do simple twilio call - twilio

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.

Related

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).

voice call using twilio to my personal number

I am creating Swift app from which user can able to do call from Twilio number to personal number and for that i have implement code as below which i have referred from Twilio documentation here is the reference link
This is the code which i have used in my app
but now issue is that call is receiving to my personal number but i am to able to talk with another person who is receiving call and when call is received from personal number than i can able to here TWIML Bin content which i have added as.XML format as below
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Say>This is the demo for Call And SMS</Say>
<Dial>+15005550006</Dial>
</Response>
so Outgoing call is from +15005550006 and receiving from my personal number then only i can here is that tag text i am not able to talk like we talk on normal voice call through network carrier so can anyone have idea how to connect call when i receive call from my personal number
if any one have idea that how to connect call from twillio number to personal number using Swift than kindly tell so i can able to solve this
This Code which you have used to make the call is only made for MAKING the call and NOT handling the actual communication after the call has been connected.
To be able to have a proper VoIP or conventional call using your twilio purchased phone number to your courier based phone number, you have to first make the swift app using the TwilioVoiceSDK, the code you have mentioned was only doing an HTTP request with parameters to invoke an outgoing call, but with twilioVoiceSDK you will not only invoke the code but will also be able to handle the actual communications after the call gets connected.
TwilioVoiceSDK look into this to get further explanation of the whole system.
Secondly you will also need to code in TwiML bin or twilio cloud to handle the outgoing calls to actual phone number as well, which you can also check from the above link.
Coming back to this code
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Say>This is the demo for Call And SMS</Say>
<Dial>+15005550006</Dial>
</Response>
This will convert the text to speech and the dials the specified number, The caller will hear the text and the call will initiate and thats it.
This is the demo for Call And SMS

Twilio Stream only when the call has been received

I am using twiml bin attached to one of my twilio numbers.
When a person calls that number it should be able to dial multiple numbers. I want to stream the call only when the call has been picked up. There can be multiple ongoing streams associated with a twilio number.
<Response>
<Start>
<Stream track="both_tracks" url="wss://XXXX.ngrok.io /twilio-stream">
</Stream>
</Start>
<Dial answerOnBridge="true" >
<Number>+91XXXXXXXXX</Number>
<Number>+91XXXXXXXXX</Number>
</Dial>
</Response>
The above twiml starts the stream as soon as the number is dialed. I want it to start the stream only when the call is received at the other end.
Unfortunately, there isn't a way to do this today, without REST API endpoint control.
You can use the REST API Calls resource to initiate calls intead. The approach you are taking doesn't work very well when the dialed parties are mobile end-points in poor coverage areas and/or devices with voicemail enabled.

twilio python outbound dial

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.

Forwarding to multiple destinations with Twilio functions

I am new to twilio. I am trying to setup a phone number that rings multiple destinations.
I followed this blog and set up a twiML script like so.
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Dial>
<Number>+1-777-777-7777</Number>
<Sip>sip:1777xxxxxxxxxx#in.callcentric.com;region=us2</Sip>
<Sip>sip:1777xxxxxxxxxx#in.callcentric.com;region=us2</Sip>
</Dial>
</Response>
However as reported by other users in the comments this does not seem to work for scenarios where the destinations are a mix of sip and numbers. In my case only the phone number rings. The sip destinations don't receive the call at all.
Is there a way to make this work? either with bins or the new twilio functions?
Twilio developer evangelist here.
It is not possible to use multiple <Sip> nouns in a <Dial>. From the documentation:
Currently, only one <Sip> noun may be specified per <Dial>, and the INVITE message may be sent to only one SIP endpoint. Also, you cannot add any other nouns (eg <Number>, <Client>) in the same <Dial> as the SIP. If you want to use another noun, set up a callback on the <Dial> to use alternate methods.
You could, instead, add the incoming caller to a queue with <Enqueue> and dial each of the numbers/SIP addresses using the REST API. Then, when one connects, connect it to the call in the queue and disconnect the other calls. You'd need to keep a reference to the original call SID so that could connect directly to it and to each of the generated call SIDs so that you can terminate them with the REST API too.
Let me know if that helps at all.

Resources