【输出所有群聊名称】
from wxpy import * bot=Bot(cache_path=True) group=bot.groups() i = 1 for g in group: print (u"群聊编号:%d" % i) print (str(g)) i += 1
【回复指定朋友msg、回复群聊@自己的msg、打印收到的msg】
import string from wxpy import * bot = Bot(cache_path=True) friend = bot.friends().search(u'AAAAA')[0] @bot.register() def print_others(msg): print(msg) @bot.register(Group, TEXT) def print_group_msg(msg): if msg.is_at: #print(msg.type) print(msg) msg.reply("reply1") @bot.register(friend) def reply_my_friend(msg): return 'received: {} ({})'.format(msg.text, msg.type) embed()
【获取所有朋友】
bot=Bot(cache_path=True) myfend=bot.friends(update=True) for f in myfend: print (str(f))
【指定群聊,添加群成员为好友】
from wxpy import * import time bot=Bot(cache_path=True) my_group = bot.chats().search(u'【互联网项目交流---001】')[0].search('A') print my_group my_group.add_all()#执行函数报错,add()也报错!!
【遍历聊天列表(包含:好友、群聊、 公众号)】
from wxpy import * 扫码登录微信,console_qr=True 显示二维码,cache_path=True缓存登录信息,避免重复扫码 bot = Bot(cache_path=True) 获取当前聊天列表 chatlist = bot.chats(update=True) friendnum = 0 遍历聊天列表,打印所有的friend、mp、group,打印出了N个对象,每个对象是一个字符串 <xxx:xxxx> for i in chatlist: if 'Friend' in str(i):#统计打印friend print(i) friendnum += 1 print("friendnum:%d" % friendnum)
【给指定好友/群聊发送随机密码】
from wxpy import * import time import os import random import string bot=Bot(cache_path = True) boring_group = bot.groups().search(u'【互联网项目交流---001】')[0] friend = bot.friends().search(u"AAAAA")[0] str='1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+=-' sa=[] for x in range(5): for y in range(12): sa.append(random.choice(str)) salt=''.join(sa) boring_group.send('random password is :%s'%salt)#群发消息
print ("【%d】random password:%s" % (x+1,salt)) friend.send('random password is :%s'%salt) time.sleep(3) salt='' sa=[]
【打印某群聊的所有群成员】
from wxpy import * import time bot=Bot(cache_path=True) # boring_group=bot.chats()#.search(u'【互联网项目交流---001】')[0] group = bot.groups().search(u'信工2015级全体同学')[0] print group print "-----------------------" for member in group: print (str(member)) #boring_group.add_all() #time.sleep(60) print ("membernum in this group :%d" % len(group))
【wxpy接入tuling api】
from wxpy import * import requests import re import time # tuling api apply # 以下所有代码没有任何效果 bot = Bot(cache_path=True) KEY = '3090e0829a6c4c04aa6b7b7d88714bfe' xiaohao = bot.friends().search('AAAAA')[0] def get_response(msg): apiUrl = 'http://www.tuling123.com/openapi/api' data = { 'key' : KEY, 'info' : msg, 'userid' : 'wechat-robot', } try: r = requests.post(apiUrl, data=data).json() return r.get('text') except: return @bot.register(msg_types=FRIENDS) def auto_accept_friends(msg): if 'wxpy' in msg.text.lower(): new_friend = bot.accept_friend(msg.card) new_friend.send(u'请求已经接受') @bot.register(Group,TEXT) def message_set(msg): if(msg.is_at): mess,num = re .subn(r'@(.+?)\s', ' ', msg.text) testmain = msg.sender return '{} '.format(get_response(mess.replace(' ',''))) @bot.register(Friend,TEXT) def message_set(msg): mess = get_response(msg.text) if mess !='': return '{}'.format(mess) else: return u'机器人离开了' @bot.register(xiaohao) def ssmessage_set(msg): return '123456' embed()