### 需要实现的功能有哪些?
最近学着开了一个订阅号,本来就是用来发一些文章,记录一些读书笔记的,后来,又不满足于这些,于是接着微信的开放借口,尝试着写出一些功能.
最初是想学着自动回复,然后发现,自动回复里面的内容太多了,如果真的实现起来,像小黄鸡?像微软小兵一样的智能机器人,如果真的实现起来,就只有去抓去别人的结果了.
归纳起来,最初想实现的是:
接口的认证
自动回复消息
一开始看别人的代码,拷贝过来,然后发现,有点难度.所以就去学习了一下webpy.
c
+---basic application
| +---helloword
| | 01helloworld.py
| | 01helloworld.pyc
| |
| +---notfound
| | | notfound.py
| | | notfound.pyc
| | | notfound02.py
| | | notfound02.pyc
| | |
| | \---templates
| | notfound.html
| |
| \---xmlfile
| | xmlfile.py
| | xmlfile.pyc
| | xmlfiles.png
| |
| \---templates
| response.xml
|
+---db
| | dbV000_select.py
| | dbV000_select.pyc
| | dbV001_add.py
| | dbV001_add.pyc
| | todo.sql
| | 连接问题.txt
| |
| \---templates
| index - 副本.html
| index.html
|
+---form
| | formV000.py
| | formV000.pyc
| | formV001.py
| | formV001.pyc
| | formV002.png
| | formV002.py
| | formV002.pyc
| | formV003.png
| | formV003.py
| | formV003.pyc
| | formV004.py
| | formV004.pyc
| |
| \---templates
| formtest.html
|
+---Templetor
| | 01templetor.py
| | 01templetor.pyc
| | 02templetor-template-as-string.py
| | 03templetor-newline.py
| | 03templetor-newline.pyc
| | 04templetor-escaping .py
| | 04templetor-escaping .pyc
| | 05templetor-control-structures.py
| | 05templetor-control-structures.pyc
| |
| \---templates
| controlStructures.html
| escaping.html
| index.html
| newline.html
|
+---urls
| | codeV000.py
| | codeV001.py
| | codeV001.pyc
| | codeV002.py
| | codeV002.pyc
| |
| \---templates
| index.html
|
\---wiki
| model.py
| model.pyc
| schema.sql
| wiki.py
| wiki.pyc
|
\---templates
base.html
edit.html
index.html
new.html
view.html
学习webpy就照着官网,学习了:
入门教程: http://webpy.org/docs/0.3/tutorial.zh-cn
cookbook: http://webpy.org/cookbook/index.zh-cn
在学习了这些基础的知识之后,实现我们的目标:
就相对容易了很多.
实现认证的代码只需要三个主语的文件:
第一个文件用来处理URL:/weixin 路径用第二个文件weixinInterface类来实现
第三个文件是一些SAE上的配置文件.
遇到的问题有:
问题1: 调试后来在SAE的帮助文档里找到了:
http://sae.sina.com.cn/doc/python/runtime.html#id4
日志系统的使用.
c
101.226.62.85 [20/Jul/2014:18:10:43 +0800] /weixin?signature=b165650640ad0ddf52d84fe6d1a9eec69935f8f4×tamp=1405851012&nonce=734319259 200 0.152 0.152 458
urmyfaith.sinaapp.com POST HTTP/1.0 "-" "Mozilla/4.0" yq34
从日志系统里可以找到HTTP响应码,200,404,405,500 200 正常 404 页面问找到 405 请求方法错误(后来确定是因为POST方法的缩进导致) 500 内部错误(需要到debug里定位错误)
从”日志中心”>”http”>”debug”里可以定位各种错误,很方便修改.
问题2,3
都是因为POST方法缩进错误导致.(缩进错误是因为editplus和python IDLE的缩进不一致导致)
问题4
由于没有导入相应库导致,从debug里可以查看到.
问题5,7,8
某些值的获取错误,从debug里定位,要么是因为结果为空,大小写不对应(sql数据库里的table里的字段名大小写不一致导致.)
问题6
ascii和utf-8的问题也是由于各个系统的编码不一致导致,mysql里的默认为utf-8,然后python读出来ascii编码,需要进行encode,或者是在字符串前加u,例如:u”中国”.
问题11
sae上数据库的读写时遇到两个问题:
删除数据的时候,
c
where_dict={'id':currID,}
db.delete('article',where=web.db.sqlwhere(where_dict))
选择数据查询的时候,
c
myvar=dict(dateInWhere=dateNow)
currLines = db.select('birthday',myvar,where="date >
$dateInWhere",limit=1,order="date ASC")
特别是后面的,myvar的使用,和myvar的位置,以及date的数据库字段的设计的问题,究竟是TEXT,DATE类型的问题,还有选择逻辑的考虑(order by asc),还有冒号里面,究竟用不用$等问题,都需要去尝试才知道怎么做.
解决这些问题的时候,参考
http://webpy.org/cookbook/index.zh-cn
里的[Database 数据库]部分,很有大的帮助.
其次,去百度一下debug的错误,差不多都可以找到答案.(谷歌的原因,唉…)
确实很花时间,需要去学习webpy,熟悉微信API里的接口文档,SAE的调试,SAE数据库的操作,数据库字段的设计,原始数据的生成,数据怎么获得,格式的转换等等.
后续有时间再实现一些小功能吧.
现在实现的功能有:
c
"00":反馈,
"12":分享文章,
"14":笑话(待完成,原始资料木有),
"25":发表情卖萌,
"36":格言(待完成,原始资料木有),
"47":猜你在做什么,
"0110907":班级生日总表,
"birthday":随机挑选一个生日,
"bd":最近要过生日的人,