Python:每天不同時(shí)間段通過微信發(fā)消息提醒女友

簡(jiǎn)介

有時(shí)候,你很想關(guān)心她,但是你太忙了,以至于她一直抱怨,覺得你不夠關(guān)心她。你暗自下決心,下次一定要準(zhǔn)時(shí)發(fā)消息給她,哪怕是幾句話,可是你又忘記了。你覺得自己很委屈??,但是她又覺得你不負(fù)責(zé)。

現(xiàn)在,再不用擔(dān)心了,用python就可以給女友定時(shí)發(fā)提示消息了,而且不會(huì)漏過每一個(gè)關(guān)鍵時(shí)刻,每天早上起床、中午吃飯、晚上吃飯、晚上睡覺,都會(huì)準(zhǔn)時(shí)發(fā)消息給她了,而且還可以讓她學(xué)習(xí)英語單詞哦!

在生日來臨之時(shí),自動(dòng)發(fā)祝福語。在節(jié)日來臨之時(shí),比如三八婦女節(jié)、女神節(jié)、情人節(jié)、春節(jié)、圣誕節(jié),自動(dòng)發(fā)問候語哦,再也不用擔(dān)心他說你沒有儀式感了??

最重要的時(shí)候,實(shí)時(shí)可以知道女友的情感情緒指數(shù)哦,再也不用擔(dān)心女友莫名其妙生氣了。

學(xué)習(xí)Python中的小伙伴,需要學(xué)習(xí)資料的話,可以到我的微信公眾號(hào):Python學(xué)習(xí)知識(shí)圈,后臺(tái)回復(fù):“01”,即可拿Python學(xué)習(xí)資料
這里有我自己整理了一套最新的python系統(tǒng)學(xué)習(xí)教程,包括從基礎(chǔ)的python腳本到web開發(fā)、爬蟲、數(shù)據(jù)分析、數(shù)據(jù)可視化、機(jī)器學(xué)習(xí)等。送給正在學(xué)習(xí)python的小伙伴!這里是python學(xué)習(xí)者聚集地,歡迎初學(xué)和進(jìn)階中的小伙伴!

編寫思路

為了方便快速開發(fā),我們使用python中的wxpy模塊完成微信的基本操作。

首先,,我們?cè)O(shè)置一個(gè)config.ini配置文件,并從這個(gè)配置文件開始讀取信息。這些參數(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è)置早上起床時(shí)間,中午吃飯時(shí)間,下午吃飯時(shí)間,晚上睡覺時(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")
# 幾號(hào),注意補(bǔ)全數(shù)字,為兩位數(shù),比如6號(hào)必須寫成08
birthday_day = cf.get("configuration", "birthday_day")

# 讀取早上起床時(shí)間,中午吃飯時(shí)間,下午吃飯時(shí)間,晚上睡覺時(shí)間的隨機(jī)提示語
# 一般這里的代碼不要改動(dòng),需要增加提示語可以自己打開對(duì)應(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é)束是否加上表情符號(hào)
# 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)

# 她生日的時(shí)候的祝福語
str_birthday = cf.get("configuration", "str_birthday")
print(str_birthday)

如果你愿意,可以在上面對(duì)時(shí)間的判斷中,加入一些其他你想要的,這樣你女友就更開心啦!后期如果有時(shí)間,我將會(huì)加上以上節(jié)日問候功能。??

接著,開啟微信機(jī)器人,為了程序的健壯性,自動(dòng)判斷一下操作系統(tǒng),根據(jù)不同操作系統(tǒng)執(zhí)行不同指令

# 啟動(dòng)微信機(jī)器人,自動(dòng)根據(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()):
    # Windows
    bot = Bot()
elif('Darwin' in platform.system()):
    # MacOSX
    bot = Bot()
elif('Linux' in platform.system()):
    # Linux
    bot = Bot(console_qr=2,cache_path=True)
else:
    # 自行確定
    print("無法識(shí)別你的操作系統(tǒng)類型,請(qǐng)自己設(shè)置")
image.gif

設(shè)置完相關(guān)參數(shù)以后,我們?cè)賮韺W(xué)習(xí)一下,如何每天教女友學(xué)英語

# 獲取每日勵(lì)志精句
def get_message():
    r = requests.get("http://open.iciba.com/dsapi/")
    note = r.json()['note']
    content = r.json()['content']
    return note,content
image.gif

只有每天的問候和節(jié)日問候是僅僅不夠的,我們必須時(shí)刻知道她的情緒指數(shù),這里可以使用snowNlp或者jieba來做分析,但是為了能夠在打包成exe可執(zhí)行文件時(shí)使得程序盡可能小,我們采取直接調(diào)用接口的方式來做。代碼如下:

# 接收女友消息監(jiān)聽器
# 女友微信名
my_girl_friend = bot.friends().search(my_lady_wechat_name)[0]
# chats=my_girl_friend 表示接收消息的對(duì)象,也就是女友
# except_self=False 表示同時(shí)也接收自己發(fā)的消息,不需要接收自己消息的可以去掉
@bot.register(chats=my_girl_friend, except_self=False)
def print_others(msg):
    # 輸出聊天內(nèi)容
    print(msg.text)

    # 做極其簡(jiǎn)單的情感分析
    # 結(jié)果僅供參考,請(qǐng)勿完全相信
    postData = {'data':msg.text}
    response = post('https://bosonnlp.com/analysis/sentiment?analysisType=',data=postData)
    data = response.text

    # 情感評(píng)分指數(shù)(越接近1表示心情越好,越接近0表示心情越差)
    now_mod_rank = (data.split(',')[0]).replace('[[','')
    print("來自女友的消息:%s\n當(dāng)前情感得分:%s\n越接近1表示心情越好,越接近0表示心情越差,情感結(jié)果僅供參考,請(qǐng)勿完全相信!\n\n" % (msg.text, now_mod_rank))

    # 發(fā)送信息到文件傳輸助手
    mood_message = u"來自女友的消息:" + msg.text + "\n當(dāng)前情感得分:" + now_mod_rank + "\n越接近1表示心情越好,越接近0表示心情越差,情感結(jié)果僅供參考,請(qǐng)勿完全相信!\n\n"
    bot.file_helper.send(mood_message)
image.gif

教完女友學(xué)英語后,開始把我們的關(guān)心語發(fā)給他。這里涉及到wxpy模塊的相關(guān)操作,很簡(jiǎn)單,看我的例子就會(huì)了。

# 發(fā)送消息給她
def send_message(your_message):
    try:
        # 對(duì)方的微信名稱
        my_friend = bot.friends().search(my_lady_wechat_name)[0]

        # 發(fā)送消息給對(duì)方
        my_friend.send(your_message)
    except:

        # 出問題時(shí),發(fā)送信息到文件傳輸助手
        bot.file_helper.send(u"守護(hù)女友出問題了,趕緊去看看咋回事~")
image.gif

最后,就是如何每天定時(shí)發(fā)關(guān)心語給女友的問題了。首先來個(gè)while循環(huán),365天無限關(guān)心??

    # 來個(gè)死循環(huán),24小時(shí)關(guān)心她
    while(True):

        # 提示
        print("守護(hù)中,時(shí)間:%s"% time.ctime())

        # 每天定時(shí)問候,早上起床,中午吃飯,晚上吃飯,晚上睡覺
        # 獲取時(shí)間,只獲取時(shí)和分,對(duì)應(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())

        …………這下面還有很多代碼,我就不列出來了…………

        # 延時(shí)60秒
        time.sleep(60)
image.gif

最后,輸入以下代碼開始守護(hù)女友模式吧~

    # 開始守護(hù)女友
    t = Thread(target=start_care, name='start_care')
    t.start()
image.gif

使用教程

  1. pip安裝下列包
  • pip install wxpy
  • pip install requests
  1. 設(shè)置以下內(nèi)容
[configuration]

# 設(shè)置女友的微信名稱,記住,不是微信ID也不是微信備注
my_lady_wechat_name = 小強(qiáng)子

# 設(shè)置女友生日信息
# 若某一項(xiàng)月份或者日期不想設(shè)置,請(qǐng)輸入99,不能留空
# 幾月,注意補(bǔ)全數(shù)字,為兩位數(shù),比如6月必須寫成06
birthday_month = 03
# 幾號(hào),注意補(bǔ)全數(shù)字,為兩位數(shù),比如6號(hào)必須寫成08
birthday_day = 18

# 設(shè)置早上起床時(shí)間,中午吃飯時(shí)間,下午吃飯時(shí)間,晚上睡覺時(shí)間
# 若某一項(xiàng)時(shí)間不想設(shè)置,請(qǐng)輸入99:00,不能留空
say_good_morning = 03:09
say_good_lunch = 03:10
say_good_dinner = 03:11
say_good_dream = 03:12

# 設(shè)置晚上睡覺問候語是否在原來的基礎(chǔ)上再加上每日學(xué)英語精句
# 1表示是,0表示否
flag_learn_english = 1

# 設(shè)置所有問候語結(jié)束是否加上表情符號(hào)
# 1表示是,0表示否
flag_wx_emoj = 1

