web侠客行(十七)--微信公众号开发教程

python

官方文档使用的python,当然也可以使用nginx,apache,jvm(springboot)

python 版本2.7,linux默认安装2.7版本。

安装 web.py

1
easy_install web.py

写服务端程序

main.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# -*- coding: utf-8 -*-
# filename: main.py
import web

urls = (
'/wx', 'Handle',
)

class Handle(object):
def GET(self):
return "hello, this is handle view"

if __name__ == '__main__':
app = web.application(urls, globals())
app.run()

启动web服务

1
2
3
4
5
// 默认8080端口
python main.py

// 启动80端口
python main.py 80

验证服务端

1
http://139.9.2.241:8080/wx

python web 深入学习

参考文章:https://blog.csdn.net/qq_22194315/article/details/79114533

webpy官方:http://webpy.org/install.zh-cn

code.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import web
from web import form

render = web.template.render('templates/')

urls = ('/', 'index')
app = web.application(urls, globals())

myform = form.Form(
form.Textbox("boe"),
form.Textbox("bax",
form.notnull,
form.regexp('\d+', 'Must be a digit'),
form.Validator('Must be more than 5', lambda x:int(x)>5)),
form.Textarea('moe'),
form.Checkbox('curly'),
form.Dropdown('french', ['mustard', 'fries', 'wine']))

class index:
def GET(self):
form = myform()
# make sure you create a copy of the form by calling it (line above)
# Otherwise changes will appear globally
print(form.render())
return render.formtest(form)

def POST(self):
form = myform()
if not form.validates():
print(form.render())
return render.formtest(form)
else:
# form.d.boe and form['boe'].value are equivalent ways of
# extracting the validated arguments from the form.
return "Grrreat success! boe: %s, bax: %s" % (form.d.boe, form['bax'].value)

if __name__=="__main__":
web.internalerror = web.debugerror
app.run()

templates/formtest.html

1
2
3
4
5
6
7
8
9
$def with (form)

<div align="center">
<form name="main" method="post">
$if not form.valid: <p class="error">Try again, AmeriCAN:</p>
$:form.render()
<input type="submit" />
</form>
<div>

微信公众号服务器基本配置

改写main.py

增加Handle

1
2
3
4
5
6
7
8
9
10
11
12
# -*- coding: utf-8 -*-
# filename: main.py
import web
from handle import Handle

urls = (
'/wx', 'Handle',
)

if __name__ == '__main__':
app = web.application(urls, globals())
app.run()

新增handle.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# -*- coding: utf-8 -*-
# filename: handle.py

import hashlib
import web

class Handle(object):
def GET(self):
try:
data = web.input()
if len(data) == 0:
return "hello, this is handle view"
signature = data.signature
timestamp = data.timestamp
nonce = data.nonce
echostr = data.echostr
token = "hundsunsoft" #请按照公众平台官网\基本配置中信息填写

list = [token, timestamp, nonce]
list.sort()
sha1 = hashlib.sha1()
map(sha1.update, list)
hashcode = sha1.hexdigest()
print "handle/GET func: hashcode, signature: ", hashcode, signature
if hashcode == signature:
return echostr
else:
return ""
except Exception, Argument:
return Argument

token为hundsunsoft

1
2
3
URL: http://139.9.2.241
Token: hundsunsoft
EncodingAESKey:x7bluXURDvLSYfQdW58Ch6nO3QVdbdjG76OfWtdjeyb

点击提交,显示提交成功即可。

微信公众号实现你问我答

代码改写

改写handle.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# -*- coding: utf-8 -*-
# filename: handle.py
import hashlib
import reply
import receive
import web

class Handle(object):
def POST(self):
try:
webData = web.data()
print "Handle Post webdata is ", webData
#后台打日志
recMsg = receive.parse_xml(webData)
if isinstance(recMsg, receive.Msg) and recMsg.MsgType == 'text':
toUser = recMsg.FromUserName
fromUser = recMsg.ToUserName
content = "test"
replyMsg = reply.TextMsg(toUser, fromUser, content)
return replyMsg.send()
else:
print "暂且不处理"
return "success"
except Exception, Argment:
return Argment

