Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Wechat to access the Turing Robot by Python

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how Python uses Wechat to access Turing Robot", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how Python uses Wechat to access the Turing Robot.

Introduction to 1.wxpy Library

Wxpy on the basis of itchat, through a large number of interface optimization to improve the ease of use of the module, and rich functional expansion.

Document address: https://wxpy.readthedocs.io

Download and install from the PYPI official source:

Pip install-U wxpy

Download and install from Douban PYPI mirror source, recommend domestic users to choose:

Pip install-U wxpy-I "https://pypi.doubanio.com/simple/"2. Turing robot

First sign up for an account: http://www.turingapi.com/, and then you can create your own Turing robot

To use the Turing robot, you can first take a look at the help center, where there are access tutorials, such as the interface address, request method and request parameters of the Turing robot, which will be used when connecting to the Turing robot.

3. Use Wechat to access Turing Robot

You need to use the wxpy library to access the Turing robot using Wechat. Import the library first, and then create an interface function.

Then configure the request parameters (in json format) in this function, such as: reqType: input type, 0 for text (default), 1 for picture, 2 for audio

Perception: input information, inputText (text) for text information, inputImage (url) for picture information, inputMedia (url) for audio information, selfInfo (location) for client attributes

UserInfo: user parameters, apiKey robot logo (you can see it when you enter the created robot), userId user ID (next to user avatar), groupId group chat ID, user nickname in userIdName group

API address: http://openapi.tuling123.com/openapi/api/v2, which is found in the access tutorial of the documentation help Center

The request address and configuration request parameters are passed through the requests.post () method. The configuration request parameters can be converted to json format through the json.dumps () method, and then a response response object is returned.

Then the returned response response object is loaded into memory and converted into a dictionary using the json.loads () method, and then used as the return value of the function

Then you can operate Wechat through python. You need to initialize the robot, such as bot = Bot (), and then reply to friends or group chat messages according to your needs.

4.wxpy.Bot ()

Syntax: wxpy.Bot (cache_path=None, console_qr=False, qr_path=None, qr_callback=None, login_callback=None, logout_callback=None)

Robot object, used to log in and operate Wechat accounts, covering most of Web Wechat functions

Cache_path: set the cache path of the current session, and enable the cache feature (not enabled by default). After enabling the cache, you can avoid repeated code scanning in a short period of time. If the cache expires, login will be required again. If set to True, the default cache path 'wxpy.pkl'' will be used.

Console_qr: to display the login QR code in the terminal, you need to install the pillow module

Qr_path: the path to save the QR code

Qr_callback: callback after obtaining the QR code, which can be used to define the processing method of the QR code. Receiving parameters: uuid, status, qrcode

Login_callback: callback after successful login. If not specified, the screen will be cleared and the QR code file will be deleted.

Logout_callback: callback when logging out

5.Bot.register ()

Syntax: Bot.register (chats=None, msg_types=None, except_self=True, run_async=True, enabled=True)

Can be used as a function decorator to register message configuration. Parameter chats indicates the chat object in which the message is located, multiple chat objects or chat types in single or list form, empty matching all chat objects, msg_types message types, single or list message types. If it is empty, it matches all message types (except SYSTEM messages), except_self means to exclude messages sent by itself, run_async indicates whether to execute the configured function asynchronously, which can improve the response speed, and enabled indicates the default on state of the current configuration, which can be turned on or off dynamically afterwards.

At this point, I believe you have a deeper understanding of "how Python uses Wechat to access the Turing robot". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report