python實現智-給微信女友定時發消息

小智介紹:

# 導入模塊
from wxpy import *
# 初始化機器人,掃碼登陸
bot = Bot()

代碼:

#author_='zhi';
#date: 2020/6/20 0020 13:34
from __future__ import unicode_literals
from threading import Timer
from wxpy import *
import requests
import random

bot = Bot()

# linux執行登陸請調用下面的這句
# bot = Bot(console_qr=2,cache_path="botoo.pkl")
def get_news():
    """獲取金山詞霸每日一句,英文和翻譯"""
    url = "http://open.iciba.com/dsapi/"
    r = requests.get(url)
    content = r.json()['content']
    note = r.json()['note']
    return content, note

def send_news():
    try:
        contents = get_news()
        # 你朋友的微信名稱,不是備註,也不是微信帳號。
        my_friend = bot.friends().search('小智')[0]
        my_friend.send(contents[0])
        my_friend.send(contents[1])
        my_friend.send(u"晚安")
        # 每86400秒(1天),發送1次
        t = Timer(86400, send_news)
        # 爲了防止時間太固定,於是決定對其加上隨機數
        ran_int = random.randint(0, 100)
        t = Timer(86400 + ran_int, send_news)

        t.start()
    except:

        # 你的微信名稱,不是微信帳號。
        my_friend = bot.friends().search('小智哥')[0]
        my_friend.send(u"今天消息發送失敗了")

if __name__ == "__main__":
    send_news()

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章