I am trying to write a Twilio script to make a call in Python. I am trying to follow the documentation, but am baffled by the url parameter.
The example the documentation supplies is as follows:
from twilio.rest import TwilioRestClient
account_sid = "ACXXXXXXXXXXXXXXXXX"
auth_token = "YYYYYYYYYYYYYYYYYY"
client = TwilioRestClient(account_sid, auth_token)
call = client.calls.create(to="+14085551234", # Any phone number
from_="+12125551234", # Must be a valid Twilio number
url="http://twimlets.com/holdmusic?Bucket=com.twilio.music.ambient")
print call.sid
What is the url parameter doing here? Should I copy it or supply my own URL? Does that mean I have to write my own endpoint?
The documentation on URL parameter is not much more enlightening: https://www.twilio.com/docs/api/rest/making-calls#url-parameter
Please could someone explain what this is, and whether I need to change it or not?
The Url parameter determines what is heard on the call when the phone is answered. This is customizable using TwiML (Twilio Markup Language) a set of roughly a dozen XML tags, which your application should return at the specified url. The XML your application returns at the Url decides how to handle the call, for example whether to forward the call to another line, speak some words or send an SMS reply. You can read a bit more about TwiML here:
https://twilio.radicalskills.com/projects/getting-started-with-twiml/2.html
Related
I have a website that requires login then allows a search api, that's public, it's returning error 405, How do I give parameters in requests.post, since following code isn't working
response = requests.post(
'https://www.somewebsite.com/api/public/',
auth=('asct1#gmail.com', 'Cons')
)
As you can see here 405 means that the method you chose in your case .get(...) is not allowed on the endpoint.
https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/405
You wrote as well that you should make a Post request. So give requests.post(...) a try!
Happy Coding, T!
What is the proper way to us a Twilio Add-on that provides intelligence on a phone number for "Lookups" in Python?
Currently, I have it working with "Incoming Voice Call" and "Incoming SMS Message" (Note: you can see "lookups", "Incoming Voice Call: and "Incoming SMS Message" in the attached picture).
With "Incoming Voice Call" and "Incoming SMS Message", the information is automatically provided with the rest of the information pertaining to the call or SMS when Twilio makes it's initial request to my application. The problem with this is that in practice, I am paying 10 times over for many spam numbers that are constantly dialing in.
Instead, I would like to look up the information manually, only when I do not already have the information cached.
It looks like I can make a raw HTTPS request from Python to do this. Example:
https://lookups.twilio.com/v1/PhoneNumbers/+16505399600/?AddOns=nomorobo_spamscore&AddOns.nomorobo_spamscore.secondary_address=+15108675309
However, it seems like a more "proper" way would be to use the TwilioRestClient that comes with the Twilio python helper library. However, its not clear get this Add-on information through the Twilio python helper library. If I'm being honest, I have a lot of trouble understanding those docs.
Twilio developer evangelist here.
You can use the Twilio Python library to achieve this with Lookups too. You can do a lookup, with the example URL you gave, like this:
import os
from twilio.rest import Client
account_sid = os.environ['TWILIO_ACCOUNT_SID']
auth_token = os.environ['TWILIO_AUTH_TOKEN']
client = Client(account_sid, auth_token)
number = client.lookups.phone_numbers("+16505399600").fetch(
add_ons="nomorobo_spamscore",
add_ons_data={
"nomorobo_spamscore": {
"secondary_address": "+15108675309"
}
}
)
print(number.add_ons)
number.add_ons will be a dict of the returned data from the Add-on. You pass the Add-ons you want to use with the add_ons argument (as a string or array of strings) and any further data in the add_ons_data argument.
Let me know if that helps at all.
I am upgrading my Twilio-enabled app from the old SDK to the new Twilio Programmable Voice (beta 5) but have run into several problems. Chief among them is poor audio quality of outgoing calls, in what can only be described as what lost packets must sound like. The problem exists even when I run the Quickstart demo app, leading me to the conclusion the problem rests in my Twiml. I've followed the instructions to a "T" with respect to setting the appropriate capabilities, entitlements, provisioning profile and uploading the voip push credential, but with little documentation on the new SDK or for Python versions of the server, I'm left scratching my head.
The only modifications to the demo app I've made are to include the "to" and "from" parameters in my call request like so:
NSDictionary *params = #{#"To" : self.phoneTextField.text, #"From": #"+16462332222",};
[[VoiceClient sharedInstance] configureAudioSession];
self.outgoingCall = [[VoiceClient sharedInstance] call:[self fetchAccessToken] params:params delegate:self];
The call goes out to my Twiml server (a python deployment on Heroku) at the appropriate endpoint as seen here:
import os
from flask import Flask, request
from twilio.jwt.access_token import AccessToken, VoiceGrant
from twilio.rest import Client
import twilio.twiml
ACCOUNT_SID = 'ACblahblahblahblahblahblah'
API_KEY = 'SKblahblahblahblahblahblah'
API_KEY_SECRET = 'blahblahblahblahblahblah'
PUSH_CREDENTIAL_SID = 'CRblahblahblahblahblahblah'
APP_SID = 'APblahblahblahblahblahblah'
IDENTITY = 'My_App'
CALLER_ID = '+15551111' # my actual number
app = Flask(__name__)
#app.route('/makeTheDamnCall', methods=['GET', 'POST'])
def makeTheDamnCall():
account_sid = os.environ.get("ACCOUNT_SID", ACCOUNT_SID)
api_key = os.environ.get("API_KEY", API_KEY)
api_key_secret = os.environ.get("API_KEY_SECRET", API_KEY_SECRET)
CALLER_ID = request.values.get('From')
IDENTITY = request.values.get('To')
client = Client(api_key, api_key_secret, account_sid)
call = client.calls.create(url=request.url_root, to='client:' + IDENTITY, from_='client:' + CALLER_ID)
return str(call.sid)
The console outputs outgoingCall:didFailWithError: Twilio Services Error and the call logs show a completed client call. An inspection of the debugger shows TwilioRestException: HTTP 400 error: Unable to create record. As you can see, the url I include in the request might be problematic as it just goes to the root but there is no way to leave the url blank (that I have found). I will eventually change this to a url=request.url_root + 'handleRecording' for call recording purposes but am taking things one step at a time for now.
My solution so far has been to ditch the call = client.calls.create in favor of the dial verb like so:
resp = twilio.twiml.Response()
resp.dial(number = IDENTITY, callerId = CALLER_ID)
return str(resp)
This makes calls, but the quality is so poor as to render it useless. (10+ seconds of silence followed by intermittent spurts of hearing the other party). Using the dial verb in this way is also unacceptable because of its inefficiency as I'm now billed for two calls each time.
The other major problem, which I'm not sure is connected or not, is the fact that I haven't yet been able to receive any incoming calls, though I suspect I may need to ask that question separately.
How can I get this line to work? I'm looking at you, #philnash. Help me make my app great again. :)
call = client.calls.create(url=request.url_root, to='client:' + IDENTITY, from_='client:' + CALLER_ID)
sorry it's taken me a while to get back to your question.
Firstly, the correct way to make the ongoing connection from your Programmable Voice SDK call is using TwiML <Dial>. You were creating a call using the REST API, however you will have already have created the first leg of the call in the SDK and the TwiML forwards onto the second leg of the call, the person you dialled. Notably, you are billed for each leg of the call, not for two calls (legs can be of different length, for example, you could put the original caller through a menu system before dialling onto the recipient).
Secondly, regarding poor call quality, that's not something I can help with on Stack Overflow. The best thing to do in this situation is to get in touch with Twilio support and provide some Call SIDs for affected calls. If you can record an example call that would help too.
Finally, I haven't seen if you've asked another question about incoming calls yet, but please do and I'll do my best to help there. That probably is a code question that we can cover on SO.
I'm having trouble with making outgoing calls using Twilio and I believe I skipped this step where I should submit a POST request to the Calls resource. I don't really understand what to do with this and I need someone to explain it for me since I'm just a beginner. And by the way, my Twilio account is just free trial. Does this have something to do as to why I can't make local calls?
Thanks.
Twilio developer evangelist here.
Best place for you to start would be the Python making calls quick start. Follow that tutorial through and you should understand how to make a call with Python.
Though, as you say, the key is the POST request to the calls resource. It's easiest to perform this using our Python helper library and looks a bit like this:
from twilio.rest import TwilioRestClient
# Get these credentials from http://twilio.com/user/account
account_sid = "ACXXXXXXXXXXXXXXXXX"
auth_token = "YYYYYYYYYYYYYYYYYY"
client = TwilioRestClient(account_sid, auth_token)
# Make the call
call = client.calls.create(to="+14085551234", # Any phone number
from_="+12125551234", # Must be a valid Twilio number
url="http://twimlets.com/holdmusic?Bucket=com.twilio.music.ambient") # Just plays hold music
Let me know if that helps at all.
I'm attempting to get the "from" number from an SMS message that I send to my Twilio number.
I have a Django server running that takes this message and performs some back-end processing, but I can't seem to retrieve the "from" number that sends that SMS to the Twilio number via Python. I keep getting 500 errors that state that this 'from' attribute doesn't exist but the Twilio documentation states otherwise.
def respond_to_sms(request):
# save the http request message
twilio_request = decompose(request)
# this works fine
twilio_message_body = twilio_request.body
# this does not
from_number = twilio_request.from
# perform backend stuff on from_number
Anyone know how to do this? I'm using the Django-Twilio package.
Twilio developer evangelist here.
You almost got it right, but it turns out that in the Python library, the name of the attribute is from_ as opposed to just from. That's because in Python, from is a reserved keyword as you can see here.
So try changing your code to the following:
from_number = twilio_request.from_
And you'll get it working. More information about the library can be found here.
Hope this helps you out!