Joke Collection Website - Cold jokes - How to make your WeChat automatically reply jokes to friends?

How to make your WeChat automatically reply jokes to friends?

hello! I'm glad to answer your questions. Recently, I wandered around the Internet and stumbled upon a python library: itchat. What can this library be used for? With this library, many interesting functions can be realized, such as sending messages to WeChat, detecting whether friends are blacked out by you, and making your WeChat reply automatically. The principle is probably to encapsulate the http request of WeChat web version. I think of the crawler that I wrote a few days ago to crawl the encyclopedia of anecdotes. I think the two can be combined to make my own WeChat into a simple robot that can automatically reply to paragraphs.

actually, the implementation is very simple, and it can be realized in more than 3 lines of code. The following are the implementation steps:

Step 1: Install itchat. There are many installation methods, and I choose pip installation:

$ pip install itchat

Step 2: load the paragraph content crawled by the crawler a few days ago into a list. My file for saving paragraphs is like this:

Remove the information such as author fun through string functions such as python's split, and only save the content in the list. The code implementation is very simple:

Step 3, make an interface that returns a paragraph randomly:

Step 4, register the function that handles WeChat text reply. All messages sent by friends will be processed by the following text_reply function:

Step 5, realize keyword reply. If your friend sends a message with a "joke" keyword, randomly reply a paragraph:

Step 6, realize the main function: initialize the paragraph list, realize WeChat login and detect WeChat events:

After the above steps are completed, run funRobots.py A QR code will pop up, scan the QR code with your WeChat, and then when a friend sends you a message with the keyword "joke", your WeChat will automatically reply to the other party with a joke. Is it interesting?

The following is the result of automatic reply from WeChat after I sent a message to myself after the above operation:

Isn't it interesting? The crawler I learned some time ago finally came in handy. Python is very powerful and interesting.

Finally, paste the complete source code:

The above is just a small example, and it is also possible to use other languages after mastering the principle. Throw a brick to attract jade here, and everyone can cooperate with reptiles to expand more functions at will.