Python玩转微信

很多人都在用微信,有没有想要用Python来控制我们的微信呀,哎呀,直接来点干货吧,我们可以直接在itchat上封装
http://itchat.readthedocs.io/zh/latest/

安装模块

1 pip install wxpy
2 
3 
4 pip install wxpy -i "https://pypi.doubanio.com/simple " #豆瓣源

1. 生成微信对象

 

1 bot = Bot() #初始化一个对象,相当于拿到这个人的微信,后续一些操作都由它完成

 

 

2.分别找到微信对象的好友,聊天对象,朋友等

 

1 friends = bot.friends() #获取朋友
2 chats = bot.chats() #获取聊天对象
3 groups = bot.groups() #获取群号
4 maps = bot.maps() #获取公众号
5 
6 #拿到的是列表,如果获取对象加上角标[0]
7 #这样很麻烦,推荐这个写法
8 ensure_one(bot.groups().search('家人'))

 

3.查找某一个好友

 

1 friends = bot.friends().search('方浩')[0]

 

4.向好友发送消息

 1 #发送文本
 2 my_friend.send('Hello,')
 3 
 4 #发送图片
 5 my_friend.send_image('01.jpg')
 6 
 7 #发送视频
 8 my_friend.send_video('video.mov')
 9 
10 #发送文件
11 my_friend.send_file('my_file.zip')
12 
13 #以动态的方式发送图片
14 my_friend.send('@img@my_prictue.png')

 

5.统计微信好友的信息,比如男女比例等等

bot.friends().stats_text()

 

6.监听群里某人的信息

 1 from wxpy import *
 2 
 3 bot  = Bot()
 4 
 5 #定位家人群
 6 company_group = ensure_one(bot.group().search('家人微信群'))
 7 
 8 #定位小弟
 9 di = ensure_one(company_group.search('小弟名'))
10 
11 #将小弟的信息转发给微信助手
12 @bot.register(company_group)
13 def forward_boss_message(msg):
14         if msg.member == boss
15               msg.forward(bot.file_helper, prefix='小弟发言')
16 
17 #堵塞线程
18 ember() #banner参数-设定欢迎内容,进入命令后展示。

7.接入图灵机器人,让机器人来回复好友消息

 1 from wxpy import *
 2 import wxpy
 3 from wxpy import *
 4 bot = Bot()   #初始化一个对象,就相当于拿到了这个人的微信,后续的一些操作都要用它来完成
 5 # me = ensure_one(bot.search('袁勇'))
 6 # me.send('哈哈')
 7 all_friends = bot.friends()  # 找到我所有的好友
 8 tuling = Tuling(api_key='0f329eba0af742cfb34daa64f9edef8b') # 接入图灵机器人
 9 for friend in all_friends :
10     @bot.register(friend)
11     def reply_me_friend(msg):
12         tuling.do_reply(msg)
13 embed()

8.设置最大保存信息条数,并且可以搜索

1 bot = Bot()
2 # 设置历史消息的最大保存数量为 10000 条
3 bot.messages.max_history = 10000
4 
5 # 搜索所有自己发送的,文本中包含 'wxpy' 的消息
6 bot.messages.search('wxpy', sender=bot.self)

 9.用微信监控你的程序

      1.获得专用logger

wxpy.get_wechat_logger(receiver=None, name=None, level=30)
获得一个可向指定微信聊天对象发送日志的 Logger

参数:    
receiver –
当为 None, True 或字符串时,将以该值作为 cache_path 参数启动一个新的机器人,并发送到该机器人的”文件传输助手”
当为 机器人 时,将发送到该机器人的”文件传输助手”
当为 聊天对象 时,将发送到该聊天对象
name – Logger 名称
level – Logger 等级,默认为 logging.WARNING
返回:    
Logger

   2.指定一个群为消息接受者

 1 from wxpy import *
 2  
 3 # 初始化机器人
 4 bot = Bot()
 5 # 找到需要接收日志的群 -- `ensure_one()` 用于确保找到的结果是唯一的,避免发错地方
 6 group_receiver = ensure_one(bot.groups().search('XX业务-告警通知'))
 7   
 8 # 指定这个群为接收者
 9 logger = get_wechat_logger(group_receiver)
10  
11 logger.error('打扰大家了,但这是一条重要的错误日志...')   #默认的日志级别设置为WARNING(日志级别等级CRITICAL > ERROR > WARNING > INFO > DEBUG)

  3.将异常消息发送到指定对象那里

 1 from wxpy import get_wechat_logger
 2 
 3 # 获得一个专用 Logger
 4 # 当不设置 `receiver` 时,会将日志发送到随后扫码登陆的微信的"文件传输助手"
 5 logger = get_wechat_logger()
 6 
 7 #指定接受对象
 8 group_reciver = ensure_one(bot.groups().search('全栈开发脱产11期'))
 9 
10 # 发送警告
11 logger.warning('这是一条 WARNING 等级的日志,你收到了吗?')
12 
13 # 接收捕获的异常
14 try:
15     1 / 0
16 except Exception as e 
17     logger.exception(e)

 

 

 

本文装载于 https://www.cnblogs.com/yyyyyyyyyy/p/9398649.html

 

posted @ 2019-07-08 15:30  瑞溪  阅读(492)  评论(0)    收藏  举报