python wechat_python之轻松玩转×××(wechat)机器人

首先安装一个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)

显示两个×××的共同好友:

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

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

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

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

原文地址:http://blog.51cto.com/m51cto/2082924

你可能感兴趣的:(python,wechat)