概述:

  本文主要分享一下博主在学习wxpy 的过程中开发的一个小程序。博主在最近有一个监控报警的需求需要完成,然后刚好在学习wxpy 这个东西,因此很巧妙的将工作和学习联系在一起。

  博文中主要使用到的技术设计到Python,Redis,以及Java。涉及到的技术看似很多,但是主要的语言是基于Python进行开发的。

  架构涉及主要采用了 生产者消费者的涉及模式,使用Redis作为消息队列进行解耦操作。

  主要架构涉及如下:

  

  接下来开始介绍一下程序的实现过程,主要讲解wxpy -> python.redis -> Java.redis

1、Wxpy初体验

  项目使用的python 是3.5版本的,因此语法会和2.x版本有所区别,wxpy 支持python3.4-3.6 以及python2.7版本 ,因此在python版本上不用太过于纠结

  1.1 安装wxpy

    在这里默认大家以及安装好了pip,我们需要安装wxpy 以及wechat_sender 两个包,这里推荐使用国内的豆瓣源,如果大家网速过硬 请忽略。。。。

pip install wxpy -i "https://pypi.doubanio.com/simple/"
pip install wechat_sender -i "https://pypi.doubanio.com/simple/"

  

  1.2 wxpy 登陆

    wxpy 使用起来非常简单,我们只需要创建一个bot 对象,程序运行后,会弹出二维码,扫描二维码后显示登陆成功。

    下述代码在登陆完成后,会向我们的文件传输助手发送一个“hello world!”。(每个程序都需要一个hello world)

from wxpy import *bot=Bot()
bot.file_helper.send('hello world!')print("ending")

    关于Bot()对象的相关参数说明,我们可以在源码中的注释中看到:

""":paramcache_path:*设置当前会话的缓存路径,并开启缓存功能;为`None` (默认)则不开启缓存功能。*开启缓存后可在短时间内避免重复扫码,缓存失效时会重新要求登陆。*设为`True`时,使用默认的缓存路径'wxpy.pkl':paramconsole_qr:*在终端中显示登陆二维码,需要安装pillow模块(`pip3 install pillow`)*可为整数(int),表示二维码单元格的宽度,通常为2 (当被设为`True`时,也将在内部当作2)*也可为负数,表示以反色显示二维码,适用于浅底深字的命令行界面。*例如:在大部分Linux系统中可设为`True`2,而在macOS Terminal的默认白底配色中,应设为-2:paramqr_path:保存二维码的路径:paramqr_callback:获得二维码后的回调,可以用来定义二维码的处理方式,接收参数: uuid, status, qrcode:paramlogin_callback:登陆成功后的回调,若不指定,将进行清屏操作,并删除二维码文件:paramlogout_callback:登出时的回调"""

    这里介绍一下两个主要使用到的参数:

cache_path: 在开发过程中可以设置为True 避免每次登陆都需要重新扫描,具有缓存的作用。
qr_path:用于保存二维码生成图片,主要解决服务器图片展示不方便的问题

    

  

  1.3 wxpy 好友与聊天群

    如代码所示,我们可以通过Bot.friends 以及Bot.groups 来获取到所有的好友以及聊天群,这里需要注意的是,聊天群需要保存到通讯录中,不然可能会出现找不到聊天群的情况。

    在搜索方法中,可以提供的参数有:姓名,city,province,sex 等相关变量。

    关于好友的详细API文档,可以参考---》 微信好友API

from wxpy import *bot=Bot()#获取所有好友
friends =bot.friends()#遍历输出好友名称
for friend infriends:print(friend)#找到好友
friend = bot.friends.search('被单')[0]print(friend)
friend.send("hello world!")#获取所有聊天群
groups =bot.groups()for group ingroups:print(group)#找到目标群
group = groups.search("409")[0]
group.send("hello world!")

    

  1.4 wxpy 消息处理

    接下来主要介绍一下用户发送消息的类型,目前wxpy 支持发送文本,图片,视频以及文件。主要的发送方式如代码所示:

    这里比较重要的就是关于 @bot.register() 的使用,该注释主要用于注册消息接收器,我们可以根据特定的需求,配置不一样的消息接收器。

    Bot.register(chats=None,msg_types=None,except_self=True,run_async=True,enabled=True) 详情可以查看源码中的介绍

    关于消息处理API,读者可以在该地址下查看详细的配置,这里不做过多的描述。

    代码中有使用到:embed() 这个方法, 主要用于阻塞进程,避免由于程序运行结束导致无法接收消息。

from wxpy import *bot=Bot()#获取好友
my_friend = bot.friends().search('被单')[0]#搜索信息
messages = bot.messages.search(keywords='测试', sender=bot.self)for message inmessages:print(message)#发送文本
my_friend.send('Hello, WeChat!')#发送图片
my_friend.send_image('my_picture.png')#发送视频
my_friend.send_video('my_video.mov')#发送文件
my_friend.send_file('my_file.zip')#以动态的方式发送图片
my_friend.send('@img@my_picture.png')#发送公众号
my_friend.send_raw_msg(#名片的原始消息类型
raw_type=42,#注意 `username` 在这里应为微信 ID,且被发送的名片必须为自己的好友
raw_content='<msg username="wxpy_bot" nickname="wxpy 机器人"/>')#消息接收监听器
@bot.register()defprint_others(msg):#输出监听到的消息
print(msg)#回复消息
msg.reply("hello world")
embed()

  1.4 wxpy 图灵机器人

    wxpy 接入图灵机器人相当方便,我们首先需要到图灵近期人官网进行注册,哆啦A梦的任意门。

    通过注册Tuling 对象,当我们接收到消息的时候,可以直接使用tuling机器人来帮我们进行答复。其他的业务需求各位可以根据自己的需求来完成相应的逻辑。

from wxpy import *bot=Bot()#获取好友
dear = bot.friends().search('被单')[0]#注册获得个人的图灵机器人key 填入
tuling = Tuling(api_key='******')#使用图灵机器人自动与指定好友聊天
@bot.register(dear)defreply_my_friend(msg):print(msg)
tuling.do_reply(msg)
embed()

  1.5 wechat_sender

    在熟悉了wxpy 的相关操作后,我们接下来介绍一下一个主要使用到的工具。由于wxpy 的设计,导致了一些业务操作并不好进行实现。因此我们在这里引入一个工具类:wechat_sender 。

    首先我们需要像往常一样进行微信登陆,然后使用 listen() 进行对我们的 bot() 对象进行监听。

    在这里我们可以看到了和上面代码的区别,这里使用的是listen(),上面是使用embed()进行监听。 我们再这里使用listen 进行监听对象后,可以设置相应的配置。监听默认设置的接收对象为self.file_helper,通过设置receivers 可以配置消息的接收者。

# login.pyfrom wxpy import *
from wechat_sender import *bot=Bot()
friend= bot.friends().search('被单')[0]
listen(bot, token='test', receivers=[friend])
#sender.py  coding: utf-8
from wechat_sender importSender
sender= Sender(token='test')
sender.send('hello world!')

    在别的python 文件中,我们只需要创建一个Sender() 对象,然后调用Sender.send()方法,即可对我们设定好的消息接收者发送消息。

    Sender()在创建的时候可以通过特定的参数设定,比如这里使用了 token 用于避免多个listen 导致sender 混淆。还可以在sender中设置receiver 从listen 中选取需要接收消息的对象。

  1.6 wxpy 在监控模块的代码实现

微信登陆模块:

from wechat_sender import *
from wxpy import *bot= Bot(qr_path="qr.png")
group= bot.groups().search('监控报警')[0]print("微信登陆成功!进行监控报警功能!")print(group)#listen(bot, token='test', receivers=[group])

业务处理模块:

importredisfrom wechat_sender import *sender= Sender(token='test', receivers='监控报警')
while true:# do anything
sender.send(message=data)
# do anythingp.unsubscribe('cardniu-monitor')print('取消订阅')

  

2、Python-Redis

    这一模块我们将简单描述一下python 对于Redis 的支持,首先我们需要安装python-redis相关模块:

  2.1 Python-redis安装

  • 下载压缩包:哆啦A梦的任意门
  • 解压进入 Redis 目录
  • 命令行执行: python setup.py install

  2.2 Python 简单操作Redis

    由于Python 操作Redis 并不是我们这里的主要内容,所以这里简单的过一下Python 对Redis 的支持。

