博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python之轻松玩转微信(wechat)机器人
阅读量:7022 次
发布时间:2019-06-28

本文共 1941 字,大约阅读时间需要 6 分钟。

python的环境为python3,搭建环境这里就不说了,之前的文章里有

首先安装一个python的微信库

pip install -U wxpy
下面放出源码:#!/usr/bin/env python3# coding: utf-8# @Author  : Aikerfrom wxpy import *# 扫码登陆bot = Bot ()# 初始化图灵机器人 (API key 申请: http://tuling123.com)tuling = Tuling (api_key='cc7e7f95db3545ad8e4867xxxxxx')# xiaoi = XiaoI('2OEuYx5PBm5i','3JfIuWWkcBVj5bx75sYl')#xiaoI机器人boring_group = bot.groups().search('Anker')[0]boring_group1 = bot.groups().search('BP')[0]boring_group2 = bot.groups().search('Aiker')[0]# boring_group1 = bot.groups().search("Anker Shenzhen",'Anker!','春运回家群')[0]# boring_group2 = bot.groups().search('Anker!')[0]my_friend = bot.friends ().search("Aiker")[0]jt = bot.friends ().search('jtt')[0]mygroup=bot.groups()print(mygroup)#打印所有群# 不自动回复指定的群消息和好友消息@bot.register ([boring_group,boring_group1,boring_group2,jt])def ignore(msg):    print(boring_group)    # 啥也不做    return# 自动回复所有文字消息并打印@bot.register (msg_types=TEXT)def auto_reply_all(msg):    tuling.do_reply (msg)    print (msg)# 打印指定的群消息@bot.register ([boring_group, boring_group1, boring_group2])def just_print(msg):    print (msg)#     f = open('wilow.txt', 'wb')#     f.writelines(msg)# 回复@的群聊消息和个人消息@bot.register((msg_types=TEXT))#注册消息类型为文本消息def auto_reply(msg):    if isinstance(msg.chat,Group) and not msg.is_at: # 判断是否是@的消息和个人消息,如果不是@消息        return  #什么也不做    else:        tuling.do_reply (msg)  #图灵自动回复消息        print(msg)  #打印消息        print(tuling.do_reply (msg)) #打印回复的消息内容#         return '收到消息:{}({})'.format(msg.text,msg.type) #回复消息:收到消息:内容# @bot.register()# def just_print(msg):#     print(msg)# 开始运行,embed下可以调试,可以直接bot.join()# bot.join ()#运行embed() # 堵塞线程,并进入 Python 命令行

#源码结束

下面的手动调试需要堵塞线程:

python下发送消息给好友:

In [26]: jt = bot.friends ().search('贾TT')[0]In [28]: jt.send('老铁')Out[28]: ↪ 贾TT : 老铁 (Text)In [29]: jt.send('从前有个人...')Out[29]: ↪ 贾TT : 从前有个人... (Text)

显示两个微信的共同好友:

python之轻松玩转微信(wechat)机器人

显示自己最近发的历史消息:

python之轻松玩转微信(wechat)机器人

其他功能需要大家自己研究,wxpy说明文档:

本文转自 喵来个鱼 51CTO博客,原文链接:http://blog.51cto.com/m51cto/2082924,如需转载请自行联系原作者

你可能感兴趣的文章