前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python之轻松玩转微信(wechat

python之轻松玩转微信(wechat

作者头像
py3study
发布2020-01-03 14:38:20
1.2K0
发布2020-01-03 14:38:20
举报
文章被收录于专栏:python3python3

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

首先安装一个python的微信库

代码语言:javascript
复制
pip install -U wxpy
代码语言:javascript
复制
下面放出源码:

#!/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下发送消息给好友:

代码语言:javascript
复制
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)机器人

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

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

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

wxpy文档说明

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-23 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档