亲爱的,收到我每天的祝福了吗? (程序员向)

in #cn6 years ago

img

小薇最近每天早晚都会收到一条微信信息,内容很简单,就是“早安”和“晚安”。

这是隔壁计算机系的一个男生发来的微信。

那是一个干干净净的男生,小薇对他说不上有好感,但是也不厌恶。干净的男生总是让人感觉很舒服。

信息已经持续发了三周了,从不间断。

这让小薇有点儿在意了,于是小薇给男生回了信息,问道:“你每天早晚发信息给我,是什么意思呢?”

很快小薇收到回复:“没什么,我就是希望你能“早安”,“晚安”。”

小薇觉得这男生很有趣,于是就聊上了。

慢慢的,聊天发展成了约会。

约会了好几次后,男生对小薇表白了。

小薇开心的答应了。

值得一提的是,即使是小薇成为了男生的女友,每日的早安晚安的问候也从未间断,哪怕再累再晚。

每日的早安晚安,仿佛成为了小薇生活的一部分,那么的自然,那么的让人放心。


看了小故事,是不是很感动?每天有人给你发信息问候早安晚安,是不是一件很奢侈的事情?

但是仔细看小故事,这个男生是计算机系的。

再仔细看,这信息从来没有间断。

你有没产生一丝怀疑?

没错,这货就是用程序办成这件事的。

怎么做到的呢?我来教大伙写一个自动定时微信发信息的小程序吧。


我们使用python来写代码,首先安装WXPY库,这是一个使用网页微信发送信息的依赖库。

说明文档:https://wxpy.readthedocs.io/zh/latest/index.html

1.加载库和登陆

from wxpy import *
bot = Bot()

第一行是加载库,第二行是登陆。会跳出一个二维码让你扫,扫描了程序就登陆了网页版微信。

2.加载发送人

my_friend =bot.friends().search(u'测试一个')[0]

这个叫“测试一个”的就是我要发送的人,如果有重名的,请修改后面的序号“0”,自己测试多几次,别发错人了。

3.发送信息

now = datetime.datetime.now()   #获取当前时间
if now.hour==8 and now.minute==0:# 到达设定时间,结束内循环
   my_friend.send("宝贝,早上好。上班辛苦了,加油!爱你:)")
   print("8点提示发送完毕")

第一行是获取当前时间,下面的if是判断时间是否到达8:00,如果到达,就发送信息,这里假设发送的信息对象是老婆,不妨写肉麻一点:“宝贝,早上好。上班辛苦了,加油!爱你:)”

4.增加循环

while True:
    now = datetime.datetime.now()   #获取当前时间
    if now.hour==8 and now.minute==0:# 到达设定时间,结束内循环
       my_friend.send("宝贝,早上好。上班辛苦了,加油!爱你:)")
       print("8点提示发送完毕")
       break
    # 不到时间就等5秒之后再次检测
    time.sleep(1)
    print("还没到时间")

不增加循环代码是跑不起来的,因为只会判断一次,所以增加一个循环,1秒钟重新判断一次时间是否到了。

5.增加更多的问候

while True:

    while True:
        now = datetime.datetime.now()   #获取当前时间
        if now.hour==8 and now.minute==0:# 到达设定时间,结束内循环
           my_friend.send("宝贝,早上好。上班辛苦了,加油!爱你:)")
           print("8点提示发送完毕")
           break
        # 不到时间就等5秒之后再次检测
        time.sleep(1)
        print("还没到时间")

    while True:
        now = datetime.datetime.now()   #获取当前时间
        if now.hour==11 and now.minute==0:# 到达设定时间,结束内循环
           my_friend.send("累了吧?喝口水休息一下再继续工作吧。等你下班哦")
           print("11点提示发送完毕")
           break
        # 不到时间就等5秒之后再次检测
        time.sleep(1)
        print("还没到时间")

    while True:
        now = datetime.datetime.now()   #获取当前时间
        if now.hour==15 and now.minute==0:# 到达设定时间,结束内循环
           my_friend.send("肚子饿了吧?帮你叫你了你最喜欢的奶茶,外卖一会就送到,爱你哦:)")
           print("15点提示发送完毕,顺便提醒自己去叫外卖奶茶。要不要考虑接入外卖API?")
           break
        # 不到时间就等5秒之后再次检测
        time.sleep(1)
        print("还没到时间")

    while True:
        now = datetime.datetime.now()   #获取当前时间
        if now.hour==23 and now.minute==0:# 到达设定时间,结束内循环
           my_friend.send("宝贝,熬夜对皮肤不好哦,早点睡觉吧,晚安,爱你。")
           print("23点提示发送完毕")
           break
        # 不到时间就等5秒之后再次检测
        time.sleep(1)
        print("还没到时间")

只是一个早安怎么够?当然要全天无死角的问候,才能彰显暖男本色,所以我们在各个时间段都增加了问候语。

恩,执行看看效果如何。

img

恩,正常运行,效果良好。另外避免单调,可以建设一个留言库,天天变幻使用。

今天跑了一天做测试,老婆对我说:“老公,我觉得你今天变了,对我好好哦,我爱你”

实测效果良好,各位亲跑起程序来吧。

另外提供一个渣男版本:

my_friend1 =bot.friends().search(u'小明')[0]
my_friend2 =bot.friends().search(u'小爱')[0]
my_friend3 =bot.friends().search(u'小天')[0]
my_friend4 =bot.friends().search(u'小东')[0]
my_friend5 =bot.friends().search(u'小西')[0]
my_friend6 =bot.friends().search(u'小陈')[0]
my_friend7 =bot.friends().search(u'小坏')[0]
my_friend8 =bot.friends().search(u'小北')[0]

使用请小心,谨防好船结局

下面放上完整源代码,可以直接抄去用:

from wxpy import *
import time
import datetime


bot = Bot()
my_friend =bot.friends().search(u'测试一个')[0]
while True:

    while True:
        now = datetime.datetime.now()   #获取当前时间
        if now.hour==8 and now.minute==0:# 到达设定时间,结束内循环
           my_friend.send("宝贝,早上好。上班辛苦了,加油!爱你:)")
           print("8点提示发送完毕")
           break
        # 不到时间就等5秒之后再次检测
        time.sleep(1)
        print("还没到时间")

    while True:
        now = datetime.datetime.now()   #获取当前时间
        if now.hour==11 and now.minute==0:# 到达设定时间,结束内循环
           my_friend.send("累了吧?喝口水休息一下再继续工作吧。等你下班哦")
           print("11点提示发送完毕")
           break
        # 不到时间就等5秒之后再次检测
        time.sleep(1)
        print("还没到时间")

    while True:
        now = datetime.datetime.now()   #获取当前时间
        if now.hour==15 and now.minute==0:# 到达设定时间,结束内循环
           my_friend.send("肚子饿了吧?帮你叫你了你最喜欢的奶茶,外卖一会就送到,爱你哦:)")
           print("15点提示发送完毕,顺便提醒自己去叫外卖奶茶。要不要考虑接入外卖API?")
           break
        # 不到时间就等5秒之后再次检测
        time.sleep(1)
        print("还没到时间")

    while True:
        now = datetime.datetime.now()   #获取当前时间
        if now.hour==23 and now.minute==0:# 到达设定时间,结束内循环
           my_friend.send("宝贝,熬夜对皮肤不好哦,早点睡觉吧,晚安,爱你。")
           print("23点提示发送完毕")
           break
        # 不到时间就等5秒之后再次检测
        time.sleep(1)
        print("还没到时间")
Sort:  

It's fab not just cool!

這樣就不浪漫了

Posted using Partiko Android

Hi @maiyude!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your UA account score is currently 3.496 which ranks you at #6327 across all Steem accounts.
Your rank has improved 161 places in the last three days (old rank 6488).

In our last Algorithmic Curation Round, consisting of 348 contributions, your post is ranked at #321.

Evaluation of your UA score:
  • You're on the right track, try to gather more followers.
  • The readers like your work!
  • Try to work on user engagement: the more people that interact with you via the comments, the higher your UA score!

Feel free to join our @steem-ua Discord server

YOU JUST GOT UPVOTED

Congratulations,
you just received a 13.53% upvote from @steemhq - Community Bot!

Wanna join and receive free upvotes yourself?
Vote for steemhq.witness on Steemit or directly on SteemConnect and join the Community Witness.

This service was brought to you by SteemHQ.com

Coin Marketplace

STEEM 0.17
TRX 0.13
JST 0.027
BTC 59046.50
ETH 2654.73
USDT 1.00
SBD 2.50