【问题标题】:How to retrieve information from outbound Twilio call with Python?如何使用 Python 从出站 Twilio 调用中检索信息?
【发布时间】:2019-12-18 15:06:02
【问题描述】:

我是 Twilio 的新手,我正在尝试弄清楚如何从我使用 Python 3 成功进行的出站呼叫中检索数据。我希望能够检索诸如从收件人那里按下了什么按钮之类的内容。

在阅读了一点 Twilio 文档(然后有点迷失)之后,我想我了解了 Twilio 的工作原理以及为什么我无法从电话中检索数据。我认为 Python 程序只是建立了从 Twilio 到电话号码的连接。收件人可以拨打任何号码,我可以使用标签获取一些信息。但是如何将这些信息引导到我的 Python 程序?这个想法是让 Twilio(以某种方式)将信息发送回我的 Python 程序,然后我可以采取行动(比如更新数据库)。

我猜测 Twilio 会将数据扔到其他地方,然后我的 Python 程序可以去检索这些数据,但我不知道在哪里学习该技能。我有 Python 3 的基本基础,但对 Web 开发了解不多。只是一些基本的 HTML5 和 CSS3。

【问题讨论】:

    标签: python twilio call outbound


    【解决方案1】:

    这里是 Twilio 开发者宣传员。

    您可能在呼入电话中看到了这个documentation on gathering user input via keypad in Python

    当您接到入站电话时,Twilio 会发出 webhook 请求以了解下一步要做什么,然后您会使用 TwiML 进行响应,例如,当您想要获取信息时使用 <Gather>

    当您进行出站呼叫时,您 initiate the call with the REST API,然后当呼叫连接时,Twilio 向您的 URL 发出 webhook 请求。然后,您可以回复 TwiML 来告诉 Twilio 要做什么,您也可以在此阶段回复 <Gather>

    让我们从outbound call as shown in this documentation 收集输入。

    首先,您购买一个 Twilio 电话号码并使用 Ngrok URL 对其进行配置:这是一个方便的工具,可通过公共 URL 将您的本地服务器打开到网络。当您拨打外线电话时,您会将这个 URL 传递给它:your-ngrok-url.ngrok.io/voice

    from twilio.rest import Client
    account_sid = 'your-account-sid'
    auth_token = 'your-auth-token'
    client = Client(account_sid, auth_token)
    
    call = client.calls.create(
        url='https://your-ngrok-url.ngrok.io/voice',
        to='phone-number-to-call',
        from_='your-twilio-number'
    )
    

    client.calls.create 中的 URL 返回 TwiML,其中包含有关用户接听电话时应该发生的情况的说明。让我们创建一个 Flask 应用程序,其中包含在用户接听电话时运行的代码。

    from flask import Flask, request
    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()
    

    您将通过带有 TwiML Gather 动词的键盘接收用户输入,该动词用于在通话期间收集数字或转录语音。 Action attribute 将绝对或相对 URL 作为值,一旦调用者完成输入数字(或达到超时),Twilio 就会发出 HTTP 请求。该请求包括用户的数据和 Twilio 的标准请求参数。

    如果您从调用者那里收集数字,Twilio 会包含 Digits 参数,其中包含调用者输入的数字。

        gather = Gather(num_digits=1, action='/gather')
        gather.say('For sales, press 1. For support, press 2.')
        resp.append(gather)
    

    如果收件人没有选择选项,我们让他们循环回到开头,这样他们就可以再次听到指示。

        resp.redirect('/voice')
        return str(resp)
    

    但是,如果他们确实选择了一个选项并在键盘中输入了一个数字,Twilio 将使用他们输入的数字向托管您的 TwiML 的 URL 发送一个 POST 请求。这就是您通过接收者按下按钮获取用户输入并将其引导回您的 Python 程序的方式:使用request.values['Digits']。根据该值(在choice 变量中,您可以相应地更新数据库或其他内容,如下面的条件所示。

    @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('You selected sales. Good for you!')
                return str(resp)
            elif choice == '2':
                resp.say('You need support. We will help!')
                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.")
    
        # If the user didn't choose 1 or 2 (or anything), send them back to /voice
        resp.redirect('/voice')
    
        return str(resp)
    

    希望这会有所帮助!

    【讨论】:

    • 哇!感谢您的回答。不会撒谎,现在我要学习 Flask,因为我从未使用过它。看来我至少应该有一个基本的了解。
    • 好的,所以我学会了一点烧瓶。基本上在这里,我制作了一个本地网络服务器。但是我们使用该 ngrok 链接来创建一种返回本地计算机的方法。真的,我应该先设置它。然后,使用用于创建呼叫的其他代码,只有在应答呼叫时才会执行 webhook。 Twilio“访问”我的服务器,并提供了使用收集和语音响应功能制作的 TwiML。这很酷。我会试试这个。
    【解决方案2】:

    @lizziepika - 出色的指导 - 非常感谢!

    我正在尝试做一个实时转录系统,该系统在 twilio 号码和用户之间的 twilio 对话中使用语音到文本和文本到语音。

    1. 我一直在跟踪流式处理,以便接收用户 在我的网络套接字(flask-sockets)下面这个链接的响应 (https://github.com/twilio/media-streams/tree/master/python/realtime-transcriptions)。在这里,我面临一个问题,即何时应该考虑用户拥有 停止说话并停下来等待 twilio 回应。
    2. 如果我使用 twiml gatherinput=speech 并得到 request.values 正如你提到的 在你的回答中,那么需要给出 2 秒的间隔 确定用户已停止交谈并等待 twilio 回复。问题是,只有在这 2 秒之后 系统可以开始处理来自用户的响应。这是一个 我相信响应延迟了很多。

    我应该从上面选择哪个建议选项,以便在 twilio 和用户之间进行近乎正常的对话。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多