python3.7中使用wxpy模块实现微信登录,自动回复消息,打印消息,发送天气情况,查找联系人等功能

微信登录和登出

#encoding=utf-8
from wxpy import *

# 初始化机器人,扫码登录
# cache_path保持登录,Bot初始化中可以加入参数:console_qr是否在控制台显示二维码
bot = Bot(cache_path=True)

print "登录成功"

bot.logout()
print '退出登录成功'

阻塞进程,保持登录状态

#encoding=utf-8
# Created by double lin at 2018/8/21
import wxpy

bot = wxpy.Bot(cache_path=True)

bot.join()

 登陆成功后输出,自己的名称,下方没有提示:finished in **s,表示我们的进程阻塞成功,便于我们在后续操作中,对微信中的信息,以及其他内容进行监听。

剩下的换个时间再写,先去看看公司产品新版本的文档。sorry啦。

2018.9.6重新开始下面的实现内容

找到自己微信里面的所有朋友的个人信息,省份,城市,昵称等信息

#encoding=utf-8
# Created by double lin at 2018/8/16
from wxpy import *

bot = Bot(cache_path=True, console_qr=False)
# 启用 puid 属性,并指定 puid 所需的映射数据保存/载入路径
bot.enable_puid('wxpy_puid.pkl')

friends = bot.friends()
sex_male = 0
sex_femal = 0
unkown = 0
for i in range(len(friends)):
    # friends[i]拥有的属性列表
    # add alias bot city get_avatar is_friend mark_as_read name nick_name pin province
    # puid raw remark_name send send_file send_image send_msg send_raw_msg send_video
    # set_remark_name sex signature uin unpin user_name wxid

    print 'friends[i]:\t', friends[i]  # 聊天对象格式为:<Friend: 备注名称>

    print 'nick_name:\t' + friends[i].nick_name  # 微信好友的微信昵称

    print 'name:\t' + friends[i].name   # 扫码用户对该微信好友的备注
    print 'remark_name:\t' + friends[i].remark_name

    print 'bot:\t', friends[i].bot    # 机器人对象 格式为:<Bot: 备注名称>

    print 'raw:\t', friends[i].raw    # 微信好友对应的用户详细信息json数据

    print 'puid:\t' + friends[i].puid   # 微信好友对应的puid值

    print 'NickName:\t' + friends[i].raw['NickName']  # 打印备注信息

    print "Province:\t" + friends[i].raw['Province']    # 打印好友所在的省份
    # print friends[i].province

    print "City:\t\t\t" + friends[i].raw['City']    # 城市
    # print friends[i].city
    # 男性 sex值为1, 女性值为2 未提供为0
    print friends[i].raw['Sex'],

    if friends[i].raw['Sex'] == 1:
        sex_male += 1
    elif friends[i].raw['Sex'] == 2:
        sex_femal += 1
    else:
        unkown += 1

print 'count of male is:', sex_male, 'count os female is:', sex_femal,'不知道性别的共:', unkown
print '共有好友:', len(friends)

 监控群聊和某一个指定的好友或多个好友发来的文本消息,并打印消息

#encoding=utf-8
from wxpy import *

bot = Bot(cache_path=True)

# 找到好友列表中昵称为“我说”的好友,监控聊天,打印该好友发来的文本消息
myfriend = bot.friends().search(u'我说')[0]
@bot.register(myfriend, TEXT)
def print_msg1(msg1):
    print(msg1)

#监控群聊消息,打印群聊中的文本消息
@bot.register(Group, TEXT)
def print_msg(msg):
    print (msg)

bot.join()

结果如图:

 自动回复消息

#encoding=utf-8
# Created by double lin at 2018/8/16
from wxpy import *
# import itchat
# itchat.auto_login(hotReload=True)

bot = Bot(cache_path=True, console_qr=False,login_callback='loginSuccess')

tuling = Tuling(api_key='你在tuling网站申请的api_key')
# print (tuling.__doc__)
# sex和city可以不输入,也可以仅通过他们进行好友的获取,结果为列表形式,所以我们通过[0]得到第一项
myfriend = bot.friends().search(u'好友昵称', sex=FEMALE, city="好友信息中的城市")[0]
myfriend1 = bot.self
print (myfriend)

@bot.register()
def reply_msg(msg):
    sender_username = msg.sender.raw['UserName']
    # 输出发送信息的好友或者群聊中的人员信息
    print (sender_username)
    # 判断是否和我设置的想要自动恢复到人一致如果一致调用tuling进行消息回复
    if sender_username == myfriend.raw['UserName']:
        #输出或得到的消息
        print (msg)
        #调用tuling机器人回复消息,并将消息赋值给message
        message = tuling.do_reply(msg)
        #输出回复消息的内容
        print message

#阻塞进程
bot.join()

 手机端的效果展示。引入tuling机器人之后的自动回复

目前实现功能如上所述,仅作自我激励之用,感谢!

猜你喜欢

转载自blog.csdn.net/qq_32670879/article/details/82222353