0

URL で指定されたパラメーターに基づいて、内部にカスタム テキストを含む TwiML Say ブロックを返すデモ Web サーバーを作成しようとしています (はい、POST の方が良いでしょうが、その方法がよくわかりません)。https://www.twilio.com/labs/twimlets/messageのように機能しますが、カスタマイズを追加できるように独自のコードを記述したい場合を除きます。

xml 内にカスタマイズされたテキストが含まれているため、 Weather by Phoneデモから構築することから始めました。

gracklevoice という独自の Google アプリ エンジンを作成し、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
4

1 に答える 1

0

Twilio 開発者エバンジェリストはこちら。template.render(path, templatevalues)の出力をunicode() 関数でキャストします。新しいコード行は次のようになります

handler.response.out.write(unicode(template.render(path, templatevalues)))

template.render 関数の出力は、書き込み関数が必要とする str 型ではなく、StringType です。unicode 関数を使用してキャストすると、出力が必要な入力形式に変換されます。

この質問には、 StringType と unicode の問題に関する追加情報があります。

于 2014-01-13T23:34:18.267 に答える