简体   繁体   English

如何使用 Python 从出站 Twilio 调用中检索信息?

[英]How to retrieve information from outbound Twilio call with Python?

I am new to Twilio and am trying to figure out how to retrieve data from an outbound call I have successfully made using Python 3. I want to be able to retrieve things like what button has been pressed from the recipient.我是 Twilio 的新手,正在尝试弄清楚如何从我使用 Python 3 成功进行的出站呼叫中检索数据。我希望能够检索诸如从收件人那里按下了什么按钮之类的内容。

After reading the Twilio docs for a little bit (then getting a little lost), I think I understand how Twilio works and why I am not able to retrieve data from a phone call.在阅读了一点 Twilio 文档(然后有点迷失)后,我想我明白了 Twilio 的工作原理以及为什么我无法从电话中检索数据。 I think the Python program merely establishes a connection from Twilio to a phone number.我认为 Python 程序只是建立了从 Twilio 到电话号码的连接。 The recipient can dial any number and I can use the tag to get some info.收件人可以拨打任何号码,我可以使用标签获取一些信息。 But how do I direct that info to my Python program?但是我如何将该信息定向到我的 Python 程序呢? The idea was to have Twilio (somehow) send info back to my Python program and then I can take action (like update a database).这个想法是让 Twilio(以某种方式)将信息发送回我的 Python 程序,然后我可以采取行动(例如更新数据库)。

I am guessing that Twilio will throw data somewhere else which then my Python program can go to to retrieve but I do not understand where to learn that skill.我猜 Twilio 会将数据扔到其他地方,然后我的 Python 程序可以去检索,但我不知道从哪里学习该技能。 I have a basic foundation of Python 3 but not a whole lot on web development.我有 Python 3 的基本基础,但在 Web 开发方面没有很多。 Just some basic HTML5 and CSS3.只是一些基本的 HTML5 和 CSS3。

Twilio developer evangelist here. Twilio 开发人员布道者在这里。

You may have seen this documentation on gathering user input via keypad in Python on an inbound call.您可能已经看过有关在入站呼叫中通过 Python中的键盘收集用户输入的文档

When you get an inbound call, Twilio makes the webhook request to find out what to do next and you respond with TwiML, for example, a <Gather> when you want to take information.当您接到入站呼叫时,Twilio 会发出 webhook 请求以了解下一步要做什么,并且您使用 TwiML 进行响应,例如,当您想要获取信息时使用<Gather>

When you make an outbound call, you initiate the call with the REST API and then when the call connects, Twilio makes a webhook request to your URL.当您进行出站呼叫时,您使用 REST API 发起呼叫,然后当呼叫连接时,Twilio 会向您的 URL 发出 Webhook 请求。 It is then that you can respond with TwiML to tell Twilio what to do and you can respond with a <Gather> at this stage too.然后,您可以使用 TwiML 进行响应以告诉 Twilio 要做什么,并且您也可以在此阶段使用<Gather>进行响应。

Let's gather input from an outbound call as shown in this documentation .让我们从出站呼叫中收集输入, 如本文档中所示

First, you purchase a Twilio phone number and configure it with a Ngrok URL: this is a handy tool that opens up your local server to the web via a public URL.首先,您购买 Twilio 电话号码并使用Ngrok URL 对其进行配置:这是一个方便的工具,可通过公共 URL 将您的本地服务器打开到网络。 When you make an outbound call, you pass it this URL: your-ngrok-url.ngrok.io/voice .当您拨打出站电话时,您会将这个 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'
)

The URL in client.calls.create returns TwiML with instructions on what should happen when the user answers the phone call. client.calls.create的 URL 返回 TwiML,其中包含有关用户接听电话时应该发生的情况的说明。 Let's create a Flask application that contains code to run when the user answers the call.让我们创建一个 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()

You would receive user input via keypad with the TwiML Gather verb which is used to collect digits or transcribe speech during a phone call.您将通过带有 TwiML Gather动词的键盘接收用户输入,该动词用于在电话通话期间收集数字或转录语音。 The Action attribute takes an absolute or relative URL as a value that Twilio makes an HTTP request to once the caller finishes entering digits (or the timeout is reached). Action 属性将绝对或相对 URL 作为值,一旦调用者完成输入数字(或达到超时),Twilio 就会向其发出 HTTP 请求。 That request includes the user's data and Twilio's standard request parameters.该请求包括用户的数据和 Twilio 的标准请求参数。

