Posted by: Eric Hansen
Now we’ll go into making outbound calls. This is pretty similar to SMS but does get more advanced pretty quickly. Now is the time where being Internet-reachable is a necessity.
First, how to create a call. For this I’m going to make the functions easy again:
@app.route("/voice/") def voice(number): try: client.calls.create(url="http://ip_address:5000/who/%d" % number, to="+1%d" % number, from_=TWILIO_NUMBER) return "Call sent." except TwilioRestException, e: return "Unable to send. Reason: %s" % (e.msg)
This is very similar to our SMS method with the exception being “client.calls.create” instead of “client.sms.messages.create”. Also, instead of passing a message to the user we are passing a URL to forward them to. While you don’t have to pass a URL, there does have to be something for Twilio to process (which will be covered shortly). So, basically, when the call is created Twilio will read special XML generated at /who/[number] and go from there (there’s a few options, we’ll cover the basic message reading first). Here’s the method for /who:
@app.route("/who/", methods=['POST']) def who(number): token = generate_token(number) xml = TwiML.Response() xml.say("Your token is: %s" % ". ".join([i for i in token])) return make_response(str(xml), 200)
Twilio only POSTs when itself is referencing a URI/URL, so that is why we only support POST for /who. I won’t go into the basics of TwiML (Twilio’s XML), but basically what’s going on is we are creating a reference to our response XML, and then passing it what we want it to read back to the user. From my experience, if HTTP response is not 200 then you will run into issues, so that is why we force a 200 (HTTP OK) response regardless.
The reason why we rejoin our token like we do is that if we gave it as one long strong then Twilio’s text-to-speech service would jumble it all up. If you just space the characters apart it reads still a little too fast. So we instead make Twilio read each character as if they are a sentence by themselves.
Visit http://ip_address/voice/verified_number (replacing “verified_number” with your verified number like with SMS) and give it a try!