1

I have the following questions for the code/project below.

  1. Twilio: what should I be using as the webhook URL for voice/messaging? I'm currently using the ngrok url for both, and I have both configured to POST. Is this right? or should it be GET?

  2. I believe I will need to make a static ngrok url below and make it https://<your-ngrok-url.ngrok.io>/voice -- do I need to get the paid version to make this static?

I believe that after doing these two things, the application should work since the code works just fine.

(Also, if you have any links so that I might better understand these applications and how they are used would be amazing.)

(phones and keys redacted)

##API info for initiaiting the call

from twilio.rest import Client

account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
auth_token = '5dXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
client = Client(account_sid, auth_token)

call = client.calls.create(
    url='https://your-ngrok-url.ngrok.io/voice',
    to='+19511231234',
    from_='+12311231234'
)

##this gathers user input from the caller
from flask import Flask
from twilio.twiml.voice_response import VoiceResponse, Gather

app = Flask(__name__)

@app.route("/voice", methods=['GET', 'POST'])
def voice():
    # Start a TwiML response
    resp = VoiceResponse()
    gather = Gather(num_digits=1, action='/gather')
    gather.say('Hello, this is Alice from your marketing company. I am calling to test the lines for an upcoming campaigm. Please press 1 as confimation of receipt of this test.')
    resp.append(gather)
    resp.redirect('/voice')
    return str(resp)

@app.route('/gather', methods=['GET', 'POST'])
def gather():
    """Processes results from the <Gather> prompt in /voice"""
    # Start TwiML response
    resp = VoiceResponse()

    # If Twilio's request to our app included already gathered digits,
    # process them
    if 'Digits' in request.values:
        # Get which digit the caller chose
        choice = request.values['Digits']

        # <Say> a different message depending on the caller's choice
        if choice == '1':
            resp.say("Thank you, goodbye!")
            return str(resp)
        elif choice == '2':
            resp.say("Sorry, I don't understand that choice. Please press 1")
            return str(resp)
        else:
            # If the caller didn't choose 1 or 2, apologize and ask them again
            resp.say("Sorry, I don't understand that choice. Please press 1.")

    # If the user didn't choose 1 or 2 (or anything), send them back to /voice
    resp.redirect('/voice')

    return str(resp)
khotch93
  • 11
  • 1

1 Answers1

0

Twilio developer evangelist here.

Twilio: what should I be using as the webhook URL for voice/messaging? I'm currently using the ngrok url for both, and I have both configured to POST. Is this right? or should it be GET?

Twilio can send webhooks as GET or POST requests, but I recommend POST requests as the parameters can be long and different servers have different limits to query string parameters.

I believe I will need to make a static ngrok url below and make it https://<your-ngrok-url.ngrok.io>/voice -- do I need to get the paid version to make this static?

You can use an ngrok URL to test your application, it is a useful tool to create a URL that tunnels through to an application running on your computer. In production I would recommend you host this application on a server and use a public URL that you own and can point at that server.

philnash
  • 70,667
  • 10
  • 60
  • 88