In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces how to use python to create a Wechat group chat assistant, the content is very detailed, interested friends can refer to, I hope it can be helpful to you.
Teach you how to use 20 lines of Python code to achieve Wechat group chat assistant, which can be used to liven up the group atmosphere. Many group owners create a group and pull a group of people, then there is a silence. Having a group chat assistant can help activate the atmosphere in the group. Through the public test of a large number of friends on their Wechat today, the results are OK.
1. Installing python environment, python2.x or python3.x has no effect on this program.
2.mac comes with python, so you only need to install python's itchat library.
Pip install itchat
Under 3.windows, you need to install the python program in advance, and then install the itchat library. After installing python, open cmd and appear as shown below to indicate that python has been installed.
There is no test under 4.Linux, so I won't repeat it here.
5. Apply for Turing Robots API and key (the official website of Turing Robots can own Baidu)
You only need to register on the Turing Robot website to get a free key. Of course, there are some restrictions on free, but it is enough for us to test and play. The interface after login is as follows:
6. Write a python program with the following code
Import itchatimport requestsdef get_ response (msg): apiUrl 'http://www.tuling123.com/openapi/api' # is changed to the api of your own Turing robot, the content in the red box above, but it doesn't matter with mine, it's just that the number of messages automatically replied every day is limited data = {' key': 769e14179d3844948f84364d92fbd14b # Tuling Key'info': msg, # this is our message 'userid':' wechat-robot', # here you can change anything you want} We send a post request = requests with the following command. Post (apiurl, data= data). Json () return r. Get ('text) @ itchat. Msg_ register (itchat. Content.TEXT) def print content (msg): return get response (msg [' Text]) @ itchat. Msg_ register ([itchat. Content.TEXT], isGroupChat = True) def print_ content (msg): return get_ response (msg ['Text']) itchat. Auto login (True) itchat. Run ()
7. After the execution of the python script, the Wechat web version login QR code will pop up, scan the login web version of Wechat (or prompt the mobile Wechat to authorize login), and the Wechat robot can use it normally. At this time, messages sent to you by others will automatically reply, and group messages will automatically reply. If you want to quit the program, directly Ctrl+C exit the program, and you can end the automatic reply.
Python wechat.py # executes python script, which can be named freely, as shown in the figure under windows
After a successful login, the following figure is shown:
A brief introduction to the general principle:
Two libraries are used, one itchat and one requests. If there is no exception in your import itchat, it also means that you installed the itchat library successfully. Requests is a network request library, which comes with python and is used to call the Turing robot API.
@ itchat.msg_register (itchat.content.TEXT): used to receive conversation messages from friends # if you don't use this, the messages sent by friends will not be answered automatically
@ itchat.msg_register ([itchat.content.TEXT], isGroupChat=True): used to receive conversation messages in the group
The implementation principle is also very simple, receiving the message, calling the get_response () method, passing the message to the Turing robot, and then the received reply message is returned to Wechat, roughly this principle
Finally, the call to the login interface, the execution of itchat.auto_login (), the execution of the python code, will pop up a login QR code, similar to the code scan login on the web side, after you scan the code to log in, the robot will handle the messages sent by your friends and the group for you. Of course, if you do not want the machine to handle it, you can log out of the web side.
On how to use python to create a Wechat group chat assistant to share here, I hope the above can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.