If you gather digits from the caller, Twilio includes the Digits parameter containing the digits the caller typed in.如果您从呼叫者那里收集数字,Twilio 会包含Digits参数,其中包含呼叫者输入的数字。

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

If the recipient doesn't select an option, let's loop them back to the beginning so they can hear the directions again.如果收件人没有选择选项,让我们将它们循环回到开头,以便他们再次听到方向。

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

However, if they do select an option and type a number into the keypad, Twilio will send a POST request back to the URL hosting your TwiML with the digit they typed.但是,如果他们确实选择了一个选项并在键盘中输入了一个数字,Twilio 将使用他们输入的数字向托管您的 TwiML 的 URL 发送一个 POST 请求。 That is how you get user input via button press from the recipient and direct it back to your Python program: with request.values['Digits'] .这就是您通过按钮从接收者那里获得用户输入并将其引导回您的 Python 程序的方式:使用request.values['Digits'] Based on that value (in the choice variable, you can update a database or something accordingly, as shown in the conditionals below.基于该值(在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)

Hope this helps!希望这可以帮助!

@lizziepika - Excellently guiding - appreciate that ! @lizziepika - 非常有指导意义 - 非常感谢!

I am trying to do a realtime transcribing system that uses speech-to-text and text-to-speech in a twilio conversation between a twilio number and a user.我正在尝试做一个实时转录系统,该系统在 twilio 号码和用户之间的 twilio 对话中使用语音到文本和文本到语音。

  1. I have been following a streaming process so that I receive user responses in my web-socket (flask-sockets) following this link ( https://github.com/twilio/media-streams/tree/master/python/realtime-transcriptions ).我一直在关注流媒体过程,以便通过此链接( https://github.com/twilio/media-streams/tree/master/python/realtime-transcriptions )在我的网络套接字(烧瓶套接字)中收到用户响应)。 Here I face a problem as to when should I consider the user has stopped talking and pausing for twilio to respond.在这里,我面临一个问题,即我应该何时考虑用户已停止说话并暂停以等待 twilio 响应。
  2. If I use twiml gather with input=speech and get request.values as you mentioned in your answer, then the gap of 2 seconds need to be given for identifying that the user has stopped talking and waiting for twilio to respond.如果我使用带有input=speech twiml gather并获取您在回答中提到的request.values ,则需要给出 2 秒的间隔来识别用户已停止说话并等待 twilio 响应。 The problem with this is, only after this 2 seconds the system can start processing the response from the user.问题在于,只有在这 2 秒后,系统才能开始处理来自用户的响应。 This is a much delayed response I believe.我相信这是一个非常延迟的响应。

Which is the suggested option I should choose from the above so that it gives a near normal conversation between twilio and the user.这是我应该从上面选择的建议选项,以便在 twilio 和用户之间进行接近正常的对话。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Python 从出站 Twilio 调用中检索信息并将其放入谷歌语音到文本中? - How to retrieve information from outbound Twilio call with Python and put it into google speech to text? 如何使用 Twilio 进行出站 SIP 呼叫? - how can I make an outbound SIP call using Twilio? 如何在 twilio 中使用 python 在入站短信中捕获用户没有响应,然后退出入站,然后发送出站短信? - How to capture no response from user in inbound sms with python in twilio to then exit the inbound and then send an outbound sms message? Twilio Python:如何通过 twilio 呼叫多个呼叫 - Twilio Python: How to call Multiple calls by twilio 如何使用Python为Twilio API提供代理信息 - How to provide proxy information to Twilio API with Python 如何在python中检索USB信息 - How to retrieve usb information in python Twilio 从 python 发起呼叫并讲话 - Twilio initiate a call from python and speak 如何使用 Python 根据日期和术语从 Pubmed 中检索信息? - How to retrieve information from Pubmed according to date and term using Python? Python:如何从“框架”object 中检索 class 信息? - Python: How to retrieve class information from a 'frame' object? 如何使用python子进程从curl中检索信息 - How to retrieve information from curl using python subprocess
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM