安装
pip install wxpy
使用
初始化机器人
- console_qr 命令行二维码
- cache_path 使用缓存登录
bot = Bot(cache_path=True, console_qr=True)
自动接收好友请求
# 自动接受新的好友请求
@bot.register(msg_types=FRIENDS)
def auto_accept_friends(msg):
# 接受好友请求
new_friend = msg.card.accept()
# 向新的好友发送消息
new_friend.send('Hi~')
回复好友消息
# 回复好友消息
@bot.register(Friend, TEXT)
def exist_friends(msg):
msg.sender.send('Hi~')
回复群消息
# 回复群消息
@bot.register(Group, TEXT)
def print_group_msg(msg):
msg.sender.send('Hi~')
搜索朋友并发送消息
from wxpy import *
bot = Bot(cache_path=True, console_qr=True)
my_friend = bot.friends().search(u'小妖精')[0]
# 发送文本
my_friend.send('Hello, WeChat!')
# 发送图片
my_friend.send_image('my_picture.png')
# 发送视频
my_friend.send_video('my_video.mov')
# 发送文件
my_friend.send_file('my_file.zip')
# 以动态的方式发送图片
my_friend.send('@img@my_picture.png')
从群聊中添加好友
my_group = bot.chats().search('gropu_name')[0].search('Frank')
my_group.add_all()