Python teaches you to use WeChat to say good night to your girlfriend every day

As long as one thing, a little bit repetitive. I will consider how to use the program to achieve it.

Here, I will share with all programmers and friends how to send "good night", stories, news, etc. to friends on WeChat every day.
It is best to run it on the server, so that it is more convenient to hang in the background.

Prepare:

WeChat
pip install wxpy
pip install requests

code show as below:

#Do not copy the source code and run it, you need to change a few places

from __future__ import unicode_literals
from threading import Timer
from wxpy import *
import requests


# bot = Bot() 
# The QR code here is printed in the form of pixels! , if you are running on win environment, replace with bot=Bot() 
bot = Bot(console_qr=2, cache_path= " botoo.pkl " )


def get_news1(): #Get
     Kingsoft PowerWord daily sentence, English and translation 
    url = " http://open.iciba.com/dsapi/ " 
    r = requests.get(url)
    contents = r.json()['content']
    translation = r.json()['translation']
    return contents, translation

def send_news():
    try:
        my_friend = bot.friends().search(u ' Zhou Xiaodong ' )[0]   #Your friend's WeChat name, not a note or a WeChat account. 
        my_friend.send(get_news1()[0])
        my_friend.send(get_news1()[1][5:])
        my_friend.send(u " Chicken soup for the soul from Zhou Xiaodong! " )
         #Every 86400 seconds (1 day), send 1 time, do not use linux timed tasks because every time you log in, you need to scan the QR code to log in, which is very troublesome One thing, let him hang on forever 
        t = Timer(86400 ,send_news)
        t.start()
    except:
        my_friend = bot.friends().search( ' Fantastic @2 ' )[0] #Your WeChat name, not WeChat account. 
        my_friend.send(u " Failed to send message today " )

if __name__ == "__main__":
    send_news()

 

The final effect is this:

 
 
 
 
 
 

Summarize:

Code makes life better!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325106806&siteId=291194637