Avoiding repeated outbound calls from twilio python program - twilio

This is my python script that I run from command line.It makes a call to the 10_DIGIT_NO.If I dont answer the call it repeatedly makes calls.The twiml was generated via https://www.twilio.com/labs/twimlets/my/create?type=callme. How to avoid this??
I want the call to be made just once irrespective of it being received or not.
Thanks
# Download the library from twilio.com/docs/libraries
from twilio.rest import TwilioRestClient
# Get these credentials from http://twilio.com/user/account
account_sid = "xyz"
auth_token = "abc"
client = TwilioRestClient(account_sid, auth_token)
# Make the call
call = client.calls.create(to="10_DIGIT_NO", # Any phone number
from_="MY_TWILIO_NO", # Must be a valid Twilio number
url="XML_FILE_PATH")
print call.sid

Twilio Developer Evangelist here. It looks like we'll need to dig into your account a bit to diagnose this properly - I would recommend e-mailing help#twilio.com so that we can help you find a solution!

Related

Twilio Conference Call

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.

How to get Twilio Studio Flow execution via call sid

We're trying to include the IVR steps in our UI, but to get the steps I have to make several API calls. That's fine, except the only way I can seem to get the relevant info is to load all flow executions.
If I could pass the flow.sid via the HTTP Request widget then I could go fetch the info I need later instead of having to iterate through all the previous executions. I tried passing {{flow.data}} as the request body, thinking it was JSON, but it ends up being empty.
Here's a spike that someone wrote for us, modified to just work with a single execution.
require "httparty"
STUDIO_FLOW_SID = "FW***"
AUTH = {username: ENV["TWILIO_ACCOUNT_SID"], password: ENV["TWILIO_AUTH_TOKEN"]}
DATE_CREATED_FROM = "2019-09-01T000000Z"
DATE_CREATED_TO = "2019-10-01T000000Z"
# Retrieves all executions in the given date range
executions_url = "https://studio.twilio.com/v1/Flows/#{STUDIO_FLOW_SID}/Executions?DateCreatedFrom=#{DATE_CREATED_FROM}&DateCreatedTo=#{DATE_CREATED_TO}"
response = HTTParty.get(executions_url, basic_auth: AUTH)
# If I can get the individual execution from the IVR {{flow.data}}
# that would be ideal
execution = response.parsed_response["executions"].first
execution_context_url = execution["links"]["execution_context"]
execution_context = HTTParty.get(execution_context_url, basic_auth: AUTH)
# Or, if I could work backwards and get the execution context ID from
# the call somehow, that would work too.
call_sid = execution_context.parsed_response["context"]["trigger"]["call"]["CallSid"]
steps = HTTParty
.get(execution["links"]["steps"], basic_auth: AUTH)
.parsed_response["steps"]
.sort_by { |step| step["date_created"] }
.map { |step| step["transitioned_to"] }
.select { |step| step.include?("option") || step.include?("menu") }
puts [call_sid, steps].inspect
tl;dr -
I either need the Flow execution info passed in an HTTP Request widget or I need to work backwards from a CallSid to get the execution steps.
Twilio developer evangelist here.
The execution Sid can be accessed in the flow's data, under flow.sid.
This was missing in the documentation, but I've just added it here: https://www.twilio.com/docs/studio/user-guide#context-variables
Note: {{flow.sid}} doesn't currently appear in Studio's autocomplete, but it's there, I promise!
I found a way to get the execution from the call:
Get the list of incoming phone numbers: https://www.twilio.com/docs/phone-numbers/api/incomingphonenumber-resource#read-multiple-incomingphonenumber-resources
Search for the number using the destination of the call
Then you can get the flowid by parsing voice_url
With this flow_id and the date the call arrived, you can get the list of executions: https://www.twilio.com/docs/studio/rest-api/v2/execution#read-a-list-of-executions
If you have more than one, you can use the calling number to search in the list.
Anyway, if you are calling the function from Studio, philnash answer is better :)

Can't make custom Twilio Call with TwiML Bin and Assets

I am trying to make a twilio call with hosting an xml file on Twilio TwiMl Bins and hosting an mp3 file in Assets:
I just want to say a simple sentence and then play a 5-10 second clip. Currently when I run my code:
# Download the helper library from https://www.twilio.com/docs/python/install
import os
from twilio.rest import Client
# Your Account Sid and Auth Token from twilio.com/console
account_sid = "stuff"
auth_token = "stuff"
client = Client(account_sid, auth_token)
call = client.calls.create(
url="https://handler.twilio.com/twiml/EHdff1ba57cc168191864794c6c44c1723",
from_="+1mytwilionumber",
to="+1mynumber"
)
print(call.sid)
I just get a call - the trial message and then silence and then the call is concluded. Any tips?
Never Mind! So this worked:
https://support.twilio.com/hc/en-us/articles/223132187--Not-Authorized-error-when-trying-to-view-TwiML-Bin-URL
But your twilio call will only run once after you "sign" the HTTP request with hmac authentication. The script is
const crypto = require('crypto')
, request = require('request')
const url = process.argv[2] + '?AccountSid=' + process.env.TWILIO_ACCOUNT_SID
const twilioSig = crypto.createHmac('sha1', process.env.TWILIO_AUTH_TOKEN).update(new Buffer(url, 'utf-8')).digest('Base64')
request({url: url, headers: { 'X-TWILIO-SIGNATURE': twilioSig }}, function(err, res, body) {
console.log(body)
})
Make sure to have node, npm, and request module installed.
npm install request --save
and you are good to go! Run this script right before your python calling script.
Just an fyi you can also make TWIML/robocalls on the fly by uploading your TWIML to a bin, just before you make the call. It just needs a public URL. This project that I found actually demonstrates the technique by uploading the TWIML/XML on the fly, getting the new URL (for the newly generated TWIML), and then makes the Twilio call. That restwords.com site is pretty handy for this purpose.

Call more than 10 numbers at once

We need to implement simultaneous GSM calls, but as stated in documentation (https://www.twilio.com/docs/api/twiml/number), "You can specify up to ten numbers within a verb to dial simultaneously" which looks like a very tough limit.
Could anyone please suggest a workaround for this?
You can do this via the REST API: https://www.twilio.com/docs/api/rest/making-calls.
A quick example would look something like this:
# Download the Python helper library from twilio.com/docs/python/install
from twilio.rest import TwilioRestClient
# Your Account Sid and Auth Token from twilio.com/user/account
account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
auth_token = "your_auth_token"
client = TwilioRestClient(account_sid, auth_token)
# The following grabs a list of numbers that have sent a message to my Twilio number.
call_list = client.messages.list(to='+1415XXXXXXX')
# Then loop through all the numbers in the list and send a call from Twilio number via REST API.
for c in call_list:
client.calls.create(to=c.from_, from_="+1415XXXXXXX", url='YOUR_VOICE_URL')

Using Twilio I want to add a third party into a call using an iOS Device

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!

Resources