- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章python微信公众号之关键词自动回复由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
最近忙国赛的一个项目,我得做一个微信公众号。功能就是调数据并回复给用户,需要用户发送给公众号一个关键词,通过关键词自动回复消息.
这时就是查询微信公众平台文档了,地址如下: 文档 。
按照它的入门指南,我基本上了解了用户给公众号发送消息的一个机制,并且一旦给公众号发送消息,在开发者后台,会收到公众平台发送的一个xml,所以通过编写Python脚本进行xml的解析与自动发送功能.
如果用户给公众号发送一段text消息,比如“hello”,那么后台就会收到一个xml为:
1
2
3
4
5
6
7
|
<
xml
>
<
ToUserName
>
<![CDATA[公众号]]>
</
ToUserName
>
<
FromUserName
>
<![CDATA[粉丝号]]>
</
FromUserName
>
<
CreateTime
>1460541339</
CreateTime
>
<
MsgType
>
<![CDATA[text]]>
</
MsgType
>
<
Content
>
<![CDATA[hello]]>
</
Content
>
</
xml
>
|
注意这里面有一些标记对于我们开发者来说是非常有用的:ToUserName,FromUserName,MsgType,Content 所以我们只要知道了这些信息,我们就能做到自动回复的功能.
我们发现这个MsgType 为 ‘text'。而微信中的MsgType有“text”(文本)、“image”(图像)、“voice”(语音)、“video”(视频)、“shortvideo”(短视频)、“location”(位置)、“link”(链接)、“event”(事件) 。
首先我们写一个main.py文件 。
main.py 。
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()
|
然后写一个receive.py,作为接受用户发送过来的数据,并解析xml,返回数据的脚本.
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
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'
:
#print('text')
return
TextMsg(xmlData)
elif
msg_type
=
=
'image'
:
return
ImageMsg(xmlData)
elif
msg_type
=
=
'location'
:
#print('location')
return
LocationMsg(xmlData)
elif
msg_type
=
=
'event'
:
#print('event')
return
EventMsg(xmlData)
class
Event(
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
.Eventkey
=
xmlData.find(
'EventKey'
).text
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
class
LocationMsg(Msg):
def
__init__(
self
, xmlData):
Msg.__init__(
self
, xmlData)
self
.Location_X
=
xmlData.find(
'Location_X'
).text
self
.Location_Y
=
xmlData.find(
'Location_Y'
).text
class
EventMsg(Msg):
def
__init__(
self
, xmlData):
Event.__init__(
self
, xmlData)
self
.Event
=
xmlData.find(
'Event'
).text
|
其中,我们使用xml.etree.ElementTree,这是一个简单而有效的用户解析和创建XML数据的API。而fromstring()就是解析xml的函数,然后通过标签进行find(),即可得到标记内的内容.
同时还要写一个reply.py,作为自动返回数据的脚本。 刚才提到了,用户给公众号发送消息,公众号的后台会接收到一个xml,那么如果公众号给用户发送消息呢,其实也就是公众号给用户发送一个xml,只是ToUserName,FromUserName换了一下而已,内容自己定.
1
2
3
4
5
6
7
|
<
xml
>
<
ToUserName
>
<![CDATA[粉丝号]]>
</
ToUserName
>
<
FromUserName
>
<![CDATA[公众号]]>
</
FromUserName
>
<
CreateTime
>1460541339</
CreateTime
>
<
MsgType
>
<![CDATA[text]]>
</
MsgType
>
<
Content
>
<![CDATA[test]]>
</
Content
>
</
xml
>
|
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
|
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)
|
接着我们要写一个handle.py,作为对消息进行反映处理(自动回复)的脚本.
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
import
web
import
reply
import
receive
import
JsonData
import
xml.dom.minidom
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
=
"hello2016"
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 as Argument:
return
Argument
def
POST(
self
):
try
:
webData
=
web.data()
#print(webData)
recMsg
=
receive.parse_xml(webData)
#print(recMsg)
if
isinstance
(recMsg, receive.Msg):
toUser
=
recMsg.FromUserName
fromUser
=
recMsg.ToUserName
if
recMsg.MsgType
=
=
'text'
:
try
:
a
=
JsonData.praserJsonFile()
#print(a)
except
Exception as Argument:
return
Argument
if
a[
'status'
]
=
=
'1'
:
content
=
"No equipment"
else
:
if
a[
'data'
][
0
][
'weather'
]
=
=
'0'
:
israin
=
'7.没有下雨'
else
:
israin
=
'7.下雨'
#print(israin)
content
=
"此设备数据如下:\n"
+
"1.id号为 "
+
a[
'data'
][
0
][
'id'
]
+
"\n"
+
"2.温度为 "
+
a[
'data'
][
0
][
'temp'
]
+
"\n"
+
"3.湿度为 "
+
a[
'data'
][
0
][
'humidity'
]
+
"\n"
+
"4.PM2.5浓度为 "
+
a[
'data'
][
0
][
'pm25'
]
+
"ug\n"
+
"5.PM10浓度为 "
+
a[
'data'
][
0
][
'pm10'
]
+
"\n"
+
"6.光照 "
+
a[
'data'
][
0
][
'illumination'
]
+
"L\n"
+
israin
#content = "%s\n%s %s\n%s %s\n%s %s\n%s %s\n%s %s\n%s" %('环境数据如下:','设备id号为',a['data']['id'],'temp is', a['data']['temp'], 'humidity is', a['data']['humidity'],'PM25 is',a['data']['pm25'],'illumination',a['data']['illumination'],israin)
#print(content)
replyMsg
=
reply.TextMsg(toUser, fromUser, content)
return
replyMsg.send()
if
recMsg.MsgType
=
=
'image'
:
mediaId
=
recMsg.MediaId
replyMsg
=
reply.ImageMsg(toUser, fromUser, mediaId)
return
replyMsg.send()
if
recMsg.MsgType
=
=
'location'
:
location_x
=
recMsg.Location_X
location_y
=
recMsg.Location_Y
content
=
"您所在的位置是在:经度为"
+
location_x
+
";纬度为:"
+
location_y
replyMsg
=
reply.TextMsg(toUser, fromUser, content)
return
replyMsg.send()
if
recMsg.MsgType
=
=
'event'
:
#print('yes')
event
=
recMsg.Event
if
event
=
=
'subscribe'
:
content
=
"欢迎关注,您好!雨燕城市环境小助手微信公众号:发送 获取数据,公众号会自动发送当前环境数据(目前为调试数据,不是真实数据).将要调试GPS,根据手机定位位置与设备位置相关联,取最近距离的设备所获取到的数据并进行返回."
replyMsg
=
reply.TextMsg(toUser, fromUser, content)
return
replyMsg.send()
else
:
return
reply.Msg().send()
else
:
print
(
"not do"
)
return
reply.Msg().send()
except
Exception as Argment:
return
Argment
|
注:代码贴了目前写的所有功能,接收关键字并自动返回数据;关注后自动回复欢迎文字;发送定位获得GPS信息.
那么我怎么样使用微信公众号去调取服务器上的数据呢,因为有了数据的json文件,我们就可以使用Python脚本进行json的解析,然后将数据在content中体现出来就可以了.
Json文件解析 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
import
types
import
urllib.request
import
json
def
praserJsonFile():
url
=
"http://118.89.244.53:8080/index.php/home/api/present_data"
data
=
urllib.request.urlopen(url).read()
value
=
json.loads(data.decode())
#print(value)
#print(value['data'])
return
value
#praserJsonFile()
|
这个value就是我们解析json出来的一个list 。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我.
原文链接:https://blog.csdn.net/karry_zzj/article/details/78451901 。
最后此篇关于python微信公众号之关键词自动回复的文章就讲到这里了,如果你想了解更多关于python微信公众号之关键词自动回复的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
当有人使用微信(微信)分享我的一款游戏(用 JavaScript 制作)时,我正在使用 WeixinJSBridge 修改分享参数。 下面的代码位于一个很大的 JavaScript 文件(超过 250
我转微信了padplus来自wechaty-puppet-puppeteer的傀儡并发现它比 wechaty-puppet-puppeteer 更频繁地停止.即,wechaty-puppet-pupp
微信小程序一出,立马炸开了锅,都去搭建自己的开发环境,我这里也来尝尝先,之前发了一篇文章,有人问demo怎么导入? demo源代码(来自网络) 百度: https://pan.baidu.com
微信小程序可谓是今天最火的一个名词了,一经出现真是轰炸了整个开发人员,当然很多app开发人员有了一个担心,微信小程序的到来会不会给移动端app带来一个寒冬,身为一个android开发者我是不相信的,
memcache缓存存储用户信息7000秒 ? 1
微信开发生成带参数的二维码的讲解 在微信公众号平台开发者那里,在“账号管理”那里,有一项功能是“生成带参数的二维码”,通过这儿生成的二维码,只要通过微信扫一扫之后,会把事件自动推送到微信公众号上
某日,一同学给小的发了 Github 源码,说是可以轻松查到删除自己的微信好友,于是就开始了作死之路。 Github 源码请看:0x5e/wechat-deleted-friends 前言
近段时间山西的连续降雨,不少的城市都出现了洪灾,救灾物资匮乏,不少明星及爱心人士都纷纷向山西捐款,那么目前来说山西捐款的通道有哪些呢?在支付宝,微信上如何给山西捐款呢?下面就和小编一起来看看山西捐款
一.集成支付宝支付 支付宝集成官方教程 https://docs.open.alipay.com/204/105295/ 支付宝集成官方demo https://docs.o
一.越来越多的app增加第三方的功能,可能app有不同的页面但调用相同的支付方式,例如界面如下: 这两个页面都会使用第三方支付支付:(微信,支付宝,银联)如果在每一个页面都直接调用第三方支付的
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 8 年前。 Improve this ques
我安装了一个虚假的位置应用程序并将我的位置设置为不同的位置。然后打开谷歌地图和微信应用, Google map 将我的位置显示为我设置的(假的) 微信应用忽略虚假位置并检测真实位置(如何?) 然后我想
前言 支付分APP支付、H5支付、扫码支付等。app支付一般在app中使用,并且需要集成相应的支付SDK,H5支付多用于网页。如果你的APP不想集成支付SDK,又想实现支付功能,你可以在项目中使用
最近一直在调用微信的api,却发现一直调用不成功,纠结了好久,各方面找教程,找官方,官方里的文档也只是写得很模糊,说是按三步走。 1、申请app_id 2、填写包名3、 获取程序签
和平精英QQ/微信 每日登陆抽奖1~188Q币 活动两个QQ和微信都可以 每天登陆游戏并且玩一局 然后可以去活动界面抽奖Q币 QQ端(需玩一局):http://t.cn/Ai1Jn7Tz
目前,当我使用带有 Link 插件的 TinyMce4.5.1 时。即使我将属性 link_list 设置为 false,我也找不到隐藏默认 URL 选项(#top、#bottom)的方法。除了破
实际上,我正在尝试使用微信为我的 Web 应用程序设置 OAuth 登录。所以,我在微信上创建了一个帐户,并使用了一个测试帐户来无限访问。 因此,在测试帐户配置中,我已成功验证来自微信的 token
不管是腾讯还是新浪,查看他们的API,PHP都是有完整的接口,但对C#支持似乎都不是那么完善,都没有,腾讯是完全没有,新浪是提供第三方的,而且后期还不一定升级,NND,用第三方的动辄就一个类库,各种
和平精英QQ/微信 每日登陆抽奖1~188Q币/现金红包 活动两个QQ和微信都可以 QQ的登录游戏和分享好友即可获得三次抽奖次数 微信登录游戏就可以抽奖 如果没有反应就分享链接出去从分享的链接进
我想做什么 我正在尝试将我自己的基于 WebGL 的引擎移植到微信小游戏环境,目前只是尝试让 WebGL 上下文被粉红色清除: 有什么问题 我已经按照腾讯提供的示例以及 ThreeJS 示例来设置游戏
我是一名优秀的程序员,十分优秀!