Twillo - 如何处理<gather>上没有输入

时间:2016-09-07 21:50:16

标签: python flask twilio twiml

我正在使用Python烧瓶和twillo-python helper library构建一个简单的Twillo(可编程语音)应用程序。语音菜单有几个步骤,但第一步要求来电者输入一个密码。

我正在努力找出以干燥方式处理TwiML <Gather>动词中没有来电者输入的最佳做法。我已经创建了一个函数process_no_input_response,它接收并返回一个带有相应resp消息的twllio <Say>对象,具体取决于是否已达到允许的最大重试次数。代码示例如下。

有没有更好的方法来处理这些情况?热衷于对此代码提出任何建议或反馈。

def process_no_input_response(resp, endpoint, num_retries_allowed=3):
    """Handle cases where the caller does not respond to a `gather` command.
       Determines whether to output a 'please try again' message, or redirect 
       to the hand up process

    Inputs:
      resp -- A Twillo resp object
      endpoint -- the Flask endpoint
      num_retries_allowed -- Number of allowed tries before redirecting to 
        the hang up process

    Returns:
      Twillo resp object, with appropriate ('please try again' or redirect) syntax
    """

    # Add initial caller message
    resp.say("Sorry, I did not hear a response.")

    session['num_retries_allowed'] = num_retries_allowed

    # Increment number of attempts
    if endpoint in session:
        session[endpoint] += 1
    else:
        session[endpoint] = 1

    if session[endpoint] >= num_retries_allowed:
        # Reached maximum number of retries, so redirect to a message before hanging up
        resp.redirect(url=url_for('bye'))
    else:
        # Allow user to try again
        resp.say("Please try again.")
        resp.redirect(url=url_for(endpoint))

    return resp

@app.route('/', methods=['GET', 'POST'])
def step_one():
    """Entry point to respond to incoming requests."""

    resp = twilio.twiml.Response()
    with resp.gather(numDigits=6, action="/post_step_one_logic", method="POST") as gather:
        gather.say("Hello. Welcome to my amazing telephone app! Please enter your pin.")

    return str(process_no_input_response(resp, request.endpoint))    

@app.route('/bye', methods=['GET', 'POST'])
def bye():
    """Hangup after a number of failed input attempts."""

    resp = twilio.twiml.Response()
    resp.say("You have reached the maximum number of retries allowed. 
       Please hang up and try calling again.")
    resp.hangup()

    return str(resp)

2 个答案:

答案 0 :(得分:2)

我实际上找到了一种更为简洁的方法,而不需要辅助函数。在这种方法中,所有超时逻辑(即“请再次尝试”或“挂断”在/timeout端点。

这似乎是对超时的隐含建议,看过Advanced use section of the Twillo documentation中的示例。

@app.route('/', methods=['GET', 'POST'])
def step_one():
    """Entry point to respond to incoming requests."""

    resp = twilio.twiml.Response()
    with resp.gather(numDigits=6, action="/post_step_one_logic", method="POST") as gather:
        gather.say("Hello. Welcome to my amazing telephone app! Please enter your pin.")
    resp.redirect(url=url_for('timeout', source=request.endpoint))

    return str(resp)


@app.route('/timeout', methods=['GET', 'POST'])
def timeout():
    """Determines whether to output a 'please try again' message, or if they
       should be cut off after a number of (i.e. 3) failed input attempts.
       Should include 'source' as part of the GET payload.
    """

    # Get source of the timeout
    source = request.args.get('source')

    # Add initial caller message
    resp = twilio.twiml.Response()
    resp.say("Sorry, I did not hear a response.")

    # Increment number of attempts
    if source in session:
        session[source] += 1
    else:
        session[source] = 1

    # Logic to determine if user should be cut off, or given another chance
    if session[source] >= 3:
        # Reached maximum number of retries, so redirect to a message before hanging up
        resp.say("""
            You have reached the maximum number of retries allowed. Please hang up 
            and try calling again.
            """)
        resp.hangup()
    else:
        # Allow user to try again
        resp.say("Please try again.")
        resp.redirect(url=url_for(source))

    return str(resp)

答案 1 :(得分:0)

Twilio开发者传道者在这里。

这似乎是构建此响应的合理方法。重定向再次播放<Gather>直到你进行多次尝试是处理这种情况的一种很好的方式(我现在无法想到一个更好的方法来处理)。实际上,它实际上是Advanced use section of the documentation中建议的解决方案。