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

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

首先安装一个python的微信库


pip install -U wxpy
下面放出源码:

#!/usr/bin/env python3
# coding: utf-8
# @Author  : Aiker

from 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说明文档:

wxpy文档说明



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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值