SAE 第一个响应
现在我们要做的,就是让用户发了信息之后,可以得到回应。回应的内容,暂且就是 "hello" 吧,刚开始写死就可以了。
我们目前只考虑普通的文本消息的回复,对应的文档在:
要回复的内容:
<xml> <ToUserName><![CDATA[ov_QzuF0iskLIXqu0r71qOLmZV6B]]></ToUserName> <FromUserName><![CDATA[gh_b47caeadeeb7]]></FromUserName> <CreateTime>12345678</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[hello]]></Content> </xml>
注意里面的 ToUserName 和 FromUserName 用前面的内容换一下。
现在的 index.wsgi :
# -*- coding: utf-8 -*-
import re
import time
from sae.storage import Bucket
def application(environ, start_response):
if environ.get('REQUEST_METHOD', 'GET') == 'GET':
q = environ.get('QUERY_STRING')
m = re.findall('echostr=(.*)', q)[0]
s = m.split('&', 1)[0]
start_response('200 ok', [('content-type', 'text/plain')])
return [s]
length = environ.get('CONTENT_LENGTH', 0)
length = int(length)
body = environ['wsgi.input'].read(length)
bucket = Bucket('log')
bucket.put_object('%s.txt' % int(time.time()), body)
start_response('200 ok', [('content-type', 'text/plain')])
s = '''<xml>
<ToUserName><![CDATA[ov_QzuF0iskLIXqu0r71qOLmZV6B]]></ToUserName>
<FromUserName><![CDATA[gh_b47caeadeeb7]]></FromUserName>
<CreateTime>12345678</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[hello]]></Content>
</xml>'''
return [s]
s 的第一行不要空行。(我记得空行好像会出错)
提交代码到 SAE,现在向测试账号发消息,就可以得到一个 hello 的回应了。