SoFunction
Updated on 2024-11-16

16 lines of Python code to implement WeChat chatbot and automatic smart reply function

In our life and work, many times we can not respond to messages in a timely manner, especially more business people, customers send us messages we do not return and not good, but there is not so much energy to reply at times, this time the intelligent robot can help us solve a lot of problems.

Like e-commerce customer service, like the big QQ group / WeChat group administrators, as well as when we play the game, hanging scripts on the line, the robot automatically help you back to the message, a moment will not offend the girlfriend, hahaha!

Today to teach you a trick, 16 lines of Python code to achieve 1 WeChat chat intelligent robot (Turing), no longer need to worry about not returning messages scolded by customers!

I. Effects

After I logged in with my own WeChat (on the left), I tested it with my assistant's WeChat, and the chatting after the bot took over my WeChat still looked good:

II. Project ideas

The overall idea is shown below:

III. Code analysis

1. Installation and importation of modules

Here to use the two, itchat module and requests module, this is an essential step, the project is based on these two modules to run. Installation method is very simple, direct window key + R to bring up the command window, and then enter the command to install, for example, install itchat module directly enter pip install itchat.

As a hint, install the two modules separately, installing 1 before the other.

Once the installation is complete, we can import the module directly into our code:

import itchat
import requests

2. Apply for Turing Robotics

The robots for this project are off-the-shelf Turing robots, we need to go to Turing's official website to register, and then go to the backend to create the robots, each robot has 1 api, which is what we are going to use.

Turing Bot was previously available in free and experiential versions, but now it requires a bit of real-name authentication to be available, and although there are only 100 message replies per day, it's fine for beta testers to have some fun.

Turing also allows you to personalize your responses with keywords, which can be set up in the "Private Corpus" section of the backend.

3. Realization of robot functions

Here in addition to the url to access the site, but also to determine which robot is called, because there may be more than one of your background robots, this time the use of each robot-specific api, as well as to get the message to be sent, and then used to post the way to send a request to send the site's url, the robot's api and the message to be sent, and finally to extract the text inside the dictionary, and finally to return the data. Finally return data.

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

4. Realization of WeChat function

If someone sends us a message, we call the data returned above, so how do we know if someone sent you a message? This time we use a decorator, itchat provides @itchat.msg_register() can realize this function; then call the robot function already written above, and finally return the robot's information or the sender's information.

Why do you add the sender's message to copy it and send it again? This is to prevent the bot from being unresponsive due to other issues such as network, at this time whoever sends us any message, we automatically reply to them with the same message to avoid unresponsive chats.

@itchat.msg_register()
def tuling_reply(user_data):
    print(user_data)# The user's information contains messages sent to you
    user=user_data["Text"]#Users sending you messages
    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 1 QR code, let us scan the code and log in, at the same time to ensure that the program has been in the running state, otherwise the program will only run once finished, these two functions in itchat are provided, directly with it.

itchat.auto_login()#Sweep the code to log in
()#loop execution

Above is the code analysis of this WeChat intelligent chatbot, this dummy is still good, the paid version I do not know, charging money will certainly be stronger, the whole thing is not more than 20 lines of code.

To this article on the 16 lines of Python code to achieve the WeChat chat robot and automatic smart reply function of the article is introduced to this, more related python WeChat chat robot content, please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!