【问题标题】:Twillo - how to handle no input on <gather>Twilio - 如何处理 <gather> 上的无输入
【发布时间】:2016-09-07 21:50:15
【问题描述】:

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

我正在尝试找出以 DRY 方式处理来自 TwiML &lt;Gather&gt; 动词的无调用者输入的最佳实践。我创建了一个函数process_no_input_response,它接收并返回一个带有适当&lt;Say&gt; 消息的twllio resp 对象,具体取决于是否达到了允许的最大重试次数。代码示例如下。

有没有更好的方法来处理这些情况?渴望获得有关此代码的任何建议或反馈。

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)

【问题讨论】:

    标签: python flask twilio twilio-twiml


    【解决方案1】:

    我实际上找到了一种更简洁的方法来执行此操作,而无需辅助函数。在这种方法中,/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)
    

    【讨论】:

      【解决方案2】:

      这里是 Twilio 开发者宣传员。

      这似乎是构建此响应的合理方式。重定向回合以再次播放&lt;Gather&gt; 直到您进行多次尝试是处理这种情况的好方法(我现在想不出更好的方法来处理)。事实上,这实际上是Advanced use section of the documentation 中建议的解决方案。

      【讨论】:

      • 非常感谢@philnash 的快速回复 - 我想我找到了一种更优雅、更整洁的方法,并发布了单独的答案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多