receive.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# -*- coding: utf-8 -*-# filename: receive.py
import xml.etree.ElementTree as ET
def parse_xml(web_data):
if len(web_data) == 0:
return None
xmlData = ET.fromstring(web_data)
msg_type = xmlData.find('MsgType').text
if msg_type == 'text':
return TextMsg(xmlData)
elif msg_type == 'image':
return ImageMsg(xmlData)

class Msg(object):
def __init__(self, xmlData):
self.ToUserName = xmlData.find('ToUserName').text
self.FromUserName = xmlData.find('FromUserName').text
self.CreateTime = xmlData.find('CreateTime').text
self.MsgType = xmlData.find('MsgType').text
self.MsgId = xmlData.find('MsgId').text

class TextMsg(Msg):
def __init__(self, xmlData):
Msg.__init__(self, xmlData)
self.Content = xmlData.find('Content').text.encode("utf-8")

class ImageMsg(Msg):
def __init__(self, xmlData):
Msg.__init__(self, xmlData)
self.PicUrl = xmlData.find('PicUrl').text
self.MediaId = xmlData.find('MediaId').text

reply.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# -*- coding: utf-8 -*-# filename: reply.py
import time

class Msg(object):
def __init__(self):
pass
def send(self):
return "success"

class TextMsg(Msg):
def __init__(self, toUserName, fromUserName, content):
self.__dict = dict()
self.__dict['ToUserName'] = toUserName
self.__dict['FromUserName'] = fromUserName
self.__dict['CreateTime'] = int(time.time())
self.__dict['Content'] = content
def send(self):
XmlForm = """
<xml>
<ToUserName><![CDATA[{ToUserName}]]></ToUserName>
<FromUserName><![CDATA[{FromUserName}]]></FromUserName>
<CreateTime>{CreateTime}</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[{Content}]]></Content>
</xml>
"""
return XmlForm.format(**self.__dict)

class ImageMsg(Msg):
def __init__(self, toUserName, fromUserName, mediaId):
self.__dict = dict()
self.__dict['ToUserName'] = toUserName
self.__dict['FromUserName'] = fromUserName
self.__dict['CreateTime'] = int(time.time())
self.__dict['MediaId'] = mediaId
def send(self):
XmlForm = """
<xml>
<ToUserName><![CDATA[{ToUserName}]]></ToUserName>
<FromUserName><![CDATA[{FromUserName}]]></FromUserName>
<CreateTime>{CreateTime}</CreateTime>
<MsgType><![CDATA[image]]></MsgType>
<Image>
<MediaId><![CDATA[{MediaId}]]></MediaId>
</Image>
</xml>
"""
return XmlForm.format(**self.__dict)

运行 python main.py 80 启动服务

获取access_token

获取密码,设置服务器白名单,139.9.2.241

使用get方法获取access_token

https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET

APPID=wx95c7d4b8439da02f
SECRET=5951b9dfc78dcdbd7400c0a5534df08b

组合起来,服务器上运行

curl ‘https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx95c7d4b8439da02f&secret=5951b9dfc78dcdbd7400c0a5534df08b'

取得access_token

在线测试

浏览器登陆 https://mp.weixin.qq.com/debug/

1
2
3
4
接口类型:向用户发送消息
接口列表:文本消息
access_token: 填入上一步取得的access_token
body: {"msg":"helloworld!你好,世界!"}

body必须发送json报文,点击 检查问题,调试程序。
返回信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
向用户发送消息: 发送客服消息接口 /message/custom/send
请求地址:
https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=hundsunsoft
返回结果:
200 OK
Connection: keep-alive
Date: Mon, 10 Jun 2019 09:25:50 GMT
Content-Type: application/json; encoding=utf-8
Content-Length: 110
{
"errcode": 48001,
"errmsg": "api unauthorized hint: [34zi4a01398964!]"
}
报错:没权限
48001 接口功能未授权,请确认公众号已获得该权限

到接口权限列表中查询使用接口是否有权限

1
https://mp.weixin.qq.com/advanced/advanced?action=table&token=1352603894&lang=zh_CN

微信接口

获取access_token

https请求方式: GET

1
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET

要求填入APPID和APPSECRET

-->