Python:程序员在每天不同时间发微信消息给女友
用python就可以給女友定時發(fā)提示消息了,而且不會漏過每一個關(guān)鍵時刻,每天早上起床、中午吃飯、晚上吃飯、晚上睡覺,都會準(zhǔn)時發(fā)消息給她了,而且還可以讓她學(xué)習(xí)英語單詞哦!
歡迎大家加入小編創(chuàng)建的Python行業(yè)交流群,有大牛答疑,有資源共享 有企業(yè)招人!是一個非常不錯的交流基地!群號:78486745在生日來臨之時,自動發(fā)祝福語。在節(jié)日來臨之時,比如三八婦女節(jié)、女神節(jié)、情人節(jié)、春節(jié)、圣誕節(jié),自動發(fā)問候語哦,再也不用擔(dān)心他說你沒有儀式感了?
最重要的時候,實(shí)時可以知道女友的情感情緒指數(shù)哦,再也不用擔(dān)心女友莫名其妙生氣了。
編寫思路
為了方便快速開發(fā),我們使用python中的wxpy模塊完成微信的基本操作。
首先,,我們設(shè)置一個config.ini配置文件,并從這個配置文件開始讀取信息。這些參數(shù)一看就懂,所以無需多言。
# 讀取配置文件 cf = configparser.ConfigParser() cf.read("./config.ini",encoding='UTF-8')# 設(shè)置女友的微信名稱,記住,不是微信ID也不是微信備注 # 你女友的微信名稱,記住,不是微信ID也不是微信備注 my_lady_wechat_name = cf.get("configuration", "my_lady_wechat_name")# 設(shè)置早上起床時間,中午吃飯時間,下午吃飯時間,晚上睡覺時間 say_good_morning = cf.get("configuration", "say_good_morning") say_good_lunch = cf.get("configuration", "say_good_lunch") say_good_dinner = cf.get("configuration", "say_good_dinner") say_good_dream = cf.get("configuration", "say_good_dream")# 設(shè)置女友生日信息 # 幾月,注意補(bǔ)全數(shù)字,為兩位數(shù),比如6月必須寫成06 birthday_month = cf.get("configuration", "birthday_month") # 幾號,注意補(bǔ)全數(shù)字,為兩位數(shù),比如6號必須寫成08 birthday_day = cf.get("configuration", "birthday_day")# 讀取早上起床時間,中午吃飯時間,下午吃飯時間,晚上睡覺時間的隨機(jī)提示語 # 一般這里的代碼不要改動,需要增加提示語可以自己打開對應(yīng)的文件修改 #早上起床問候語列表,數(shù)據(jù)來源于新浪微博 str_list_good_morning = '' with open("./remind_sentence/sentence_good_morning.txt", "r",encoding='UTF-8') as f:str_list_good_morning = f.readlines() print(str_list_good_morning)#中午吃飯問候語列表,數(shù)據(jù)來源于新浪微博 str_list_good_lunch = '' with open("./remind_sentence/sentence_good_lunch.txt", "r",encoding='UTF-8') as f:str_list_good_lunch = f.readlines() print(str_list_good_lunch)#晚上吃飯問候語列表,數(shù)據(jù)來源于新浪微博 str_list_good_dinner = '' with open("./remind_sentence/sentence_good_dinner.txt", "r",encoding='UTF-8') as f:str_list_good_dinner = f.readlines() print(str_list_good_dinner)#晚上睡覺問候語列表,數(shù)據(jù)來源于新浪微博 str_list_good_dream = '' with open("./remind_sentence/sentence_good_dream.txt", "r",encoding='UTF-8') as f:str_list_good_dream = f.readlines() print(str_list_good_dream)# 設(shè)置晚上睡覺問候語是否在原來的基礎(chǔ)上再加上每日學(xué)英語精句 # False表示否 True表示是 if((cf.get("configuration", "flag_learn_english")) == '1'):flag_learn_english = True else:flag_learn_english = False print(flag_learn_english)# 設(shè)置所有問候語結(jié)束是否加上表情符號 # False表示否 True表示是 str_emoj = "(? ̄?? ̄??)??°----(?′ `?)----(?ˉ?ε ˉ??)----(??? ? ???)----( ?? .? ?? )----(?? ??)----(●′ω`●)----(●・??・?●)----?_?----_(:qゝ∠)----(′;ω;`)----( `)3')----Σ((( つ??ω??)つ----╰(*′︶`*)╯----( ′′??′?` )----(′∩`。)----( ?? ?)----(。?_?)----( ?? _ ?? )----ヽ(*??????‵ *)----( ? 3?)----(; ′_ゝ`)----(*ˉ﹃ˉ)----(?'?`?)ノ゙----(。???。)----(? .?.? ?)----(′???`)----(。?ˇェˇ?。)----(???)----(`???′+)----(▼ _ ▼)----( ?????)----ㄟ(??? )ㄏ----(●'?'●)ノ?----(。?ˇ?ˇ?)----( ? ? )----( ′? ??`)----(?﹏?)----(????)----?(???? )----(???????)" str_list_emoj = str_emoj.split('----') if ((cf.get("configuration", "flag_wx_emoj")) == '1'):flag_wx_emoj = True else:flag_wx_emoj = False print(str_list_emoj)# 設(shè)置節(jié)日祝福語 # 情人節(jié)祝福語 str_Valentine = cf.get("configuration", "str_Valentine") print(str_Valentine)# 三八婦女節(jié)祝福語 str_Women = cf.get("configuration", "str_Women") print(str_Women)# 平安夜祝福語 str_Christmas_Eve = cf.get("configuration", "str_Christmas_Eve") print(str_Christmas_Eve)# 圣誕節(jié)祝福語 str_Christmas = cf.get("configuration", "str_Christmas") print(str_Christmas)# 她生日的時候的祝福語 str_birthday = cf.get("configuration", "str_birthday") print(str_birthday)如果你愿意,可以在上面對時間的判斷中,加入一些其他你想要的,這樣你女友就更開心啦!后期如果有時間,我將會加上以上節(jié)日問候功能。?
接著,開啟微信機(jī)器人,為了程序的健壯性,自動判斷一下操作系統(tǒng),根據(jù)不同操作系統(tǒng)執(zhí)行不同指令
# 啟動微信機(jī)器人,自動根據(jù)操作系統(tǒng)執(zhí)行不同的指令 # windows系統(tǒng)或macOS Sierra系統(tǒng)使用bot = Bot() # linux系統(tǒng)或macOS Terminal系統(tǒng)使用bot = Bot(console_qr=2) if('Windows' in platform.system()):# Windowsbot = Bot() elif('Darwin' in platform.system()):# MacOSXbot = Bot() elif('Linux' in platform.system()):# Linuxbot = Bot(console_qr=2,cache_path=True) else:# 自行確定print("無法識別你的操作系統(tǒng)類型,請自己設(shè)置")設(shè)置完相關(guān)參數(shù)以后,我們再來學(xué)習(xí)一下,如何每天教女友學(xué)英語
# 獲取每日勵志精句 def get_message():r = requests.get("http://open.iciba.com/dsapi/")note = r.json()['note']content = r.json()['content']return note,content只有每天的問候和節(jié)日問候是僅僅不夠的,我們必須時刻知道她的情緒指數(shù),這里可以使用snowNlp或者jieba來做分析,但是為了能夠在打包成exe可執(zhí)行文件時使得程序盡可能小,我們采取直接調(diào)用接口的方式來做。代碼如下:
# 接收女友消息監(jiān)聽器 # 女友微信名 my_girl_friend = bot.friends().search(my_lady_wechat_name)[0] # chats=my_girl_friend 表示接收消息的對象,也就是女友 # except_self=False 表示同時也接收自己發(fā)的消息,不需要接收自己消息的可以去掉 @bot.register(chats=my_girl_friend, except_self=False) def print_others(msg):# 輸出聊天內(nèi)容print(msg.text)# 做極其簡單的情感分析# 結(jié)果僅供參考,請勿完全相信postData = {'data':msg.text}response = post('https://bosonnlp.com/analysis/sentiment?analysisType=',data=postData)data = response.text# 情感評分指數(shù)(越接近1表示心情越好,越接近0表示心情越差)now_mod_rank = (data.split(',')[0]).replace('[[','')print("來自女友的消息:%s\n當(dāng)前情感得分:%s\n越接近1表示心情越好,越接近0表示心情越差,情感結(jié)果僅供參考,請勿完全相信!\n\n" % (msg.text, now_mod_rank))# 發(fā)送信息到文件傳輸助手mood_message = u"來自女友的消息:" + msg.text + "\n當(dāng)前情感得分:" + now_mod_rank + "\n越接近1表示心情越好,越接近0表示心情越差,情感結(jié)果僅供參考,請勿完全相信!\n\n"bot.file_helper.send(mood_message)教完女友學(xué)英語后,開始把我們的關(guān)心語發(fā)給他。這里涉及到wxpy模塊的相關(guān)操作,很簡單,看我的例子就會了。
# 發(fā)送消息給她 def send_message(your_message):try:# 對方的微信名稱my_friend = bot.friends().search(my_lady_wechat_name)[0]# 發(fā)送消息給對方my_friend.send(your_message)except:# 出問題時,發(fā)送信息到文件傳輸助手bot.file_helper.send(u"守護(hù)女友出問題了,趕緊去看看咋回事~") 最后,就是如何每天定時發(fā)關(guān)心語給女友的問題了。首先來個while循環(huán),365天無限關(guān)心?# 來個死循環(huán),24小時關(guān)心她while(True):# 提示print("守護(hù)中,時間:%s"% time.ctime())# 每天定時問候,早上起床,中午吃飯,晚上吃飯,晚上睡覺# 獲取時間,只獲取時和分,對應(yīng)的位置為倒數(shù)第13位到倒數(shù)第8位now_time = time.ctime()[-13:-8]if (now_time == say_good_morning):# 隨機(jī)取一句問候語message = choice(str_list_good_morning)# 是否加上隨機(jī)表情if(flag_wx_emoj):message = message + choice(str_list_emoj)send_message(message)print("提醒女友早上起床:%s" % time.ctime())…………這下面還有很多代碼,我就不列出來了…………# 延時60秒time.sleep(60)最后,輸入以下代碼開始守護(hù)女友模式吧~
# 開始守護(hù)女友t = Thread(target=start_care, name='start_care')t.start()使用教程
pip安裝下列包
pip install wxpy
pip install requests
設(shè)置以下內(nèi)容
完整代碼
# -*- coding:utf-8 -*- from __future__ import unicode_literals from wxpy import * from requests import get from requests import post from platform import system from os import chdir from random import choice from threading import Thread import configparser import time import sys# 獲取每日勵志精句 def get_message():r = get("http://open.iciba.com/dsapi/")note = r.json()['note']content = r.json()['content']return note,content# 發(fā)送消息給她 def send_message(your_message):try:# 對方的微信名稱my_friend = bot.friends().search(my_lady_wechat_name)[0]# 發(fā)送消息給對方my_friend.send(your_message)except:# 出問題時,發(fā)送信息到文件傳輸助手bot.file_helper.send(u"守護(hù)女友出問題了,趕緊去看看咋回事~")# 在規(guī)定時間內(nèi)進(jìn)行關(guān)心她操作 def start_care():# 待發(fā)送的內(nèi)容,先置為空message = ""# 來個死循環(huán),24小時關(guān)心她while(True):# 提示print("守護(hù)中,時間:%s"% time.ctime())# 每天定時問候,早上起床,中午吃飯,晚上吃飯,晚上睡覺# 獲取時間,只獲取時和分,對應(yīng)的位置為倒數(shù)第13位到倒數(shù)第8位now_time = time.ctime()[-13:-8]if (now_time == say_good_morning):# 隨機(jī)取一句問候語message = choice(str_list_good_morning)# 是否加上隨機(jī)表情if(flag_wx_emoj):message = message + choice(str_list_emoj)send_message(message)print("提醒女友早上起床:%s" % time.ctime())elif (now_time == say_good_lunch):message = choice(str_list_good_lunch)# 是否加上隨機(jī)表情if(flag_wx_emoj):message = message + choice(str_list_emoj)send_message(message)print("提醒女友中午吃飯:%s" % time.ctime())elif (now_time == say_good_dinner):message = choice(str_list_good_dinner)# 是否加上隨機(jī)表情if(flag_wx_emoj):message = message + choice(str_list_emoj)send_message(message)print("提醒女友晚上吃飯:%s" % time.ctime())elif (now_time == say_good_dream):# 是否在結(jié)尾加上每日學(xué)英語if(flag_learn_english):note, content = get_message()message = choice(str_list_good_dream) + "\n\n" + "順便一起來學(xué)英語哦:\n" + "原文: " + content + "\n\n翻譯: " + noteelse:message = choice(str_list_good_dream)# 是否加上隨機(jī)表情if(flag_wx_emoj):message = message + choice(str_list_emoj)send_message(message)print("提醒女友晚上睡覺:%s" % time.ctime())# 節(jié)日問候語festival_month = time.strftime('%m', time.localtime())festival_day = time.strftime('%d', time.localtime())if(festival_month == '02' and festival_day == '14' and now_time == "08:00"):send_message(str_Valentine)print("發(fā)送情人節(jié)祝福:%s" % time.ctime())elif(festival_month == '03' and festival_day == '08' and now_time == "08:00"):send_message(str_Women)print("發(fā)送三八婦女節(jié)祝福:%s" % time.ctime())elif(festival_month == '12' and festival_day == '24' and now_time == "00:00"):send_message(str_Christmas_Eve)print("發(fā)送平安夜祝福:%s" % time.ctime())elif(festival_month == '12' and festival_day == '25' and now_time == "00:00"):send_message(str_Christmas)print("發(fā)送圣誕節(jié)祝福:%s" % time.ctime())# 生日問候語if(festival_month == birthday_month and festival_day == birthday_day and now_time == "00:00"):send_message(str_birthday)print("發(fā)送生日祝福:%s" % time.ctime())# 每60秒檢測一次time.sleep(60)if __name__ == "__main__":# 若發(fā)現(xiàn)讀取取配置文件出錯,可以取消注釋下面這行,一般在pycharm環(huán)境下才需要增加# 設(shè)置當(dāng)前文件所在的目錄為當(dāng)前工作路徑# chdir(sys.path[0])# 啟動微信機(jī)器人,自動根據(jù)操作系統(tǒng)執(zhí)行不同的指令# windows系統(tǒng)或macOS Sierra系統(tǒng)使用bot = Bot()# linux系統(tǒng)或macOS Terminal系統(tǒng)使用bot = Bot(console_qr=2)if('Windows' in system()):# Windowsbot = Bot()elif('Darwin' in system()):# MacOSXbot = Bot()elif('Linux' in system()):# Linuxbot = Bot(console_qr=2,cache_path=True)else:# 自行確定print("無法識別你的操作系統(tǒng)類型,請自己設(shè)置")# 讀取配置文件cf = configparser.ConfigParser()cf.read("./config.ini",encoding='UTF-8')# 設(shè)置女友的微信名稱,記住,不是微信ID也不是微信備注# 你女友的微信名稱,記住,不是微信ID也不是微信備注my_lady_wechat_name = cf.get("configuration", "my_lady_wechat_name")# 設(shè)置早上起床時間,中午吃飯時間,下午吃飯時間,晚上睡覺時間say_good_morning = cf.get("configuration", "say_good_morning")say_good_lunch = cf.get("configuration", "say_good_lunch")say_good_dinner = cf.get("configuration", "say_good_dinner")say_good_dream = cf.get("configuration", "say_good_dream")# 設(shè)置女友生日信息# 幾月,注意補(bǔ)全數(shù)字,為兩位數(shù),比如6月必須寫成06birthday_month = cf.get("configuration", "birthday_month")# 幾號,注意補(bǔ)全數(shù)字,為兩位數(shù),比如6號必須寫成08birthday_day = cf.get("configuration", "birthday_day")# 讀取早上起床時間,中午吃飯時間,下午吃飯時間,晚上睡覺時間的隨機(jī)提示語# 一般這里的代碼不要改動,需要增加提示語可以自己打開對應(yīng)的文件修改#早上起床問候語列表,數(shù)據(jù)來源于新浪微博str_list_good_morning = ''with open("./remind_sentence/sentence_good_morning.txt", "r",encoding='UTF-8') as f:str_list_good_morning = f.readlines()print(str_list_good_morning)#中午吃飯問候語列表,數(shù)據(jù)來源于新浪微博str_list_good_lunch = ''with open("./remind_sentence/sentence_good_lunch.txt", "r",encoding='UTF-8') as f:str_list_good_lunch = f.readlines()print(str_list_good_lunch)#晚上吃飯問候語列表,數(shù)據(jù)來源于新浪微博str_list_good_dinner = ''with open("./remind_sentence/sentence_good_dinner.txt", "r",encoding='UTF-8') as f:str_list_good_dinner = f.readlines()print(str_list_good_dinner)#晚上睡覺問候語列表,數(shù)據(jù)來源于新浪微博str_list_good_dream = ''with open("./remind_sentence/sentence_good_dream.txt", "r",encoding='UTF-8') as f:str_list_good_dream = f.readlines()print(str_list_good_dream)# 設(shè)置晚上睡覺問候語是否在原來的基礎(chǔ)上再加上每日學(xué)英語精句# False表示否 True表示是if((cf.get("configuration", "flag_learn_english")) == '1'):flag_learn_english = Trueelse:flag_learn_english = Falseprint(flag_learn_english)# 設(shè)置所有問候語結(jié)束是否加上表情符號# False表示否 True表示是str_emoj = "(? ̄?? ̄??)??°----(?′ `?)----(?ˉ?ε ˉ??)----(??? ? ???)----( ?? .? ?? )----(?? ??)----(●′ω`●)----(●・??・?●)----?_?----_(:qゝ∠)----(′;ω;`)----( `)3')----Σ((( つ??ω??)つ----╰(*′︶`*)╯----( ′′??′?` )----(′∩`。)----( ?? ?)----(。?_?)----( ?? _ ?? )----ヽ(*??????‵ *)----( ? 3?)----(; ′_ゝ`)----(*ˉ﹃ˉ)----(?'?`?)ノ゙----(。???。)----(? .?.? ?)----(′???`)----(。?ˇェˇ?。)----(???)----(`???′+)----(▼ _ ▼)----( ?????)----ㄟ(??? )ㄏ----(●'?'●)ノ?----(。?ˇ?ˇ?)----( ? ? )----( ′? ??`)----(?﹏?)----(????)----?(???? )----(???????)"str_list_emoj = str_emoj.split('----')if ((cf.get("configuration", "flag_wx_emoj")) == '1'):flag_wx_emoj = Trueelse:flag_wx_emoj = Falseprint(str_list_emoj)# 設(shè)置節(jié)日祝福語# 情人節(jié)祝福語str_Valentine = cf.get("configuration", "str_Valentine")print(str_Valentine)# 三八婦女節(jié)祝福語str_Women = cf.get("configuration", "str_Women")print(str_Women)# 平安夜祝福語str_Christmas_Eve = cf.get("configuration", "str_Christmas_Eve")print(str_Christmas_Eve)# 圣誕節(jié)祝福語str_Christmas = cf.get("configuration", "str_Christmas")print(str_Christmas)# 她生日的時候的祝福語str_birthday = cf.get("configuration", "str_birthday")print(str_birthday)# 開始守護(hù)女友t = Thread(target=start_care, name='start_care')t.start()# 接收女友消息監(jiān)聽器# 女友微信名 my_girl_friend = bot.friends().search(my_lady_wechat_name)[0] @bot.register(chats=my_girl_friend, except_self=False) def print_others(msg):# 輸出聊天內(nèi)容print(msg.text)# 可采用snownlp或者jieba等進(jìn)行分詞、情感分析,由于打包后文件體積太大,故暫時不采用這種方式# 僅僅是直接調(diào)用網(wǎng)絡(luò)接口# 做極其簡單的情感分析# 結(jié)果僅供參考,請勿完全相信postData = {'data':msg.text}response = post('https://bosonnlp.com/analysis/sentiment?analysisType=',data=postData)data = response.text# 情感評分指數(shù)(越接近1表示心情越好,越接近0表示心情越差)now_mod_rank = (data.split(',')[0]).replace('[[','')print("來自女友的消息:%s\n當(dāng)前情感得分:%s\n越接近1表示心情越好,越接近0表示心情越差,情感結(jié)果僅供參考,請勿完全相信!\n\n" % (msg.text, now_mod_rank))# 發(fā)送信息到文件傳輸助手mood_message = u"來自女友的消息:" + msg.text + "\n當(dāng)前情感得分:" + now_mod_rank + "\n越接近1表示心情越好,越接近0表示心情越差,情感結(jié)果僅供參考,請勿完全相信!\n\n"bot.file_helper.send(mood_message)總結(jié)
以上是生活随笔為你收集整理的Python:程序员在每天不同时间发微信消息给女友的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【转载】一次项目管理交流会总结
- 下一篇: 前端开发JS的学习之AngularJS库