微信定时消息发送 Python脚本神器

设计目的
最开始只是想能不能做一个程序,可以每天定时发个消息,检查一下机器是否运行正常,所以在🦅总支援下修正,复测写了一个定时发送的Python脚本。灵感总是来源于生活….

当然功能远不止是发送个消息而已,稍加改造可以做更多有趣的事情。

需要用到pywinauto的库

么有安装的需要先安装

pip install pywinauto

第一个版本

from pywinauto.application import Application
from pywinauto.keyboard import send_keys
import datetime
import time

time2 = datetime.datetime.now().strftime("%Y-%m-%d @ %H:%M")

def Atime():
    print(time2)
    return time2
 
def function_1():
    #连接微信
    time3 = datetime.datetime.now().strftime("%Y-%m-%d @ %H:%M")
    app = Application(backend="uia").connect(title = "微信")
    #获取微信窗口
    dlg = app.window(title= "微信")
 
    #进入微信,模拟按键Ctrl+F
    send_keys('^f')
    #进入微信,找到联系人(文件传输助手)
    send_keys('文件传输助手')
    time.sleep(1)
    send_keys('{ENTER}') # 回车键必须全部大小

    #获取聊天列表中的“文件传输助手”
    fileSend = dlg.child_window(title = "文件传输助手",control_type = "ListItem")
    #点击“文件传输助手”
    fileSend.click_input()
    #获取点击聊天窗口
    dlg.child_window(title="文件传输助手",control_type = "Edit").click_input()
    #在聊天界面输入"设备正常"
    send_keys("设备正常"+Atime())
    #获取发送按钮
    sendButton = dlg.child_window(title="发送(S)",control_type = "Button")
    #点击发送按钮
    sendButton.click_input()
    print("Good")
    return

while True:
    current_time = datetime.datetime.now()
    current_time_str = current_time.strftime("%H:%M")
    #自定义发送时间
    time_1 = "09:25"
    time_2 = "15:34"

    if current_time_str == time_1:
        function_1()
    elif current_time_str == time_2:
        function_1()

    # 等待一秒钟,避免重复执行
    datetime.datetime.now()
    time.sleep(60)

第二个版本

增加了 打开微信 

from pywinauto.application import Application
from pywinauto.keyboard import send_keys
import datetime
import time
import os

time2 = datetime.datetime.now().strftime("%Y-%m-%d @ %H:%M")

def Atime():
    print(time2)
    return time2
 
def function_1():

    # 打开微信
    def open_app(app_dir):
        os.startfile(app_dir)
    if __name__ == "__main__":
        app_dir = r'C:\Program Files\Tencent\WeChat\WeChat.exe'  # 此处为微信的绝对路径
        open_app(app_dir)
        time.sleep(1)
        
    #连接微信
    time3 = datetime.datetime.now().strftime("%Y-%m-%d @ %H:%M")
    app = Application(backend="uia").connect(title = "微信")

    #获取微信窗口
    dlg = app.window(title= "微信")
 
    #进入微信,模拟按键Ctrl+F
    send_keys('^f')
    #进入微信,搜索指定的微信名称“文件传输助手”
    send_keys('文件传输助手')
    time.sleep(1)
    send_keys('{ENTER}') # 回车键必须全部大小

    #获取聊天列表中的“文件传输助手”
    fileSend = dlg.child_window(title = "文件传输助手",control_type = "ListItem")
    #点击“文件传输助手”
    fileSend.click_input()
    #获取点击聊天窗口
    dlg.child_window(title="文件传输助手",control_type = "Edit").click_input()
    #在聊天界面输入"设备正常"+当前时间(可以替换为你需要的内容)
    send_keys("设备正常"+time3)
    #获取发送按钮
    sendButton = dlg.child_window(title="发送(S)",control_type = "Button")
    #点击发送按钮
    sendButton.click_input()
    print("Good"+time3)
    return

while True:
    current_time = datetime.datetime.now()
    current_time_str = current_time.strftime("%H:%M")
    time_1 = "09:23"
    time_2 = "15:24"

    if current_time_str == time_1:
        function_1()
    elif current_time_str == time_2:
        function_1()

    # 等待60秒钟,避免重复执行
    datetime.datetime.now()
    time.sleep(60)

第三个版本

优化了一下代码

from pywinauto.application import Application
from pywinauto.keyboard import send_keys
import datetime
import time
import os

def get_current_time():
    """
    获取当前时间并格式化
    """
    return datetime.datetime.now().strftime("%Y-%m-%d @ %H:%M")

def open_wechat(app_dir):
    """
    打开微信应用
    """
    os.startfile(app_dir)

def send_message(app, dlg, recipient, message):
    """
    在微信中发送消息给指定的接收者
    """
    # 进入微信,模拟按键Ctrl+F
    send_keys('^f')
    # 找到指定的接收者“recipient”
    send_keys(recipient)
    time.sleep(1)
    send_keys('{ENTER}')

    # 获取聊天列表中的接收者“recipient”
    fileSend = dlg.child_window(title=recipient, control_type="ListItem")
    fileSend.click_input()

    # 获取点击聊天窗口“recipient”
    chatWindow = dlg.child_window(title=recipient, control_type="Edit")
    chatWindow.click_input()

    # 在聊天界面输入消息
    send_keys(message)

    # 获取发送按钮并点击
    sendButton = dlg.child_window(title="发送(S)", control_type="Button")
    sendButton.click_input()

def main():
    # 定义微信应用的路径
    app_dir = r'C:\Program Files\Tencent\WeChat\WeChat.exe'

    # 打开微信
    open_wechat(app_dir)
    time.sleep(1)

    # 连接微信
    app = Application(backend="uia").connect(title="微信")
    dlg = app.window(title="微信")

    while True:
        current_time = datetime.datetime.now()
        current_time_str = current_time.strftime("%H:%M")
        time_1 = "09:23"
        time_2 = "15:24"

        if current_time_str == time_1 or current_time_str == time_2:
            send_message(app, dlg, "文件传输助手", f"设备正常 {get_current_time()}")
            print(f"Good {get_current_time()}")

        # 等待一秒钟,避免重复执行
        time.sleep(60)

if __name__ == "__main__":
    main()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值