I was able to implement basic Voice Conferencing but I feel my implementation may be lacking.
client = Client('ACxxxxxxxx', '34xxxxxxxxx')
#app.route('/', methods=["GET", "POST"])
def home():
form = ConferenceList() #form made using flask-wtf
if form.validate_on_submit():
contact_1 = form.data['contact_1'] #callee1
contact_2 = form.data['contact_2'] #callee2
from_ = form.data['from_'] #caller
response = VoiceResponse()
with Dial() as dial:
if from_ == MODERATOR:
dial.conference(
'Conf',
start_conference_on_enter=True,
end_conference_on_exit=True
)
else:
dial.conference('Conf', start_conference_on_enter=False)
response.append(dial)
'''here I feel could be a bottleneck'''
add_user(contact_1, conference_name='Conf', label='laed#1')
add_user(contact_2, conference_name='Conf', label="consumer")
return Response(str(response), 'text/xml')
return render_template('hello.html', form=form)
def add_user(contact, conference_name, label):
participant = client.conferences(conference_name).\
participants.create(
label=label, #label for participant
beep='onEnter',
record=True,
from_='from_', #same as above
to=str(contact)
)
if __name__ == '__main__':
app.run(debug=True, port=8000)
Basically the submit button triggers the / endpoint and the conference starts.
I feel there could be an issue with this implementation as I plan on cleaning it up and pushing to production (salesperson can make a conference call to leads on the app). Is there something I could have done better?
The voice(one client-one client) utilizes the Twilio Voice SDK, it there a way I could tweak it for conferencing?
You only need to respond with TwiML to a webhook from Twilio. In this case it appears that you are responding with TwiML to your application's front end when a submit button is pressed.
So, you can drop all the TwiML:
#app.route('/', methods=["GET", "POST"])
def home():
form = ConferenceList() #form made using flask-wtf
if form.validate_on_submit():
contact_1 = form.data['contact_1'] #callee1
contact_2 = form.data['contact_2'] #callee2
'''here I feel could be a bottleneck'''
add_user(contact_1, conference_name='Conf', label='laed#1')
add_user(contact_2, conference_name='Conf', label="consumer")
return "whatever"
Since the settings you are trying to apply in the TwiML do not apply to the participants joining the conference, you need to adjust your add_user function to use them. In this case, the startConferenceOnEnter and endConferenceOnExit parameters for the moderator should be sent when you create the participant. It might be easier to write two methods, add_user and add_moderator, to make things clear:
def add_user(contact, conference_name, label):
participant = client.conferences(conference_name).\
participants.create(
label=label, #label for participant
beep='onEnter',
record=True,
from_='from_', #same as above
to=str(contact),
start_conference_on_enter=False
)
def add_moderator(contact, conference_name, label):
participant = client.conferences(conference_name).\
participants.create(
label=label,
beep='onEnter',
record=True,
from_='from_',
to=str(contact),
start_conference_on_enter=True,
end_conference_on_exit=True
)
Then call different functions for the different participants:
add_moderator(contact_1, conference_name='Conf', label='laed#1')
add_user(contact_2, conference_name='Conf', label="consumer")
When you call add_user or add_moderator it will make an API request and slow down your server response. If you wanted to offload those requests to a worker, that would make your response quicker. But for 2 API requests, it is likely not a problem.
One other thing you might want to consider is the consumer experience. If they answer the phone before your agent does, then they will be greeted with hold music. You might want to architect it so that the application calls the agent first and only once they have picked up it then dials the consumer. Just worth considering.
Edit
After further explanation, you are now telling me that you want to make the call from the browser using the Twilio Voice SDK for JS.
To make outbound calls with the JS SDK you need to create an access token which includes an outgoing application sid, which refers to a TwiML application. That TwiML application has a voice URL. When you place the call with the SDK, Twilio makes a webhook request to the voice URL of your TwiML app. Your application can perform actions and return TwiML to tell Twilio what to do with the call.
When you create the call with the JS SDK you can pass parameters to the call.
const device = new Device(token);
const call = await device.connect({
params: {
To: ["+15551234567", "+145557654321"]
}
});
Those parameters are sent with the webhook request to your TwiML App voice URL. You can then use a response very similar to your original code to respond here, because you need to return TwiML to the request from Twilio, and start calls to the other participants in the call.
#app.route('/conference', methods=["POST"])
def conference():
const numbers = request.form["To"]
response = VoiceResponse()
with Dial() as dial:
dial.conference(
'Conf',
start_conference_on_enter=True,
end_conference_on_exit=True
)
response.append(dial)
for number in numbers:
add_user(number, conference_name='Conf')
return Response(str(response), 'text/xml')
This code receives the To parameter, a list of numbers to dial into this conference, builds the TwiML response that will put the browser caller into a conference call, places outbound calls to the numbers to dial them into the conference and then returns the TwiML to Twilio. The dialler in the browser will start the conference and the other participants will arrive in the conference when they answer the phone.
In this case you don't make the request to your server yourself, you use the JS SDK to trigger the call and let Twilio make the request to your server. As mentioned in the comments, you may want to offload the API calls to create participants to a background job so that you can respond to the webhook request quicker, but that is beyond the scope of this answer.
Related
I have a Twilio phone number configured to direct inbound calls to a PHP webhook. The webhook uses some of the addon information to try and find a useful caller name. I'm also using Twilio's built-in CNAM lookups, but they don't work right in Canada (I always get the caller's number as their name).
The webhook is designed to forward calls to a Twilio SIP Domain first, where I expect I'll be answering most of the calls. Other calls, if deemed urgent, will be forwarded via PSTN.
I've reached the point where I can pull out a relevant name, but I'm having difficulty trying to forward that information to my FXS (HT802). As per the device's documentation:
http://www.grandstream.com/sites/default/files/Resources/ht80x_administration_guide.pdf
Auto: When set to “Auto”, the HT801/HT802 will look for the caller ID in the order of P-Asserted Identity Header, Remote-Party-ID Header and From Header in the incoming SIP INVITE
I'm not able to find a means to pass these headers via a SIP noun in TwiML. Based on Twilio's documentation:
https://www.twilio.com/docs/voice/twiml/sip#custom-headers
UUI (User-to-User Information) header can be sent without prepending x-
https://www.twilio.com/docs/voice/api/sending-sip#sip-x-headers
If you send headers without X- prefix, Twilio will not read the header. As a result, the header will not be passed in the output.
For context, here's a reduced snippet of the PHP code I'm using so far. Note: I'm not actually doing anything with the $callerName value yet.
<?php
// Simple "starting value", in case we can't resolve the name.
// (will also resolve the numbers used for unknown/blocked IDs)
$callerName = FriendlyFormatPhoneNumber($_POST['From']);
use Twilio\Twiml;
$addOns = null;
if (array_key_exists('CallerName', $_POST)) {
$callerName = $_POST['CallerName'];
} elseif (array_key_exists('AddOns', $_POST)) {
$addOns = json_decode($_POST['AddOns']);
$teloName = $addOns->results->telo_opencnam->result->name;
// If we pulled a telo name, and it doesn't seem to be a phone number
// (in case that could happen), use the telo name.
if (isset($teloName) && preg_match('/.*[0-9]{4,}, $teloName') == 0) {
$callerName = $teloName;
}
}
$response = new TwiML;
$dialParams = array(,
'timeout' => 20,
'hangupOnStar' => false,
'answerOnBridge' => true,
'action' => API_BASE_URL . '/dial-callback.php'
);
$dialer = $response->dial($dialParams);
$dialer->sip('sip:101#mytwiliodomain.sip.us1.twilio.com;transport=tls');
echo $response;
Long story short: How do I pass a custom caller name to my SIP devices using TwiML and the Twilio SIP Domains? I don't want to overwrite the number, just the name. And only on the inbound calls to the devices registered to my Twilio SIP domain.
In case it helps: Don't worry about translating to PHP if that's not your field; I can translate from TwiML :)
Unfortunately, this is not possible with Twilio SIP Domains. Currently, there is no way to set the Caller Name via TwiML.
I've been stalking around here and have gotten most of my answers as I make my way through this new tool, but I'm now stuck and need some direct advice.
The Gather function in Studio is not PCI compliant, so I have to shift my call to a Function and return the parsed data--I finally figured out how to do that one--however, I've found that I cannot call the web service housed within the single function and had to send the with event.Digits to another function to make the web service call to my token provider. This works, however it has led to a strange result: my token is read back as TTS and then the call is hung up. I have no TTS action in play. Below are my sets of code:
Initial function called from Studio:
const got = require('got');
exports.handler = function(context, event, callback) {
let twiml = new Twilio.twiml.VoiceResponse();
twiml.gather({
input: 'dtmf',
finishOnKey: '#',
timeout: 10,
action: 'paymenttest',
method: 'GET'
}).say('Enter CC');
console.log(twiml);
callback(null, twiml);
};
This successfully calls my function with the digits entered:
const got = require('got');
exports.handler = function(context, event, callback) {
let twiml = new Twilio.twiml.MessagingResponse();
const url ='my payment gateway' + event.Digits + '&EXPDATE=1220&CARDTYPE=VI';
got.get(url, {
headers: {
'content-Type': 'application/x-www-form-urlencoded'
}
}).then(function(response) {
// Check the response and ask your second question here
event.callback(null, response.body);
}).catch(function(error) {
// Boo, there was an error.
callback(error)
});
};
This successfully returns the token....but as mentioned prior...it's read back out to me instead of getting included in the data returned back to Studio.
Twilio developer evangelist here.
Right now Studio is not well setup for using TwiML from a Twilio Function and then continuing the flow. In your case when you return the token from your second Function Twilio is dealing with it as if you just returned text to a regular TwiML webhook. When this happens Twilio defaults to assuming you meant <Say> and reads out the text.
While the team work on redirecting calls back into Studio flows there is a workaround.
Instead of returning the token in the second Function, return some TwiML that includes a <Redirect> to your Studio flow's webhook URL with ?FlowEvent=audioComplete appended. You will also need to add a dummy Say/Play widget after your Function widget (it becomes the next part in the flow that can trigger an "audio complete" message, so exists to collect that and send on to the next widget).
The only thing that we haven't handled in thie workaround is sending the token to the flow. I don't believe we can do this via this redirect workaround, so instead I'd recommend storing the token in your own database or something like a Twilio Sync object. This way you can use it outside of Studio however you like. If you need it within the Studio flow then you can create one more Function that returns the token as JSON, and that will be stored within the flow variables then.
If you would prefer to use <Pay>, as this would be a lot easier, I also recommend requesting the pay connector you need.
I think philnash answer here got old, even when it still works.
Right now, you should have to call the first function using TwiML Redirect node.
In the second function, you should have to add a redirect to the webhook of the flow adding ?FlowEvent=return&foo=bar (where foo=bar should be changed by the info you really want to return).
I'm using Twilio in an iOS app using the TwilioClient library in Swift. The app I've built can connect from user A's device to user B's device or to an outside phone number. This is working fine. What I need to do now is to bring a third party into the call, either a third iOS device (user C) or an additional outside line.
I understand that I will need to use the Conference capability to do this, but I'm not finding examples of how to do this with the TwilioClient library for iOS. Currently my TwiML server is the Python quick start server (which has been fine for two-party calls).
What I'm unclear on is:
Using the TwilioClient device.connect(...) call how do I connect User A & User B into a conference room in the first place?
Once I have A & B in the conference call, how, using TwilioClient, do I bring in party C? I've connected with device.connect(...) which hides all of the REST API code from me.
Since I don't know at the beginning of the call if I'll need the third party is there a way to start off with a direct connect then add all parties to a conference call once we decide we need to add User C?
Thanks,
KeithB
Below is an answer on how to solve question 1 in my OP.
When you use the iOS call device.connect(...) it calls the TwiML server /call URL (in my case a Python server), which returns "TwiML" (Twilio Markup Language). The basic pattern is to call a destination directly. To do this the TwiML server uses the passed parameters To: and From: and constructs a TwiML "Dial" string that is returned. This causes Twilio to dial the To: number using the From: number as a callerid.
In order to have both parties put into a conference it is a two step process. First you modify your TwiML code to tell Twilio to put the Caller into the conference room. Second, you use the REST API at the TwiML server to have Twilio call out to the To: caller and put them in the conference room.
This requires several changes. First you must pass in the conference room name from the device.connect(...) call in your iOS app.
connection = device?.connect(["To": "+1234567890", "Conference": "MyConference"], delegate: delegate)
Then you have to modify the /call route in the Python server to return TwiML for a conference room AND make the outbound REST call.
from twilio.rest import TwilioRestClient
#app.route('/call', methods=['GET', 'POST'])
def call():
account_sid = os.environ.get("ACCOUNT_SID", ACCOUNT_SID)
auth_token = os.environ.get("AUTH_TOKEN", AUTH_TOKEN)
from_value = request.values.get('From')
to = request.values.get('To')
conf_room = request.values.get('Conference')
callsid_value = request.values.get('CallSid')
# This section creates the TwiML code that will be returned
# This causes the caller to be placed in a conference room
# Note: endConferenceOnExit=true ends conference when call originator exits
conf_response = twilio.twiml.Response()
conf_response.dial(callerId=from_value).conference(conf_room, endConferenceOnExit="true")
# This section makes the REST call out to the To: number
client = TwilioRestClient(account_sid, auth_token)
call = client.calls.create(url="www.YourPythonServer" + "/getconf?Conference=" + conf_room,
to=to,
from_=from_value,
method="POST",
status_callback="www.YourPythonServer" + "/callback?init_callsid=" + callsid_value,
status_callback_method="POST",
status_events=["completed"])
return str(conf_response)
When we call client.calls.create(...) it causes Twilio to then call the URL "www.yourpythonserver/getconf..." to get the TwiML it uses to know how to execute the outgoing call.
#app.route('/getconf', methods=['GET', 'POST'])
def getconf():
resp = twilio.twiml.Response()
conf_room = request.values.get('Conference')
resp.dial().conference(conf_room)
return str(resp)
You'll notice up above we also have a parameter "status_callback". This is used to let us know the To: caller's status. In this case we request 'complete' but we may also get "no-answer", "busy" & "failed". If we don't do this and the call is unsuccessful then the caller will just sit in an empty conference room w/o knowing the outgoing call was unsuccessful. This requires yet another route in our Python server.
#app.route('/callback', methods=['GET', 'POST'])
def callback():
account_sid = os.environ.get("ACCOUNT_SID", ACCOUNT_SID)
auth_token = os.environ.get("AUTH_TOKEN", AUTH_TOKEN)
init_callsid = request.values.get('init_callsid')
callback_status = request.values.get('CallStatus')
# Check to see what status was passed in. Note that "completed" is
# passed after a successful call and the To: party hangs up or drops.
# You can implement any behavior you want here, but this code
# simply hangs up the originating caller. This might not be appropriate
# if there are still other parties in the call.
if callback_status == 'completed' or callback_status == "no-answer" or callback_status == 'busy' or callback_status == 'failed':
client = TwilioRestClient(account_sid, auth_token)
call = client.calls.hangup(init_callsid)
return str("")
The following is an answer to question #2 in my OP.
After step #1 we have a conference room named 'MyConference' with parties 1 & 2 (From & To). Now we would like to add a third party to the conference and trigger that action from our iPhone.
What we need to do is create a new Route in our python server and call that route directly from our iOS code.
The code in the python server will look something like this:
#app.route('/add_to_conference', methods=['GET', 'POST'])
def add_to_conference():
account_sid = os.environ.get("ACCOUNT_SID", ACCOUNT_SID)
auth_token = os.environ.get("AUTH_TOKEN", AUTH_TOKEN)
from_value = request.values.get('From')
to = request.values.get('To')
conf_room = request.values.get('Conference')
client = TwilioRestClient(account_sid, auth_token)
call = client.calls.create(url="www.YourPythonServer" + "/getconf?Conference=" + conf_room,
to=to,
from_=from_value,
method="POST",
status_callback="www.YourPythonServer" + "/callback?Conference=" + conf_room,
status_callback_method="POST",
if_machine = "Hangup",
status_events=["completed"]
)
return str("True")
The code above will make a REST call that will cause Twilio to make an outgoing call to the "To" phone number and place the party in the conference room. You will notice that like the code in step #1 we employ the '/getconf' route. The Twilio server will call this URL to get the TwiML that defines how it should process the call.
#app.route('/getconf', methods=['GET', 'POST'])
def getconf():
resp = twilio.twiml.Response()
conf_room = request.values.get('Conference')
resp.dial().conference(conf_room)
return str(resp)
We also defined a '/callback' route that is called upon call completion (hangup) or failure of caller #2 or #3. Since answering Step 1 I have enhanced the way this callback works. My goal is that when all 'To' parties hang up, leaving only the original caller in the conference room, the call should hangup for the original caller. Otherwise, the From caller will just be left in the conference room listening to music. This is not what is expected.
Therefore I implemented the following callback code:
#app.route('/callback', methods=['GET', 'POST'])
def callback():
account_sid = os.environ.get("ACCOUNT_SID", ACCOUNT_SID)
auth_token = os.environ.get("AUTH_TOKEN", AUTH_TOKEN)
callback_status = request.values.get('CallStatus')
conf_room = request.values.get('Conference')
client = TwilioRestClient(account_sid, auth_token)
conferences = client.conferences.list(friendly_name=conf_room, status = "in-progress")
if (len(conferences) > 0):
conference = conferences[0]
participants = client.participants(conference.sid).list()
if len(participants) == 1:
participant = participants[0]
call = client.calls.hangup(participant.call_sid)
return str("200")
When called, this code grabs the conference room by name, then grabs the list of participants in the conference room. If there is only one participant remaining then it hangs up on the last participant.
The final step is making the call from your iOS code. This is quite simple, you do not need to use the Twilio client libraries for this call, you simply make an http GET request passing in From, To and the Conference room name as parameters. Like this:
http:/yourpythonserver.com/add_to_conference?To=1234567890&From=0987654321&Conference=MyConference
This will activate the entire process and party #3 will be placed in the conference room!
I have this conceptual Twiml that I want the echo twimlet to provide:
<Response>
<Record timeout="10" method="GET" action="http://someURL" />
<Redirect>http://twimlets.com/forward</Redirect>
</Response>
My intent is to use this for outbound dialing, so that the calls that are being made are recorded. The issue is that the request parameters are sent to the echo Twimlet containing the To, From, CallerID, etc. but I really need them passed to the url in the Redirect verb. Is that possible using the echo Twimlet?
Twilio developer evangelist here.
You don't actually need to do this in order to record a two legged call. The <Record> verb is used to record messages, for example a voicemail service.
If you are generating these calls using the REST API, then you can set the call to record in the API call, like this (example in Node.js, I see you've answered some SO questions with Node):
var accountSid = 'AC...';
var authToken = "{{ auth_token }}";
var number1 = '+1555123456';
var number2 = '+1555456789';
var twilioNumber = '+1555654321';
var client = require('twilio')(accountSid, authToken);
client.calls.create({
url: "http://twimlets.com/forward?PhoneNumber=" + encodeURIComponent(number2),
to: outboundNumber,
from: twilioNumber,
record: true
}, function(err, call) {
process.stdout.write(call.sid);
});
You can also give that call a statusCallBack URL that the recording will be POSTed to after the call.
If you are not generating the call from the REST API but you still want to record both sides of the call. You need to use the <Dial> verb and set to record that way. You need to create some TwiML at a URL your Twilio number points to that looks like this:
<Response>
<Dial record="record-from-answer">
{{ onward number }}
</Dial>
</Response>
If you supply an action attribute to the <Dial> verb then once the call is done Twilio will POST the URL of the recording to the action.
I'm not exactly sure how you'd accomplish this with Twimlets. Ideally you want to be able to set the URL that the recording URL is sent to and save it somehow, but you'd need your own server for that. It would be possible to create any of the resulting TwiML you'd need using the echo Twimlet, but it may be better to consider your own server at this point.
Let me know if this helps at all.
twilio javascript client set from number , Also how I can get the call sid after connect?
I tried to set the from Number in the call options like the next lines before connect and still the same issue in the javascript
$(document).ready(function () {
Twilio.Device.setup(token);
var connection = null;
$("#call").click(function () {
var params = { "Phone": $('#Phone').val(), "from":$('#Phone').val() };
connection = Twilio.Device.connect(params);
return false;
});
});
-- and inside the server side code vbnet when I am generating the token I added the next code but this doesn't solve the from number issue
Dim options As New CallOptions()
options.Record = True
options.From = model.FromNumber
Dim cap = New TwilioCapability(_settings.AccountSID, _settings.AuthToken)
cap.AllowClientOutgoing(_settings.ClientCallApplicationSID, options)
dim token = cap.GenerateToken()
Twilio evangelist here.
The params collection that you pass into the connect function is just a dictionary of key/value pairs. Those key/values simply get passed as parameters to the Voice URL that Twilio requests when Client makes its connection to Twilio, and you can use those parameters to dynamically generate some TwiML markup. Twilio does not do anything with them.
For example, if this is a PHP application, in the Voice URL you could do something like:
<Response>
<Dial>$_REQUEST['From']</Dial>
</Response>
One note of caution, Twilio already adds a parameter called from (which in the case of Client will be the client identifier set when you made your capability token) to the parameters sent to the Voice URL, so you might want to choose a different key name for your dictionary entry. I normally use a name like target for the key that holds the number that I want to dial.
Hope that helps.
To get the call sid, you can get it in connect event.
Please note that I am using Twilio JS v1.9.7
device.on('connect', function (conn) {
log('Successfully established call!');
//Get the CallSid for this call
callUUID = conn.outboundConnectionId;
console.log('CallSid: ' + callUUID);
});