importredis
r= redis.Redis(host='ip', port=6379, db=15, password='****')
r.set('name', 'Jaycekon')
value= r.get('name')print(value)

  2.3 Redis的发布订阅模式

    在为大家讲解Redis 的发布订阅模式前,先为大家科普一下生产者消费者模式:

    大家来领略一下我的灵魂画图,生产者消费者的核心思想是通过一个冰箱来进行解耦,就是我们的厨师不需要出厨房,顾客也不需要去厨房拿饭吃。通过一个冰箱来进行中间的解耦合。

     下面是我们通过python 实现的一个生产者消费者模式,厨师不停的做饭,顾客不停的吃。。大家相互不影响。

from threading importThread
queues= queue.Queue(10)classProducer(Thread):defrun(self):whileTrue:
elem= random.randrange(9)
queues.put(elem)print("厨师 {} 做了 {} 饭 --- 还剩 {} 饭没卖完".format(self.name, elem, queues.qsize()))
time.sleep(random.random())classConsumer(Thread):defrun(self):whileTrue:
elem=queues.get()print("吃货{} 吃了 {} 饭 --- 还有 {} 饭可以吃".format(self.name, elem, queues.qsize()))
time.sleep(random.random())defmain():for i in range(3):
p=Producer()
p.start()for i in range(2):
c=Consumer()
c.start()if __name__ == '__main__':
main()

    

    再来说一下为什么使用到Redis 的发布订阅模式。

    Redis在当前程序中,主要担当了一个消息队列的角色,我们并没有使用目前较为热门的RabbitMq,ActiveMq来消息队列进行解耦。主要原因在于我们的服务不大,消息量也比较小,因此在不影响程序的架构基础上,采用了Redis 作为消息队列。

    消息队列的关键点在于,当生产者发布消息后,要确保消费者能够快速的接收消息。发布订阅模式能够很好的帮我们解决,当有消息到达的时候,程序马上能够做出响应操作。

    Redis消息发布:

importredis
pool= redis.ConnectionPool(host='ip', port=6379, db=4, password='****')
r= redis.StrictRedis(connection_pool=pool)whileTrue:
inputs= input("publish:")
r.publish('spub', inputs)if inputs == 'over':print('停止发布')break

   Redis消息订阅:

importredispool= redis.ConnectionPool(host='ip', port=6379, db=4, password='***')
r= redis.StrictRedis(connection_pool=pool)
p=r.pubsub()
p.subscribe('cardniu-monitor')for item inp.listen():print(item)if item['type'] == 'message':
data= item['data']print("消息队列中接收到信息:", data)if item['data'] == 'over':breakp.unsubscribe('cardniu-monitor')print('取消订阅')

  2.4 wxpy+Redis 实现监控系统的消费者

    最终,在python 这边实现的监控系统消费者如下:

    微信登陆模块:

from wechat_sender import *
from wxpy import *bot= Bot(qr_path="qr.png")
group= bot.groups().search('监控报警')[0]print("微信登陆成功!进行监控报警功能!")print(group)#listen(bot, token='test', receivers=[group])

    Redis消息订阅模块:

importredisfrom wechat_sender import *sender= Sender(token='test', receivers='监控报警')
pool= redis.ConnectionPool(host='10.201.3.18', port=6379, db=4, password='kntest%pw_@dk2')
r= redis.StrictRedis(connection_pool=pool)
p=r.pubsub()
p.subscribe('cardniu-monitor')for item inp.listen():print(item)if item['type'] == 'message':
data= item['data']print("消息队列中接收到信息:", data)    
sender.send(message=data)if item['data'] == 'over':breakp.unsubscribe('cardniu-monitor')print('取消订阅')

3、Java-Redis

    最后,在生产者这块,即是我们监控系统的核心部分,当我们的Java系统出现异常时,我们即可向Redis发送消息,最后由消费者那一边完成消息的发送。

    在下面会跟大家简单讲解一下生产者这边的代码,但是由于代码设计公司内容,因此不做过多的描述。

    Spring-redis.xml

