首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过手机扩展Twilio Weather创建python TwiML服务器

问通过手机扩展Twilio Weather创建python TwiML服务器
EN

Stack Overflow用户
提问于 2013-11-19 05:49:41
回答 1查看 225关注 0票数 0

我正在尝试创建一个演示Yes服务器,它根据TwiML提供的参数返回一个包含自定义文本的URL Say块(是的,POSTing会更好,但我不太确定如何做到这一点)。它的工作原理很像https://www.twilio.com/labs/twimlets/message,只是我想写自己的代码,这样我就可以添加更多的定制。

我从构建Weather by Phone演示开始,因为它在XML中包含定制的文本。

我已经创建了自己的google应用程序引擎,名为gracklevoice,并且我让weatherbyphone示例正常工作。现在,当我试图简化它时,我遇到了麻烦。我的代码如下所示:

代码语言:javascript
复制
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文件:

代码语言:javascript
复制
application: gracklevoice
version: 1
runtime: python27
api_version: 1
threadsafe: no

handlers:
- url: /.*
  script: gracklevoice.py

和notification.xml

代码语言:javascript
复制
<?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日志(长度有限),我看到:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2014-01-14 07:34:18

Twilio开发者布道者在这里。使用unicode()函数转换template.render(path, templatevalues)的输出。您的新代码行将以如下形式结束

代码语言:javascript
复制
handler.response.out.write(unicode(template.render(path, templatevalues)))

template.render函数的输出是StringType类型,而不是写函数需要的str类型。使用unicode函数进行强制转换可将输出转换为所需的输入格式。

在this question about issues with StringType and unicode中有更多信息。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20058665

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档