This article is from article 3 of the reader’s submission, the public account “Brucepk”.
Before to share with you how to use itchat library to make wechat friends gender statistics, today to introduce a more powerful library: WXpy library.
On the basis of ItChat, WXpy improves the module’s ease of use through a lot of interface optimization, and carries out rich function expansion. Wxpy library is probably the most elegant wechat personal NUMBER API.
Today I only use one small feature of the WXPY library: chat with people.
The installation
The wxpy library supports Python versions 3.4-3.6 and 2.7. If you only have one version of Python installed on your computer, use the following command to install it:
pip install wxpy
Copy the code
If you have both versions 2.7 and 3.x installed on your computer, replace the “PIP” in the command above with pip3 or pip2 to ensure that you have installed the appropriate Python version.
Or you can download and install it from the official PYPI source (which may be slow or unstable in China) :
pip install -U wxpy
Copy the code
It can also be downloaded and installed from Douban PYPI mirror source (recommended for domestic users) :
pip install -U wxpy -i "https://pypi.doubanio.com/simple/"
Copy the code
I use Python version 3.6 and the code editing tool is Pycharm. Once installed, import the Wxpy library in the code editing tool Pycharm.
Landing WeChat
Directly call the Bot() method in the WXpy library will pop up the wechat QR code and scan it to log in to wechat.
# import module
from wxpy import *
import requests
import json
# Initialize the robot and scan the code to log in
bot = Bot()
Copy the code
After logging in, you can conduct relevant operations on wechat.
Create Turing robots
Next, you need to register your account on the Turing Robot website. Click below to read the original text to enter the website.
Create a robot and fill in the application terminal, application industry, and application scenario.
You get the APIkey once it’s created successfully.
Interface request API
The newly registered apiKey and the information entered by the user, info, are used as data in the POST request. Request. Post directly to the Turing robot API address, plus the data. This completes the request for the Turing robot interface.
robot = Bot()
def talk_robot(info='Hello.') :Define a default parameter
api_url = 'http://www.tuling123.com/openapi/api' # Turing interface URL
apikey = 'xxxxxxxxxx' # register Turing to generate key
data = {'key': apikey, 'info': info}
r = requests.post(api_url, data=data).text
Copy the code
The data returned from requests. Post is in JSON format. For example, if I typed the info parameter “Hello everyone”, the json statement would be:
{"code": 100000,"text":"You seem to be in a good mood!"}
Copy the code
Of course, this parameter corresponds to more than one reply, this is one of the random replies, may also reply:
{"code": 100000,"text":"Hello, I'm fine."}
Copy the code
Loads [‘text’] with json.loads(r)[‘text’]
response = json.loads(r)['text']
return response
Copy the code
Create chatty robots
Wxpy provides a way to register messages, making it easy to register various types of messages and customize how they are handled. Register messages using the simple @bot.register() method.
Define a function that responds to a friend’s message and format the message string as an input to the talk_robot() function.
Of course, just writing the above content will cause your program to exit when the main program ends. Wxpy gives you the embed() method, which adds the Embed () method to the end of your program (or wherever you want to pause debugging) to keep your program running while accessing the Python command line.
@robot.register()
def reply_my_friend(msg):
message = '{}'.format(msg.text)
response = talk_robot(info=message)
return response
embed()
Copy the code
In this way, my trumpet was transformed into a chatterbox wechat robot, who would chat with us whatever we said in the group.
Create micro-intelligent robots
If there are too many people in the technical group discussing the problem, the chatty person will appear to be not very intelligent. At this time, we can add a judgment condition to optimize TA, only when @TA, TA will talk.
@robot.register()
def print_group_msg(msg):
if msg.is_at:
message = '{}'.format(msg.text)
response = talk_robot(info=message)
return response
embed()
Copy the code
Take a look at this micro-intelligent robot.
In this way, the simple two wechat robots are completed, eager to try partners quickly try it. Please remember to log in with your own trumpet to make a robot, if you use your commonly used wechat to make a chatterbox robot, your wechat groups will explode, the unknown people still think your wechat virus.
Trumpet do safe robot also should pay attention to put the trumpet to cancel the attention focus of the public, because once the number of public tweets, you tuberculosis robot would drop, and the general public will set the key words to reply and the function of the messages are received reply, so they will always talk, but the free API interface request times of the day is limited, 5000 free requests per day. If the limit is exceeded, he or she will simply say, “Honey, I’ve run out of requests for the day.” Of course, tuhao please ignore this sentence I said above.
If the group is large and active, you can re-register Turing with another phone number to get an Apikey.
Source code is very simple, you can knock down, if you need to refer to my source code, public number reply “WXpy” to obtain the source code.
To optimize the
The robot created above can only recognize Chinese and some English, but not expressions, pictures, voice and video. This will be gradually optimized in the later stage.
This is just the tip of the wXPY library iceberg. There are many more interesting features:
-
Send text, pictures, videos and files;
-
Search friends, group chats, group members, etc., by keyword or user attribute;
-
Obtain the nickname, remarks, gender, region and other information of friends/group members;
-
Add friends, create groups, invite into groups, remove from groups and so on;
Recommended reading:
How to Teach Yourself Python
Welcome everyone to pay attention to my public number “crazy sea”, the background reply “1024”, for you to prepare the most popular technology.