微信控制电脑

# encoding:utf8
import itchat
import os
import time
import cv2  # 如果使用opencv的话可以远程拍照
from win_capture import window_capture

sendMsg = u"[消息助手]:暂时无法回复"  # 自动回复内容
usageMsg = u"使用方法:\n" \
           u"1.运行CMD命令:cmd xxx (xxx为命令)\n-例如关机命令:\ncmd shutdown -s -t 60 \n" \
           u"2.获取张图片:cap_3(获取3张,每2秒一张)\n" \
           u"3.启用消息助手(默认关闭):ast\n4.关闭消息助手:astc\n" \
           u"4.获取屏幕截图:scr_3(获取3张,每2秒一张)\n"


@itchat.msg_register('Text')  # 注册文本消息
def text_reply(msg):  # 心跳程序
    global flag
    message = msg['Text']  # 接收文本消息
    fromName = msg['FromUserName']  # 发送方
    toName = msg['ToUserName']  # 接收方

    if toName == "filehelper":
        if "cap" in message:  # 远程拍照并发送到手机
            try:
                n = int(message.split('_')[-1])
            except:
                n = 3
            for i in range(n):
                cap = cv2.VideoCapture(0)
                ret, img = cap.read()
                cv2.imwrite("weixinTemp.jpg", img)
                itchat.send('@img@%s' % u'weixinTemp.jpg', 'filehelper')
                cap.release()
                time.sleep(2)
        if message[0] + message[1] + message[2] == "cmd":  # 远程执行cmd命令
            os.system(message.strip(message[0] + message[1] + message[2] + message[3]))  # 远程执行cmd命令,可以实现关机
        if message == "ast":
            flag = 1
            itchat.send("消息助手已开启", "filehelper")
        if message == "astc":
            flag = 0
            itchat.send("消息助手已关闭", "filehelper")
        if "scr" in message:  # 远程拍照并发送到手机
            try:
                n = int(message.split('_')[-1])
            except:
                n = 3
            for i in range(n):
                window_capture("scrTemp.jpg")
                itchat.send('@img@%s' % u'scrTemp.jpg', 'filehelper')
                time.sleep(2)

    elif flag == 1:
        itchat.send(sendMsg, fromName)
        myfile.write(message)  # 保存消息内容
        myfile.write("\n")
        myfile.flush()


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')

if __name__ == '__main__':
    itchat.auto_login()
    itchat.send(usageMsg, "filehelper")
    itchat.run()

 

你可能感兴趣的:(Python)