Python-twilio API错误?

2 投票
2 回答
596 浏览
提问于 2025-04-17 03:19

我正在使用twilio-python,并且在跟着这个教程:

http://readthedocs.org/docs/twilio-python/en/latest/usage/twiml.html?highlight=nest

但是当我尝试这个代码:

from twilio import twiml

r = twiml.Response()
r.say("hello")
with r.gather(finishOnKey=4) as g:
    g.say("world")
print str(r)

但是我得到了这个错误:

AttributeError: __exit__

有人知道怎么解决吗?

2 个回答

2

Emmet - 你真厉害。

Matt - 你的回答太完美了。

我们刚刚发布了3.3.2版本,修复了这个问题。你可以在PyPi上下载,或者在这里的GitHub仓库找到:

https://github.com/twilio/twilio-python

只需更新你的模块,按照文档中的方法就能正常使用了。如果有问题,请发邮件到我个人资料里的邮箱 - 你们刚刚赢得了一件Twilio的T恤。:)

2

看起来它们和 with 语句不是很兼容。你可以试试这个:

from twilio import twiml

r = twiml.Response()
r.say("hello")
g = r.gather(finishOnKey=4)
g.say("world")
print str(r)

这是我得到的结果:

>>> from twilio import twiml
>>> 
>>> r = twiml.Response()
>>> r.say("hello")
<twilio.twiml.Say object at 0x1098d05d0>
>>> g = r.gather(finishOnKey=4)
>>> g.say("world")
<twilio.twiml.Say object at 0x1098d0950>
>>> print str(r)
<?xml version="1.0" encoding="UTF-8"?><Response><Say>hello</Say><Gather finishOnKey="4"><Say>world</Say></Gather></Response>

撰写回答