python wxpy库体验---实现微信一些自动化功能

 朋友推荐的这个好玩的库,花了几天研究了下,发现挺有意思的。

效果截图

功能实现

1.好友验证信息带关键字自动通过,并发送欢迎语和功能菜单

2.按照发送关键字推送对应功能

3.好友聊天接入‘图灵机器人’,实现尬聊;选择每周几发送好运统计信息给自己

4.群聊被艾特自动回复、定时定群发送消息、转发定群定人定关键字消息到目的群聊

wxpy 使用了 Web 微信的通讯协议,因此仅能覆盖 Web 微信本身所具备的功能。

所以以下功能目前 均不支持

  • 支付相关 - 红包、转账、收款 等都不支持
  • 在群聊中@他人 - 是的,Web 微信中被人@后也不会提醒
  • 发送名片 - 但可以通过 send_raw_msg() 转发
  • 发送分享链接 - 也无法转发
  • 发送语音消息
  • 朋友圈相关

具体使用场景:1)商家会员管理,推送相应文章、发送消息,建群拉人发布活动

       2)新生协会纳新、报名管理等   ---可参考一篇不错的教程:https://www.jianshu.com/p/ef75e97146c2  

       3)打游戏不回妹子消息?把自己微信打造成一款尬聊机器人;过年群发转发自动回复祝福消息等等

       4)期待你们挖掘然后分享

实现思路

wxpy能模拟简单的微信操作,之前公司做锦鲤活动的时候就用的微信号拉人进群的操作(添加管理员微信,被拉进群,定时发送群聊消息,各个群同步消息),用wxpy简单模拟一下各种操作,然后用configparser管理一些配置文件,脚本运行只需修改配置文件即可,不用看代码就能根据修改自定义配置文件运行

环境准备

python 3.6 版本,用到的库:wxpy,configparser,time

实现过程

wxpy库文档:https://wxpy.readthedocs.io/zh/latest/bot.html

configparser参考:https://blog.csdn.net/harryzzz/article/details/78754670

关键代码

 1 [Group]
 2 #定时发送时间
 3 timing = 00:00:00
 4 #定时发送内容
 5 timing_send = 
 6 #定时发送群聊
 7 timing_group = 
 8 
 9 #监控group里面boss发送的包含key的消息到forward_group里面(带前缀)
10 forward_prefix = 
11 boss_name = 
12 boss_key = 
13 boss_group = 
14 forward_group = 
15 
16 #自动回复被group里面@的消息,回复内容为msg
17 auto_reply_msg = 
18 auto_reply_group = 
19 #每周六定时发送好友统计信息
20 week = 
21 
22 [Friend]
23 #添加好友信息中包含key的时候回复content
24 friend_key = 
25 friend_content = 
26 
27 [Default]
28 #基础信息,tuling的key和图片地址
29 tuling_key = 你的图灵key
30 img_path = 
31 
32 [Add_group]
33 user_add_group =
34 
35 [Function]
36 #博客地址
37 person_url = 
38 #功能列表
39 function_list = 回复关键字: 进群  博客 其他 打赏  继续我们的互撩吧(*^__^*)
40 #回复其他的时候获得的列表
41 other_function = 回复讲笑话、讲故事、XX座运势、脑筋急转弯、歇后语、绕口令、顺口溜体验更多功能
自定义配置文件
import time
from wxpy import *
import configparser
cp = configparser.ConfigParser()
# 配置文件目录
cp.read(r'F:\PythonDemo\Demo\wxpy.ini',encoding='utf-8')