# 設(shè)置節(jié)日祝福語
# 情人節(jié)祝福語
str_Valentine = 親愛的,情人節(jié)快樂!我想和你一起分享生命中的每一天,直到永遠(yuǎn)。

# 三八婦女節(jié)祝福語
str_Women = 嘿,女神節(jié)到了,祝我的女神開心快樂!你每天都是那么好看^_^

# 平安夜祝福語
str_Christmas_Eve = 寶貝,平安夜快樂,你吃蘋果了嗎?n(*≧▽≦*)n

# 圣誕節(jié)祝福語
str_Christmas = 小仙女,圣誕節(jié)快樂哦?。à牛? ̄)づ╭?~

# 她生日的時(shí)候的祝福語
str_birthday = 親愛的,生日快樂,我已經(jīng)給你準(zhǔn)備好了禮物哦,明天你就能看到啦!(*@ο@*) 哇~
image.gif

演示圖片

image
image

完整代碼

?

# -*- 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

# 獲取每日勵(lì)志精句
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:
        # 對(duì)方的微信名稱
        my_friend = bot.friends().search(my_lady_wechat_name)[0]

        # 發(fā)送消息給對(duì)方
        my_friend.send(your_message)
    except:

        # 出問題時(shí),發(fā)送信息到文件傳輸助手
        bot.file_helper.send(u"守護(hù)女友出問題了,趕緊去看看咋回事~")

# 在規(guī)定時(shí)間內(nèi)進(jìn)行關(guān)心她操作
def start_care():

    # 待發(fā)送的內(nèi)容,先置為空
    message = ""

    # 來個(gè)死循環(huán),24小時(shí)關(guān)心她
    while(True):

        # 提示
        print("守護(hù)中,時(shí)間:%s"% time.ctime())

        # 每天定時(shí)問候,早上起床,中午吃飯,晚上吃飯,晚上睡覺
        # 獲取時(shí)間,只獲取時(shí)和分,對(duì)應(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翻譯: " + note
            else:
                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秒檢測(cè)一次
        time.sleep(60)

if __name__ == "__main__":

    # 若發(fā)現(xiàn)讀取取配置文件出錯(cuò),可以取消注釋下面這行,一般在pycharm環(huán)境下才需要增加
    # 設(shè)置當(dāng)前文件所在的目錄為當(dāng)前工作路徑
    # chdir(sys.path[0])

    # 啟動(dòng)微信機(jī)器人,自動(dòng)根據(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()):
        # Windows
        bot = Bot()
    elif('Darwin' in system()):
        # MacOSX
        bot = Bot()
    elif('Linux' in system()):
        # Linux
        bot = Bot(console_qr=2,cache_path=True)
    else:
        # 自行確定
        print("無法識(shí)別你的操作系統(tǒng)類型,請(qǐ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è)置早上起床時(shí)間,中午吃飯時(shí)間,下午吃飯時(shí)間,晚上睡覺時(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")
    # 幾號(hào),注意補(bǔ)全數(shù)字,為兩位數(shù),比如6號(hào)必須寫成08
    birthday_day = cf.get("configuration", "birthday_day")

    # 讀取早上起床時(shí)間,中午吃飯時(shí)間,下午吃飯時(shí)間,晚上睡覺時(shí)間的隨機(jī)提示語
    # 一般這里的代碼不要改動(dòng),需要增加提示語可以自己打開對(duì)應(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é)束是否加上表情符號(hào)
    # 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)

    # 她生日的時(shí)候的祝福語
    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)行分詞、情感分析,由于打包后文件體積太大,故暫時(shí)不采用這種方式
    # 僅僅是直接調(diào)用網(wǎng)絡(luò)接口

    # 做極其簡(jiǎn)單的情感分析
    # 結(jié)果僅供參考,請(qǐng)勿完全相信
    postData = {'data':msg.text}
    response = post('https://bosonnlp.com/analysis/sentiment?analysisType=',data=postData)
    data = response.text

    # 情感評(píng)分指數(shù)(越接近1表示心情越好,越接近0表示心情越差)
    now_mod_rank = (data.split(',')[0]).replace('[[','')
    print("來自女友的消息:%s\n當(dāng)前情感得分:%s\n越接近1表示心情越好,越接近0表示心情越差,情感結(jié)果僅供參考,請(qǐng)勿完全相信!\n\n" % (msg.text, now_mod_rank))

    # 發(fā)送信息到文件傳輸助手
    mood_message = u"來自女友的消息:" + msg.text + "\n當(dāng)前情感得分:" + now_mod_rank + "\n越接近1表示心情越好,越接近0表示心情越差,情感結(jié)果僅供參考,請(qǐng)勿完全相信!\n\n"
    bot.file_helper.send(mood_message)
image.gif

?

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容