微信已经成了中国人生活中基本的通信工具(除了那些自由开源人士之外),前两天发现微信机器人的项目,其实早就有了。想着本身也作一个吧,顺便加了一些小小的功能。python
微信扫一扫加他,跟他尬聊吧,把他拽到群里调戏他。git
具体功能下面会介绍。github
手机json
微信登录必须得有手机端登录才能使用网页登录,由于要扫一扫
Python 平台服务器
该项目基于 Python 开发,因此至少得来个嵌入式的开发板,或者电脑,或者...云服务器 ;-),若是要保证长时间开启与话,最好使用云服务器。
该项目基于 Github 上的 wxpy,使用文档在 这里。中文版的,因此我就不介绍这个怎么使用了。简单描述一下微信
from wxpy import * bot = Bot()
机器人对好友、群聊中 at
他的人进行回复,在群聊中同时统计每一个人的发言次数和第一次发言的时间,将这些信息实时存储在本地,以防程序错误致使数据丢失。app
消息回复中的机器人使用 图灵机器人, 可免费申请 API,调用他。也可使用 小 I 机器人。这两个都是深度整合在项目里的。工具
@bot.register([Friend, Group]) def reply_friend(msg): """ 消息自动回复 """ print(msg) if isinstance(msg.chat, Group): group = msg.chat.name name = msg.member.name if group in stat: if name in stat[group]['count']: stat[group]['count'][name] += 1 else: stat[group]['count'][name] = 1 flag = True for rank in stat[group]['rank']: if name == rank['name']: flag = False break if flag: stat[group]['rank'].append({'name': name, 'time': time.strftime("%H:%M:%S", time.localtime())}) else: stat[group] = {"count": {name: 1}, 'rank': [{'name': name, 'time': time.strftime("%H:%M:%S", time.localtime())}, ]} if msg.text == "发言排行榜": g = bot.groups().search(group)[0] if not stat[g.name]: return msg_text = "" index = 1 count = stat[g.name]['count'] for name in sorted(count, key=lambda x: count[x], reverse=True): # print("{}: {} {}".format(index, rank['name'], rank['time'])) msg_text += "{}: {} 发言了 {} 次\n".format(index, name, count[name]) index += 1 if msg_text: msg_text = "发言排行榜:\n" + msg_text g.send(msg_text) if msg.text == "起床排行榜": g = bot.groups().search(group)[0] if not stat[g.name]: return msg_text = "" index = 1 for rank in stat[g.name]['rank']: # print("{}: {} {}".format(index, rank['name'], rank['time'])) msg_text += "{}: {} {}\n".format(index, rank['name'], rank['time']) index += 1 if msg_text: msg_text = "起床排行榜:\n" + msg_text g.send(msg_text) with open('stat.json', 'w') as fh: fh.write(json.dumps(stat)) if not msg.is_at: return return tuling_auto_reply(msg)
@bot.register(msg_types=FRIENDS) def auto_accept_friends(msg): """ 自动接受好友请求 """ # 接受好友请求 new_friend = msg.card.accept() # 向新的好友发送消息 new_friend.send('哈哈,咱们如今是超级好的好朋友了呢~~')
光回复怎么够,还要作一些小小的有趣的功能,我这里添加了两个统计,一个是起床时间统计,另外一个是发言统计。spa
当天群聊的用户第一次发言做为起床时间,虽然有些不严谨,但毕竟功能是受限制的。线程
而后天天的 9 点发布一次起床排行榜, 20 点发布一次发言排行榜。固然其实主动发送 “起床排行榜”、“发言排行榜” 也会回复当前的排行。
class ScheduleThread(threading.Thread): """ 计划任务线程 """ def run(self): global schedule_time global bot global stat while 1: time.sleep(300) cur_hour = time.strftime("%H", time.localtime()) # print("cur:{}\tschedule:{}".format(cur_hour, schedule_time)) if cur_hour == schedule_time: continue elif cur_hour == '09': for group in bot.groups(): print(group.name) if not stat[group.name]: continue msg_text = "" index = 1 for rank in stat[group.name]['rank']: # print("{}: {} {}".format(index, rank['name'], rank['time'])) msg_text += "{}: {} {}\n".format(index, rank['name'], rank['time']) index += 1 if msg_text: msg_text = "排行日报\n起床排行榜:\n" + msg_text group.send(msg_text) elif cur_hour == '20': for group in bot.groups(): print(group.name) if not stat[group.name]: continue msg_text = "" index = 1 count = stat[group.name]['count'] for name in sorted(count, key=lambda x: count[x], reverse=True): # print("{}: {} {}".format(index, rank['name'], rank['time'])) msg_text += "{}: {} 发言了 {} 次\n".format(index, name, count[name]) index += 1 if msg_text: msg_text = "排行日报\n发言排行榜:\n" + msg_text group.send(msg_text) elif cur_hour == '00': stat = dict() with open('stat.json', 'w') as fh: fh.write('') schedule_time = cur_hour
展现两个机器人互相尬聊的状况是怎么样的。
建立机器人时添加一个 console_qr
参数, True
时表示在终端显示二维码,False
表示用图片程序打开二维码。按状况来,若是在没有界面的云服务器上,那就在终端打开,若是只能连 tty ,那最好的办法就是生成一张图片,放到指定的 FTP 或者云盘目录,而后本地打开扫描,或者建个简单的 HTTP 服务器展现图片,方法不少,根据本身状况来吧。
原文地址:一个简单有趣的微信聊天机器人
个人博客:时空路由器