在 Python 中使用 Twilio API 进行出站调用时,我必须始终使用“url”字段吗?

Must I always use `url` field in making outbound calls with Twilio API in Python?

我一直在想办法在检测到答录机时如何拨打电话以留下语音信箱。我仍处于探索的初期,并一直在尝试遵循像 this 这样的 Twilio 示例。我想使用 Python,所以我从下面的 Twilio 中复制粘贴示例:

# Download the helper library from https://www.twilio.com/docs/python/install
from twilio.rest import Client


# Your Account Sid and Auth Token from twilio.com/console
# DANGER! This is insecure. See http://twil.io/secure
account_sid = 'your account sid'
auth_token = 'your_auth_token'
client = Client(account_sid, auth_token)

call = client.calls.create(
                        url='http://demo.twilio.com/docs/voice.xml',
                        to='+1555123456',
                        from_='+1501123456'
                    )

print(call.sid)

在上面的代码中,似乎 Twilio 的 Python SDK 总是期望 url(公开可用 XML file/response)作为 ping 到的资源。我想知道我是否可以构建一个有效的 XML (TwiML) 文件并在上面的 client.calls.create(...) 调用中引用它。换句话说,如何使 Twilio 'speak' 我在 本地(在我的计算机上)XML 文件 中设计的内容(如果可能的话)?我只打算根据需要从我的个人计算机 运行 我的 Python 脚本,我无法访问任何地方的服务器。

提前感谢您的answer/suggestion!

好吧,您可以使用 python 的简单 http 服务器对此进行测试。并让它为该文件提供服务器。

curl http://demo.twilio.com/docs/voice.xml -o voice.xml
python -m SimpleHTTPServer 
#run to above commands then
#try using bellow?
call = client.calls.create(
                    url='http://localhost:8000/voice.xml',
                    to='+1555123456',
                    from_='+1501123456'
                )
#the file at that location is dead simple
<Response>
<Say voice="alice">Thanks for trying our documentation. Enjoy!</Say>
<Play>http://demo.twilio.com/docs/classic.mp3</Play>
</Response>

需要public可访问URL。如果您希望 Twilio 托管您的逻辑,则可以使用 Twilio Studio or Twilio Serverless Functions,这样您就不需要公开 public URL.

对于开发,您可以使用一个名为 Ngrok 的工具,它将 Twilio TwiML 请求传送到您的私有应用程序。

您可以在此处找到更多详细信息。

How to test webhooks locally with ngrok - Twilio Tip #6