微信定时自动给指定好友发送信息

 

自动化任务:微信界面模拟

在这篇文章中,我们将探讨一个有趣的自动化任务:模拟微信界面。我们将使用Python编写代码,以便在特定时间自动执行一些操作。

任务概述

我们的目标是在每天的10:00 AM(目标时间)之后,执行以下操作:

  1. 从文本文件中读取一系列文本行。
  2. 将每行文本复制到剪贴板。
  3. 在微信或其他聊天应用中粘贴文本并发送。

代码实现

以下是我们的Python代码:

import random
import pyperclip
import time
import pyautogui
from datetime import datetime, time as dt_time

def send_text_to_clipboard_and_paste(text):
    pyperclip.copy(text)
    pyautogui.hotkey('ctrl', 'v')
    pyautogui.press('enter')

def main():
    target_time = dt_time(10, 0)  # 目标时间设定为每天的10:00
    while True:
        now = datetime.now().time()
        if now >= target_time:
            break
        print(f"当前时间:{now}, 未到目标时间:{target_time}")
        time.sleep(3)  # 间隔3秒检查一次

    # 在目标时间执行任务
    with open('str.txt', 'r', encoding='utf-8') as file:
        lines = file.readlines()
    jc()
    for line in lines:
        line = line.strip()
        send_text_to_clipboard_and_paste(line)
        time.sleep(random.uniform(0.5, 1.5))

def jc():
    # 循环检测这个图片,检测到了就是返回true
    while True:
        location = pyautogui.locateOnScreen('xb.png', confidence=0.8)
        if location is not None:
            print(f"检测到了{time.time()}")
            print(location)
            break
        else:
            print(f"没有检测到{time.time()}")
            print(location)
            time.sleep(1)

if __name__ == "__main__":
    main()

解释

  • 我们使用pyperclip库来操作剪贴板,将文本复制到剪贴板。
  • pyautogui库用于模拟键盘按键和鼠标点击。
  • jc()函数用于检测特定图片(在这里是xb.png),以确保微信界面已打开。
THE END