<?xml version="1.0" encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:util="http://www.springframework.org/schema/util"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
<!--redis连接池的配置-->
<beanid="jedisPoolConfig"class="redis.clients.jedis.JedisPoolConfig">
<propertyname="maxTotal"value="${redis.pool.maxTotal}" />
<propertyname="maxIdle"value="${redis.pool.maxIdle}" />
<propertyname="minIdle"value="${redis.pool.minIdle}" />
<propertyname="maxWaitMillis"value="${redis.pool.maxWaitMillis}" />
<propertyname="testOnBorrow"value="${redis.pool.testOnBorrow}" />
<propertyname="testOnReturn"value="${redis.pool.testOnReturn}" />
</bean>
<beanid="sentinelJedisPool"class="redis.clients.jedis.JedisSentinelPool">
<constructor-argindex="0"value="${redis.sentinel.masterName}" />
<constructor-argindex="1"value="#{'${redis.sentinels}'.split(',')}" />
<constructor-argindex="2"ref="jedisPoolConfig" />
<constructor-argindex="3"value="${redis.sentinel.timeout}"type="int" />
<constructor-argindex="4"value="${redis.sentinel.password}" />
<constructor-argindex="5"value="${redis.sentinel.database}"type="int" />
</bean>
</beans>

View Code

    JedisUtils.java

@AutowiredprivateJedisSentinelPool jedisPool;
@PostConstructprivate void init() throwsException {/*缓存初始化*/JedisUtils.setJedisPool(jedisPool);
}public static void setJedisPool(Pool<Jedis> jedisPool) throwsException {
JedisCache.jedisPool=jedisPool;
Jedis jedis= null;try{
jedis=jedisPool.getResource();
isInitSuc= true;
logger.info("redis start success!");
}catch(Exception e) {if (null !=jedis)
jedisPool.returnBrokenResource(jedis);
logger.error("redis start exception!!!error:{}", e.getMessage(), e);if (e instanceofredis.clients.jedis.exceptions.JedisConnectionException) {throwe;
}
}finally{if (null !=jedis)
jedisPool.returnResource(jedis);
}
}public staticLong publish(String chanel, String value) {
Jedis jedis= null;try{
jedis=jedisPool.getResource();returnjedis.publish(chanel,value);
}catch(Exception e) {if (null !=jedis) {
jedisPool.returnBrokenResource(jedis);
jedis= null;
}
logger.error("redis exception:{}", e.getMessage(), e);return 0L;
}finally{if (null !=jedis)
jedisPool.returnResource(jedis);
}
}

View Code

    NoticeTask.java

@Scheduled(cron = "*/5 * *  * * ? ")public voidrunMonitor() {try{
List<T>notices;
List<EbankNotice>result;while ((notices = QueueHolder.noticeBlockingQueue.take()) != null) { //消费
if(notices.isEmpty()) {continue;
}
result= service.calculateNotice(notices);
result.forEach(notice->{
JedisUtils.publish("cardniu-monitor", notice.getTitle() + "," +DateUtils.format(notice.getPostTime(),"yyyy-MM-dd hh:mm:ss") + "," +notice.getLinkAddress());});
}
}catch(InterruptedException e) {
logger.error("发送邮件定时任务异常,{}", e.getMessage(), e);
}
}

    

4、总结

     这个项目的核心在于wxpy 的运用,以及生产者消费者的设计思想。语言的话,核心的python这一块的wxpy,在生产者这边,无论是其他的什么语言,都可以作为我们的生产者。

     项目github地址:https://github.com/jaycekon/WxpyDemo

     希望各位喜欢的朋友可以fork 或者start一下~~~~~

参考:

wxpy API:http://wxpy.readthedocs.io/zh/latest/messages.html

wechat_sender Api:https://pypi.python.org/pypi/wechat-sender/0.1.3

python-redis :https://pypi.python.org/pypi/redis

Java-Redis:http://docs.spring.io/spring-data/redis/docs/2.0.0.M4/reference/html/  

