I.T. Security and Linux Administration

Sep 30 2013   4:57PM GMT

Create Your Own Two-Factor Authentication System: Outgoing Calls

Eric Hansen Eric Hansen Profile: 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:

def voice(number):
        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!

 Comment on this Post

There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

Share this item with your network: