微信自动发送、回复、定时发送-python

微信发送功能:

import itchat
itchat.auto_login(enableCmdQR=True, hotReload=True) #登陆 二维码
to_name1 = itchat.search_friends(name='微信的名字或备注名字')
print(to_name1)
#itchat.send('你好~', toUserName=to_name1[0]['UserName']) #发送文字

file_img = '../test.PNG'
itchat.send_image(file_img,toUserName=to_name1[0]['UserName']) #发送图片

file_text = '../test.PNG'
itchat.send_file(file_text,toUserName=to_name1[0]['UserName']) #发送文件

微信自动回复:

#coding=utf8
import itchat
import time

# 自动回复
# 封装好的装饰器,当接收到的消息是Text,即文字消息
@itchat.msg_register('Text')
def text_reply(msg):
    # 当消息不是由自己发出的时候
    if not msg['FromUserName'] == myUserName:
        # 发送一条提示给文件助手
        # 回复给好友
        return u'&***自动回复***&
啦啦啦啦啦啦
。。。。。'
        #return u'hhh'

if __name__ == '__main__':
    itchat.auto_login(hotReload=True)

微信定时发送:

import itchat
from apscheduler.schedulers.blocking import BlockingScheduler
import time


# 发送信息
def send_msg():
    user_info = itchat.search_friends(name='cookie')
    if len(user_info) > 0:
        user_name = user_info[0]['UserName']
        itchat.send_msg('test22!', toUserName=user_name)

def send_msg1():
    user_info = itchat.search_friends(name='区钧亮')
    if len(user_info) > 0:
        user_name = user_info[0]['UserName']
        itchat.send_msg('臭弟弟,起床学习啦', toUserName=user_name)

def after_login():
    print("test")
    for i in range(0,30):
        sched.add_job(send_msg, 'cron', year=2020, month=12, day=11, hour=16, minute=21, second=i)
    print("test")
    sched.start()


def after_logout():
    sched.shutdown()


if __name__ == '__main__':
    sched = BlockingScheduler()
    itchat.auto_login( hotReload=True,loginCallback=after_login, exitCallback=after_login)
    itchat.run()

微信遥控电脑功能:

import itchat
from itchat.content import *
import os

@itchat.msg_register(TEXT)    #如果接收的是文字
def text_reply(msg):
    #reply_text = msg.text.replace('吗?','!')  #替换
    if msg.text == "打开微信":             #遥控打开微信
        cmd = 'start D:weixinWeChatWeChat.exe'
        # res = os.popen(cmd)
        # output_str = res.read()   # 获得输出字符串
        # print(output_str)
        os.system(cmd)
        return "已经打开!"
    return "未配置指令,无法识别"
itchat.auto_login(enableCmdQR=True, hotReload=True)
itchat.run()


原文地址:https://www.cnblogs.com/trevain/p/14120271.html