python - 如何使用 Python 从出站 Twilio 调用中检索信息?
问题描述
我是 Twilio 的新手,正在尝试弄清楚如何从我使用 Python 3 成功进行的出站呼叫中检索数据。我希望能够检索诸如从收件人那里按下了什么按钮之类的东西。
在阅读了一点 Twilio 文档(然后有点迷失)之后,我想我了解了 Twilio 的工作原理以及为什么我无法从电话中检索数据。我认为 Python 程序只是建立了从 Twilio 到电话号码的连接。收件人可以拨打任何号码,我可以使用标签获取一些信息。但是如何将这些信息引导到我的 Python 程序?这个想法是让 Twilio(以某种方式)将信息发送回我的 Python 程序,然后我可以采取行动(比如更新数据库)。
我猜 Twilio 会将数据扔到其他地方,然后我的 Python 程序可以去检索这些数据,但我不知道在哪里学习该技能。我有 Python 3 的基本基础,但对 Web 开发了解不多。只是一些基本的 HTML5 和 CSS3。
解决方案
Twilio 开发人员布道者在这里。
您可能已经看过有关在入站呼叫中通过 Python 中的键盘收集用户输入的文档。
当您收到入站呼叫时,Twilio 会发出 webhook 请求以了解下一步要做什么,然后您使用 TwiML 进行响应,例如,<Gather>
当您想要获取信息时。
当您进行出站呼叫时,您使用 REST API 发起呼叫,然后当呼叫连接时,Twilio 会向您的 URL 发出 webhook 请求。然后,您可以使用 TwiML 进行响应以告诉 Twilio 要做什么,并且您也可以<Gather>
在此阶段使用 a 进行响应。
让我们从出站呼叫中收集输入,如本文档中所示。
首先,您购买一个 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'
)
中的 URLclient.calls.create
返回 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 属性将绝对或相对 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)
希望这可以帮助!
推荐阅读
- ag-grid - 如何在 ag-grid vuejs 中保留列过滤器
- excel - 在 Ruby 中,如何从原始 CSV 有条件地创建新的 CSV?
- java - javax.jms.JMSException:无法发布命令:KeepAliveInfo
- google-cloud-platform - 如何从 Google AI Platform 停止/暂停预测节点?
- c# - 对基类中虚方法的所有实现应用相同的覆盖
- jq - 使用 jq 从 JSON 输出中读取密钥
- excel - 为什么我的代码运行时不断收到“对象'范围'失败”错误,但当我将此代码附加到按钮时却没有?
- c++ - 如何判断 C++ 生成了什么模板
- php - PHPMailer 两次发送相同的电子邮件
- r - 在 R 中的 ID 和最接近的日期上合并 data.table