我正在尝试创建一个演示Yes服务器,它根据TwiML提供的参数返回一个包含自定义文本的URL Say块(是的,POSTing会更好,但我不太确定如何做到这一点)。它的工作原理很像https://www.twilio.com/labs/twimlets/message,只是我想写自己的代码,这样我就可以添加更多的定制。
我从构建Weather by Phone演示开始,因为它在XML中包含定制的文本。
我已经创建了自己的google应用程序引擎,名为gracklevoice,并且我让weatherbyphone示例正常工作。现在,当我试图简化它时,我遇到了麻烦。我的代码如下所示:
import os
import wsgiref.handlers
from google.appengine.ext.webapp import template
from google.appengine.ext import webapp
BASE_URL = "http://gracklevoice.appspot.com/"
def xml_response(handler, page, templatevalues=None):
"""
Renders an XML response using a provided template page and values
"""
path = os.path.join(os.path.dirname(__file__), page)
handler.response.headers["Content-Type"] = "text/xml"
handler.response.out.write(template.render(path, templatevalues))
class GracklePage(webapp.RequestHandler):
def get(self):
self.post()
def post(self):
xml_response(self, 'notification.xml')
def main():
application = webapp.WSGIApplication([ \
('/', GracklePage)],
debug=True)
wsgiref.handlers.CGIHandler().run(application)
if __name__ == "__main__":
main()还有一个yaml文件:
application: gracklevoice
version: 1
runtime: python27
api_version: 1
threadsafe: no
handlers:
- url: /.*
script: gracklevoice.py和notification.xml
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Say voice="alice" language="en-US">
This is a message from Grackle.
</Say>
</Response>这看起来应该很简单,但是当我的客户端应用程序将呼叫url设置为http://gracklevoice.appspot.com/时,我得到的不是语音消息,而是一个错误消息:“很抱歉,发生了一个应用程序错误。再见。”我遗漏了什么?
查看appEngine日志(长度有限),我看到:
2013-11-18 14:45:09.781
Traceback (most recent call last):
E 2013-11-18 14:45:09.781
File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/wsgiref/handlers.py", line 86, in run
E 2013-11-18 14:45:09.781
self.finish_response()
E 2013-11-18 14:45:09.781
File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/wsgiref/handlers.py", line 128, in finish_response
E 2013-11-18 14:45:09.781
self.write(data)
E 2013-11-18 14:45:09.781
File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/wsgiref/handlers.py", line 204, in write
E 2013-11-18 14:45:09.781
assert type(data) is StringType,"write() argument must be string"
E 2013-11-18 14:45:09.781
AssertionError: write() argument must be string发布于 2014-01-14 07:34:18
Twilio开发者布道者在这里。使用unicode()函数转换template.render(path, templatevalues)的输出。您的新代码行将以如下形式结束
handler.response.out.write(unicode(template.render(path, templatevalues)))template.render函数的输出是StringType类型,而不是写函数需要的str类型。使用unicode函数进行强制转换可将输出转换为所需的输入格式。
在this question about issues with StringType and unicode中有更多信息。
https://stackoverflow.com/questions/20058665
复制相似问题