title: Create own IVR that will access Database
body: <p>I'm currently interning with a company and have been tasked with researching some methods of using telephony. The goal is to provide our clients with the ability to call in and through an IVR-prompted questions, get information back. The information will be from our database.</p>
  
  <p>I have successfully done this using Twilio and a small python app. It does exactly what I'm looking to do, except the cost factor can be a bit high, especially if we have 30,000+ clients calling for minutes on end.</p>
  
  <p>My goal is to find a way to replicate what I've done with Twilio, but on our own server. I've found options like Asterisk and IncrediblePBX, but because my limited knowledge of Linux, every error I run into results in scouring the internet for answers. Ultimately, I'm not sure if I'm heading in the right direction.</p>
  
  <p>This is an example of what I'd like to accomplish:</p>
  
  <p>Client calls into number. They're directed to provide an account number, (possibly their phone number) At that point it will take this information and talk to a database. Gathering this information it will relay back to the client the status of their account etc. </p>
  
  <p>Questions:
  I was hoping to use Google Voice to route calls similar to Twilio, is this possible? Alternatively, could my company switch to a VoIP and do the same thing?</p>
  
  <p>If I move away from Twilio, can Asterisk perform the necessary tasks? Receiving calls and running the app to gather database information.</p>
  
  <p>Current code for Twilio, in Python:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>from flask import Flask, request, redirect
  import twilio.twiml
  import json
  from urllib.request import urlopen
   
  app = Flask(__name__)
  callers = {
      "+": "Nicholas",
  }
   
  @app.route("/", methods=['GET', 'POST'])
  def initial():
      # Get the caller's phone number from the incoming Twilio request
      from_number = request.values.get('From', None)
      resp = twilio.twiml.Response()
   
      # if the caller is someone we know:
      if from_number in callers:
          # Greet the caller by name
          caller = callers[from_number]
      else:
          caller = ""
  
      resp = twilio.twiml.Response()
      resp.say("Hello " + caller)
      resp.say("Thank you for calling.")
      your_number = list(from_number)
      del your_number[0]
      del your_number[0]
      resp.say("You are calling from: ")
      x = 0
      while x &lt; len(your_number):
          resp.say(your_number[x])
          x += 1
  
      print("Please enter the neighborhood I.D. you're looking for.")
      with resp.gather(numDigits=1, action="/handle-first", method="POST") as g:
       g.say("Please enter the neighborhood I.D. you're looking for.")
  
      return str(resp)
  
  @app.route("/handle-first", methods=['GET', 'POST'])
  def handle_key():
      digit_pressed = request.values.get('Digits', '')
      resp = twilio.twiml.Response()
      url = 'http://localhost/...'
      response = urlopen(url)
      data = json.loads(response.readall().decode('utf-8'))   
      current = data['rows'][0]['Neighborhood']
      print(current)
      resp.say("You have chosen " + current + "as your neighborhood.")
      with resp.gather(numDigits=1, action="/handle-second", method="POST") as h:
          h.say("Press 1 to choose another Neighborhood?")
  
      return str(resp)
  
  @app.route("/handle-second", methods=['GET', 'POST'])
  def handle_key2():
      digit_pressed = request.values.get('Digits', '')
      resp = twilio.twiml.Response()
      if digit_pressed == "1":
          return redirect("/")
  
      else:
          resp.say("Thank you for calling. Good-bye.")
          return str(resp)
  
   
  if __name__ == "__main__":
      app.run(debug=True)</code></pre>
  </div>
  </div>
  </p>