微信已經成了中國人生活中基本的通信工具(除了那些自由開源人士之外),前兩天發現微信機器人的項目,其實早就有了。想着本身也作一個吧,順便加了一些小小的功能。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 服務器展現圖片,方法不少,根據本身狀況來吧。
原文地址:一個簡單有趣的微信聊天機器人
個人博客:時空路由器