Python通過微信遠程控制電腦 - python itchat

itchat是一個開源的微信個人號接口,使用python調用微信從未如此簡單。

使用不到三十行的代碼,你就可以完成一個能夠處理所有信息的微信機器人。

當然,該api的使用遠不止一個機器人,更多的功能等着你來發現,比如這些

該接口與公衆號接口itchatmp共享類似的操作方式,學習一次掌握兩個工具。

如今微信已經成爲了個人社交的很大一部分,希望這個項目能夠幫助你擴展你的個人的微信號、方便自己的生活。

官方文檔http://itchat.readthedocs.io

itchat的倉庫地址https://github.com/littlecodersh/ItChat 

安裝:

pip install itchat

代碼:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import itchat
import os
import cv2
from PIL import ImageGrab

usageMsg = u"使用方法:\n1.運行CMD命令:cmd xxx (xxx爲命令)\n" \
           u"例如關機命令:\ncmd shutdown -s -t 0 \n" \
           u"2.獲取攝像頭並拍照:cap\n" \
           u"2.獲取屏幕截屏:pc\n" \

@itchat.msg_register('Text')
def handler_receive_msg(msg):  # 處理收到的消息
    message = msg['Text']
    to_name = msg['ToUserName']
    path = 'E:/sample/temp.jpg'  # 臨時保存截屏圖片地址
    if to_name == "filehelper":
        if message == "cap":  # 拍照
            #  要使用攝像頭,需要使用cv2.VideoCapture(0)創建VideoCapture對象,
            # 參數:0指的是攝像頭的編號。如果你電腦上有兩個攝像頭的話,訪問第2個攝像頭就可以傳入1
            cap = cv2.VideoCapture(0)
            ret, img = cap.read()  # 獲取一幀
            cv2.imwrite("temp.jpg", img)
            itchat.send('@img@%s' % u'temp.jpg', 'filehelper')
            cap.release()  # 釋放資源
        elif message[0:3] == "cmd":  # 處理cmd命令
            os.system(message.strip(message[0:4]))
        elif message == "pc":  # 截圖
            im = ImageGrab.grab()  # 實現截屏功能
            im.save(path, 'JPEG')  # 設置保存路徑和圖片格式
            itchat.send_image(path, 'filehelper')
        else:
            return msg['Text']


if __name__ == '__main__':
    itchat.auto_login(hotReload=True)   # 退出程序後暫存登陸狀態
    # itchat.auto_login()
    itchat.send(usageMsg, "filehelper")
    itchat.run()

# 大部分代碼爲轉載,侵告刪

(注意:如果安裝CV2時出現異常,請參考:Python安裝CV2

效果圖:

 

如果你想在後臺無界面運行,參考:運行bat時隱藏cmd窗口

如果需要做一個聊天機器人,可對接圖靈機器人,教程可自行查詢,這裏不在敘述

 

 

 

參考:https://blog.csdn.net/m0_38106923/article/details/81978279

https://blog.csdn.net/sixkery/article/details/81674684

https://blog.csdn.net/weixin_37557902/article/details/82740593

https://blog.csdn.net/wangxiao7474/article/details/80050730

https://itchat.readthedocs.io/zh/latest/tutorial/tutorial0/

https://www.cnblogs.com/dongxiaodong/p/10490563.html

https://www.cnblogs.com/wang-li/p/9744502.html

侵告刪

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章