python脚本给微信好友自动发晚安消息

一、引言

做这个的原因是因为有一个朋友特别喜欢熬夜,所以就写一个脚本,每天晚上定时提醒Ta睡觉。网上找了点资料,在别人基础上改了一下。效果图如下(视频放不了,gif过大):
在这里插入图片描述

二、python源代码

import win32clipboard as w
import win32con
import win32api
import win32gui
import time
import threading
import datetime
sentence = ['睡觉呀,冲呀!', '睡觉啦啦啦啦啦', '你是睡着了呢,还是睡着了呢,还是睡着了呢', '早点睡,明天你还要拯救地球呀。',
            '别玩手机了,赶紧睡啦', '注意休息,早睡早起,精神好,人会更漂亮。',  '既睡之,则安之,我就不打扰了',
            '其实你不必这样,睡觉对你来说就是重生。', '睡觉啦,熬夜不仅会让你日渐消瘦,还会让你会长胖哦。',
            '睡觉啦,熬夜一时爽,熬后火葬场', '早睡早起,锻炼身体。', '黑夜不会亏待晚睡的人,因为它会让你脱发。',
            '你想一夜暴富吗?那就跟我一起关灯睡觉吧', '没有足够的睡眠是成为不了公主的', '安啦!早点睡!', '我有一个小小的愿望,就是希望你能早点睡觉。',
            '把所有的烦恼都抛掉,拉上窗帘,挂上月亮,好好睡一觉。', '月亮不睡你不睡,你是人间小美味?']

# 轮询时间间隔:多久发一次信息。单位ms
sendMsgInterval = 6;
# 短暂的暂停时间,模拟人为操作,以防微信反应不过来
normalWaitingTime = 0.3;
# 检测取消口令
cancelToken = '已经睡觉啦';

#把文字放入剪贴板
def setText(aString):
    w.OpenClipboard()
    w.EmptyClipboard()
    w.SetClipboardData(win32con.CF_UNICODETEXT,aString)
    w.CloseClipboard()

#模拟ctrl+V
def ctrlV():
    win32api.keybd_event(17,0,0,0) #ctrl
    win32api.keybd_event(86,0,0,0) #V
    win32api.keybd_event(86,0,win32con.KEYEVENTF_KEYUP,0)#释放按键
    win32api.keybd_event(17,0,win32con.KEYEVENTF_KEYUP,0)

#模拟ctrl+C
def ctrlC():
    win32api.keybd_event(17,0,0,0) #ctrl
    win32api.keybd_event(0x43,0,0,0) #V
    win32api.keybd_event(0x43,0,win32con.KEYEVENTF_KEYUP,0)#释放按键
    win32api.keybd_event(17,0,win32con.KEYEVENTF_KEYUP,0)

#模拟alt+s
def altS():
    win32api.keybd_event(18,0,0,0)
    win32api.keybd_event(83,0,0,0)
    win32api.keybd_event(83,0,win32con.KEYEVENTF_KEYUP,0)
    win32api.keybd_event(18,0,win32con.KEYEVENTF_KEYUP,0)

# 模拟enter
def enter():
    win32api.keybd_event(13,0,0,0)
    win32api.keybd_event(13,0,win32con.KEYEVENTF_KEYUP,0)
#模拟单击
def click():
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
#模拟双击
def dbClick():
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
    time.sleep(normalWaitingTime);
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
#移动鼠标的位置
def movePos(x,y):
    win32api.SetCursorPos((x,y))

# 判断时间是不是准确的
def judgeCurrentTime ():
    d_time = datetime.datetime.strptime(str(datetime.datetime.now().date()) + '00:00', '%Y-%m-%d%H:%M')
    d_time1 = datetime.datetime.strptime(str(datetime.datetime.now().date()) + '00:40', '%Y-%m-%d%H:%M')
    n_time = datetime.datetime.now()
    return n_time > d_time and n_time < d_time1

def getCopyText():
    w.OpenClipboard()
    copy_text = w.GetClipboardData(win32con.CF_TEXT)
    w.CloseClipboard()
    return copy_text

# 给好友发送信息
def openChatWindow(people):
    time.sleep(normalWaitingTime)
    print('打开聊天窗口')
    #获取鼠标当前位置
    hwnd = win32gui.FindWindow("WeChatMainWndForPC", None)
    win32gui.ShowWindow(hwnd,win32con.SW_SHOW)
    win32gui.MoveWindow(hwnd,0,0,1000,700,True)
    time.sleep(normalWaitingTime)
    #1.移动鼠标到通讯录位置,单击打开通讯录
    movePos(28,80)
    click()
    #2.移动鼠标到搜索框,单击,输入要搜索的名字
    movePos(148,35)
    click()
    setText(people)
    ctrlV()
    # 清空操作
    time.sleep(normalWaitingTime)
    setText('')
    time.sleep(1) #别问我为什么要停1秒,问就是给微信一个反应的时间,他反应慢反应不过来,其他位置暂停的原因同样
    # 3.选中联系人,进行聊天
    enter()
    time.sleep(normalWaitingTime)

def sendMsg(count):
    global timer;

    time.sleep(normalWaitingTime)
    # 发信息前要检测是否取消
    movePos(400,450);
    time.sleep(normalWaitingTime)
    dbClick();
    time.sleep(normalWaitingTime);
    ctrlC();
    time.sleep(normalWaitingTime);
    shuangText = (getCopyText().decode('GB2312')).strip();

    # 点击聊天界面
    movePos(400,600);
    dbClick();
    time.sleep(normalWaitingTime);

    # 1. 轮询的第一种情况,正常发信息
    if len(shuangText) ==0:
        count = (count + 1) % (len(sentence));
        # 4.复制要发送的消息,发送
        setText(sentence[count])
        ctrlV()
        altS()
        time.sleep(normalWaitingTime)
        setText('')
        timer = threading.Timer(sendMsgInterval, sendMsg, [count])
        timer.start();
    # 2. 检测到对方发了取消指令,就停止发信息
    elif (shuangText == cancelToken):
        setText('好的呢,晚安~')
        time.sleep(normalWaitingTime)
        ctrlV()
        time.sleep(normalWaitingTime)
        altS()
        time.sleep(normalWaitingTime)
        setText('')
        print(shuangText, '取消了')
        timer.cancel()
    # 3. 如果对方发了其他消息(非图片和emoji表情),就通知她输入取消指令
    else:
        setText('如想取消,请输入指令: ' + cancelToken)
        ctrlV()
        altS()
        time.sleep(normalWaitingTime)
        setText('')
        timer = threading.Timer(sendMsgInterval, sendMsg, [count])
        timer.start();

# 要发信息的对象名字
people= '清风'
# 为了遍历词条列表
count = -1;
# 打开聊天窗口
openChatWindow(people)
# 短暂的反应时间
time.sleep(3);
# 轮询发请求喊睡觉
sendMsg(-1)

注意:运行该代码之前,微信图标要放在桌面左上角。不然它点不到

三、结语

代码写到很简单,功能不是很齐全。并且需要自己每天在自己的电脑上跑这个项目。后面的人有需要,自己慢慢改进吧。

  • 3
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值