joe*_*joe 3 python call outbound 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'
)
Run Code Online (Sandbox Code Playgroud)
中的 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()
Run Code Online (Sandbox Code Playgroud)
您将通过带有 TwiML Gather动词的键盘接收用户输入,该动词用于在电话通话期间收集数字或转录语音。的行动属性需要的绝对或相对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)
Run Code Online (Sandbox Code Playgroud)
如果收件人没有选择选项,让我们将它们循环回到开头,以便他们再次听到方向。
resp.redirect('/voice')
return str(resp)
Run Code Online (Sandbox Code Playgroud)
但是,如果他们确实选择了一个选项并在键盘中输入了一个数字,Twilio 将使用他们输入的数字将 POST 请求发送回托管您的 TwiML 的 URL。这就是您通过按钮从接收者那里获得用户输入并将其引导回您的 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)
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助!