# 配置信息
timing = cp.get('Group','timing')
timing_send = cp.get('Group','timing_send')
timing_group = cp.get('Group','timing_group')
forward_prefix = cp.get('Group','forward_prefix')
boss_name = cp.get('Group','boss_name')
boss_group = cp.get('Group','boss_group')
forward_group = cp.get('Group','forward_group')
boss_key = cp.get('Group','boss_key')
auto_reply_msg = cp.get('Group','auto_reply_msg')
auto_reply_group = cp.get('Group','auto_reply_group')
friend_key = cp.get('Friend','friend_key')
friend_content = cp.get('Friend','friend_content')
tuling_key = cp.get('Default','tuling_key')
person_url = cp.get('Function','person_url')
function_list = cp.get('Function','function_list')
other_function = cp.get('Function','other_function')
week = cp.getint('Group','week')
user_add_group = cp.get('Add_group','user_add_group')
img_path = cp.get('Default','img_path')

bot = Bot(console_qr=0, cache_path=True)
bot.enable_puid('wxpy_puid.pkl')

# 每周发送好友信息给自己
def info_statistics(week):
    if time.strftime('%w') == str(week):
        friends_stats_text = bot.friends().stats_text()
        bot.self.send_msg(friends_stats_text)

def add_group(user):
    group = bot.groups().search(user_add_group)[0]
    if user in group:
        user.send('您已加入该群聊:<{}>'.format(group.nick_name))
    else:
        group.remove_members(user,use_invitation=True)

# 主要功能列表
@bot.register(Friend,msg_types=TEXT)
def auto_reply_friendMsg(msg):
    user = msg.sender
    if '能干嘛' in msg.text:
        user.send(function_list)
    elif '哈哈' in msg.text:
        add_group(user)
    elif '进群' in msg.text:
        add_group(user)
    elif '博客' in msg.text:
        user.send(person_url)
    elif '打赏' in msg.text:
        # 打赏的图片
        bot.upload_file(img_path)
        user.send_image(img_path)
    elif '其他' in msg.text:
        user.send(other_function)
    else:
        tuling = Tuling(api_key=tuling_key)
        tuling.do_reply(msg)

# 好友验证带key就自动添加好友
@bot.register(msg_types=FRIENDS)
def auto_accept_friend(msg):
    if friend_key in msg.text.lower():
        new_friend = bot.accept_friend(msg.card)
        new_friend.send(friend_content)

# 自动回复群聊艾特消息
def auto_reply_groupMsg(msg):
    group = ensure_one(bot.chats().search(auto_reply_group))
    if msg.is_at and msg.chat == group:
        msg.reply(auto_reply_msg)

# 自动转发定组定人定关键字的消息到目的群聊
def auto_forward_bossMsg(msg):
    src_group = ensure_one(bot.groups().search(boss_group))
    dst_group = ensure_one(bot.groups().search(forward_group))
    boss = ensure_one(src_group.search(boss_name))
    if boss_key in msg.text and msg.member == boss:
        msg.forward(dst_group,prefix=forward_prefix)

# 定时发送消息到目的群聊
def auto_send_timeMsg():
    dst_group = bot.chats().search(timing_group)[0]
    ti = time.localtime(time.time())
    dst_time = time.strftime('%H:%M:%S',ti)
    if dst_time == timing:
        dst_group.send(timing_send)

# 群聊支持的功能
@bot.register(Group,msg_types=TEXT)
def auto_ReplyForwardSend_groupMsg(msg):
    auto_forward_bossMsg(msg)
    auto_reply_groupMsg(msg)
    auto_send_timeMsg()

if __name__ == '__main__':
    # bot.join()
    embed()
运行代码

遇到的问题

1)TypeError: dump() missing 1 required positional argument: 'fp'

字典和json格式转换的时候遇到的,用法错误,需要用dumps

2)UnicodeEncodeError: 'latin-1' codec can't encode characters in position 50-54:

图灵API 说明里面要求要utf-8编码,s.encode('utf-8')即可

3) 一般是配置文件没有正确打开

4)UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 20: illegal multibyte sequence    

cp = configparser.ConfigParser()
cp.read(r'F:\PythonDemo\Demo\wxpy.ini',encoding='utf-8')

下载地址:https://git.dev.tencent.com/mumu0802/Demo.githttps://git.dev.tencent.com/mumu0802/Demo.git

转载于:https://www.cnblogs.com/mumu0802/p/10046684.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值