Python-WXPY实现微信监控报警相关推荐

  1. 企业微信监控服务器数据接入,如何在alertmanager报警中添加企业微信监控报警

    如何在alertmanager报警中添加企业微信监控报警 发布时间:2020-06-17 15:19:06 来源:亿速云 阅读:535 作者:元一 栏目:云计算 Prometheus机器:172.27 ...

  2. python公众号监控_Python-WXPY实现微信监控报警

    概述: 本文主要分享一下博主在学习wxpy 的过程中开发的一个小程序.博主在最近有一个监控报警的需求需要完成,然后刚好在学习wxpy 这个东西,因此很巧妙的将工作和学习联系在一起. 博文中主要使用到的 ...

  3. Python wxpy 操作微信 大全集

    安装及导入 wxpy 安装 wxpy pip install wxpy 导入 wxpy import wxpy 登录微信 登录 bot = wxpy.Bot(cache_path=None, cons ...

  4. python自动发送微信文件_python自动发微信监控报警

    我们每个人每天都是在用微信,在程序开发过程中,我们会需要监控我们的程序,发短信监控收费,发邮件懒得看,发微信是最好的方式,而且是免费的.发现个非常好用的python库:wxpy.wxpy基于itcha ...

  5. python自动发微信新闻网_python自动发微信监控报警

    我们每个人每天都是在用微信,在程序开发过程中,我们会需要监控我们的程序,发短信监控收费,发邮件懒得看,发微信是最好的方式,而且是免费的.发现个非常好用的python库:wxpy.wxpy基于itcha ...

  6. zabbix之微信告警(python版):微信个人报警,微信企业号告警脚本

    微信个人告警脚本 微信个人告警:使用个人微信,发送到微信群组,微信好友 两个脚本执行: 1)能连接网络 2)先执行server.py,扫描登录微信,登录之后没有报错,打开新终端查看端口是否起来了 3) ...

  7. 用python wxpy管理微信公众号,并利用微信获取自己的开源数据。

    之前了解到itchat 乃至于 wxpy时 是利用tuling聊天机器人的接口.调用接口并保存双方的问答结果可以作为自己的 问答词库 的一个 数据库累计. 这些数据可以用于自己训练. 而最近希望获取一 ...

  8. python wxpy登录微信,扫完二维码后,打开的软件没有关闭/ 扫二维码时不小心关闭了软件,程序会卡住不动的处理方法

    加了几行代码,扫完二维码后,把打开图片的软件也结束程序 圈起来的代码是我改动后的!! 先找到login.py 追踪到打开图片的函数: 扫描结束后的函数: 这样的话,登录微信过程中,一旦发现显示二维码的 ...

  9. Zabbix 使用微信接收报警信息

    1.Zabbix 使用微信接收报警信息 目录[-] 1.配置Zabbix微信报警媒介 2.配置收件人 3.配置Action 4.准备微信报警脚本 5.微信脚本关联企业微信 6.微信告警脚本配置连接微信 ...

最新文章

  1. 一篇很全面的freemarker教程
  2. bzoj29894170: 数列
  3. Bootstrap学习笔记01【快速入门、栅格布局】
  4. Spring 框架基础(06):Mvc架构模式简介,执行流程详解
  5. js不区分大小写查找字符串
  6. 微服务项目部署在docker容器运行
  7. python装饰器详解-Python 函数装饰器
  8. 使用fiddler4进行微信小程序抓包
  9. 迅雷高速通道破解教程
  10. C# 斑马打印机USB接口实现打印各种类型的码
  11. K8s入门:Docker,kubectl,Kind的安装
  12. cvpr 深度估计_近两年 CVPR ICCV ECCV 相机位姿估计、视觉定位、SLAM相关论文汇总...
  13. 三相交流电源中相电压电流符号表示
  14. CES2014盘点:九大穿戴式设备
  15. unity 移动开发优化二 图形优化,脚本优化概述
  16. 公司被告,晋升受阻,为刷考评互拆台...这届FB员工太难了
  17. 微机原理与接口技术——8255A的使用方法
  18. 计算机桌面24小时制设置,时间怎么设置24小时
  19. 将波旁威士忌的整洁Mixins提升到新的水平
  20. linux操作系统 第02章 linux操作做基础

热门文章

  1. 全国计算机考试界面,2017计算机二级考试MSOffice考试设置页面对话框的技巧
  2. Windows 10,快捷截屏WIN+SHIFT+S不起作用解决办法。
  3. metricbeat8.1.3的安装
  4. win7无法关机,无法关闭程序
  5. 猿思考系列4——一文学会java的斗转星移
  6. 关于App端使用微软云的一点笔记
  7. dbeaver 设置编码_苹果视频压缩编码输出软件Compressor中文版
  8. 118-idea 激活
  9. GitHub删除仓库或修改仓库名
  10. h5+css3+js实现七夕言情