准备工作:
①两个微信号(A添加B,备注B为Girl)
②python编辑器(推荐pycharm)
③wxpy库(三种方式安装)
pip install -U wxpy
pycharm-->prefermence-->project interpreter
pip install -U wxpy -i “https://pypi.doubanio.com/simple/”
代码演示:(扫码登录测试即可)
from wxpy import *
import re
# 生成机器人实例,启动缓存避免重复登录
bot = Bot(cache_path=False)
# 在好友列表中搜索名字是‘Girl【你自己设置的备注名】'性别为男的一项
found = bot.friends().search('Girl【你自己设置的备注名】', sex=1)
# 确保只有一个结果
boyfriend = ensure_one(found)
bf_rules = {
r'^.*在[吗|嘛]': '在呀,在想你呀',
r'^.+[什么|怎么]安排': '都听你的呀,嘻嘻',
r'^.*星火燎原': '我想了想,不然我们还是分手吧'
}
@bot.register([boyfriend, bot.self], msg_types=TEXT, except_self=True)
def reply_bf(msg):
for rule in bf_rules:
if re.match(rule, msg.text):
try:
# 尝试向消息发送者回复消息
msg.sender.send_msg(bf_rules[rule])
except ResponseError as e:
# 查看错误号和错误消息
print(e.err_code, e.err_msg)
return
return '我有点笨,能不能迁就下我,我们说点别的?'
# 进入 python 命令行、让程序保持运行
embed()