16 lines of code to realize WeChat chat robot, automatic intelligent reply, let it chat with his wife after playing a group

Hits: 0

In our life and work, we are often unable to respond to messages in a timely manner, especially for people with a lot of business. It is not good if customers send us messages and we do not respond, but we do not have so much energy to reply all the time. At this time, intelligent Robots can help us solve many problems.

Like e-commerce customer service, like the administrator of a large QQ group/WeChat group, and when we play games, just hang the script, the robot will automatically reply to the message for you, and will not offend your girlfriend for a while, hahaha!

Today, I will teach you a trick, 16 lines of Python code to implement a WeChat chat intelligent robot (Turing), no longer have to worry about being scolded by customers for not responding to messages!

1. Effect

After I logged in with my own WeChat (on the left), I tested it with my assistant’s WeChat, and the chat effect after the robot took over my WeChat still looked good:

2. Project ideas

The overall idea is shown in the figure below:

3. Code Analysis

1. Install and import modules
The two used here, the itchat module and the requests module, are an essential step, and the project runs based on these two modules. The installation method is very simple. Direct the window key + R to call up the command window, and then enter the command to install. For example, to install the itchat module, directly enter pip install itchat.
As a reminder, the two modules are installed separately, and after one is installed, the other is installed.

After the installation is complete, we can import the module directly in the code:

import itchat
import requests

2. Apply for Turing Robot

The robot of this project is a ready-made Turing robot. We need to go to the Turing official website to register, and then enter the background to create a robot. Each robot has an api, which is what we will use.
Turing Robot used to have a free version and a trial version. Now, it requires real-name authentication. Although there are only 100 message replies per day, it is ok for testers to play.
Turing can also make personalized responses to keywords, which can be set in the “private corpus” in the background.

3. To implement the robot function
, in addition to the url to visit the website, it is also necessary to determine which robot is called, because there may be multiple robots in the background. At this time, the specific API of each robot is used, and the required The message sent is then used to send a request in post mode, sending the url of the website, the api of the robot and the message to be sent, and finally extracting the text in the dictionary, and finally returning the data.

def get_response(msg):
    apiUrl= "http://www.tuling123.com/openapi/api" #Website  to visit# 
    key: the key of the robot, info: the message to be sent 
    data={ 'key' :KEY , 'info' :msg , 'userid' : "WeChat-robot" }
     #Send request:
    r = requests.post(apiUrl,data=data)
    return r["text"]

4. Implement WeChat function
If someone sends us a message, we will call the data returned above, so how do we know if someone sends you a message? At this time, we use the decorator. @itchat.msg_register() provided by itchat can realize this function; then call the robot function written above, and finally return the information of the robot or the information of the sender.

Why do you need to add the sender’s information and copy it to send it again? This is to prevent the robot from not responding due to other problems such as the network. At this time, whoever sends us any message, we will automatically reply to him with the same message to avoid unresponsive chat.

@itchat.msg_register(itchat.content.TEXT) 
def  tuling_reply (user_data) : 
    print(user_data) #User information includes messages sent to you 
    user=user_data[ "Text" ] #Users send your information 
    return_user = get_response(user_data [ "Text" ])
    print(return_user)
    return return_user or user

5. Pop-up QR code and loop program
When the program is running, we need to pop up a QR code, let us scan the code and log in. At the same time, we must ensure that the program is always running, otherwise the program will be finished after only one run. These two functions It is provided in itchat, just use it directly.

itchat.auto_login() #scan code to log 
in itchat.run() #loop execution

The above is the code analysis of this WeChat intelligent chatbot. This dummy is still good. I don’t know the paid version. The charging will definitely be stronger. The whole code does not exceed 20 lines.

If you want the source code, you can leave a message “robot” in the comment area and chat with me privately. I will send it to you. Friends in the fan group can directly ask the administrator to ask for it.

Tip : WeChat registered in recent years may not be able to log in to the web version, so you may not be able to log in after scanning the code, but mine is always fine.

Fans who buy for free, move to the CSDN community

Leave a Reply

Your email address will not be published.