gpt4 book ai didi

python - 发起调用、录音并回放录音

转载 作者:太空宇宙 更新时间:2023-11-03 16:05:43 26 4
gpt4 key购买 nike

我正在为想要进行基于电话的测试的客户整理一个 POC。在 POC 中,我们只是想让用户在网页上输入电话号码。然后我们会显示一个问题并调用他们的电话号码。我们会记录他们对问题的回答并向他们播放。

我可以发起通话,但不知道如何表明我想要录音。理想情况下,我想说些什么并在嘟嘟声后开始录音。

我有 3 个小时的 Twilio 经验,所以请原谅我的无知。

这是迄今为止我的代码:

import logging

# [START imports]
from flask import Flask, render_template, request
import twilio.twiml
from twilio.rest import TwilioRestClient
# [END imports]

app = Flask(__name__)


# [START form]
@app.route('/form')
def form():
return render_template('form.html')
# [END form]


# [START submitted]
@app.route('/submitted', methods=['POST'])
def submitted_form():
phone = request.form['phone']

account_sid = "AC60***********************"
auth_token = "27ea************************"
client = TwilioRestClient(account_sid, auth_token)
call = client.calls.create(to=phone, # Any phone number
from_="+160#######", # Must be a valid Twilio number
url="https://my-host/static/prompt.xml")

call.record(maxLength="30", action="/handle-recording")

return render_template(
'submitted_form.html',
phone=phone)
# [END render_template]

@app.route("/handle-recording", methods=['GET', 'POST'])
def handle_recording():
"""Play back the caller's recording."""

recording_url = request.values.get("RecordingUrl", None)

resp = twilio.twiml.Response()
resp.say("Thanks for your response... take a listen to what you responded.")
resp.play(recording_url)
resp.say("Goodbye.")
return str(resp)

@app.errorhandler(500)
def server_error(e):
# Log the error and stacktrace.
logging.exception('An error occurred during a request.')
return 'An internal error occurred.', 500
# [END app]

最佳答案

这里是 Twilio 开发者布道者。

当您创建调用时,您可以将 URL 传递给调用。当用户接听电话时,将调用该 URL。对该请求的响应应该是 TwiML指示 Twilio say the messagerecord响应。就像这样:

@app.route("/handle-call", methods=['GET', 'POST'])
def handle_call():
resp = twilio.twiml.Response()
resp.say("Please leave your message after the beep")
resp.record(action="/handle-recording", method="POST")
return str(resp)

然后您只需更新您的调用创建以指向该 URL

call = client.calls.create(to=phone,  # Any phone number
from_="+160#######", # Must be a valid Twilio number
url="https://my-host/handle-call")

您的 /handle-recording 路径看起来好像它已经可以完成您想要的操作。

只是一个快速提示,因为您是 Twilio 的新手,所以在使用 Webhooks 进行开发时,我建议使用 ngrok隧道到您的开发机器并将您的应用程序公开给 Twilio。我写了一个blog post about how to use ngrok以及一些我也喜欢的功能。

请告诉我这是否有帮助。

关于python - 发起调用、录音并回放录音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39824121/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com