The guy wrote a QQ robot in Python and chatted with his girlfriend for an afternoon and was not found?

1 Technical selection

1.1 QQBot

The first one Baidu learned about is QQBot, a simple QQ bot implemented in python based on Tencent’s SmartQQ protocol. It can run on Linux and Windows platforms. All the code is integrated in a qqbot.py file, which you can use achieve:

  • Monitor and collect QQ messages
  • Automatic message push
  • Chatbot

The disadvantage is that it is inconvenient to manually scan each time you log in; the supporting functions are relatively small, which does not meet the needs of later needs.

1.2 Cool Q

Cool Q is a smart robot software that includes a powerful plug-in mechanism. You can install a variety of plug-ins and extend the functions of Cool Q. There are free versions and paid versions. The following describes the functions that the free version of the game can support.

  • Login support account password login
  • Private chat messages, group messages, discussion group messages, group management operations
  • Message: text message, At others, QQ expression, Emoji expression

Many people learn python and don't know where to start.
Many people learn python and after mastering the basic grammar, they don't know where to find cases to get started.
Many people who have done case studies do not know how to learn more advanced knowledge.
So for these three types of people, I will provide you with a good learning platform, free to receive video tutorials, e-books, and the source code of the course!
QQ group: 721195303

 

The basic free functions can already support my needs, so I chose CoolQ. CoolQ itself only encapsulates the SmartQQ protocol, it does not have functions, and can be extended by plug-ins.

1.3 Omnipotent Taobao

Search all kinds of software for mass message in the universal Taobao, there are a lot of them, and the price is very cheap. Why do we need to get one ourselves.

  • Security, in case other software is used, qq is stolen
  • It is not complicated to develop a function similar to Taobao
  • The self-developed one can be changed freely according to needs, and is more flexible. For example, I want to connect to a Turing robot today, and it is very convenient to connect to other robots tomorrow.

The guy wrote a QQ robot in Python and chatted with his girlfriend for an afternoon and was not found?

 

2 Start tossing

2.1 Install the required software

After downloading CoolQ directly, just double-click to install it.

2.1.1 CoolQ HTTP API plug-in

Report CoolQ events through HTTP and receive HTTP requests to call CoolQ's DLL interface, so that CoolQ plug-ins can be written in other languages. WebSocket is now supported.

Download the latest cpk file and put it in the app folder of CoolQ, and then enable it. After enabling, the plug-in will start an HTTP server to receive requests, the default listening is 0.0.0.0:5700, and a default configuration file will be generated for the first activation. In the app\io.github.richardchien.coolqhttpapi\config folder of CoolQ, the file name is As <user_id>.json

At this time, you can call the function of Cool Q through http://192.168.1.123:5700/, for example, http://192.168.1.123:5700/send_private_msg?user_id=123456&message=Hello, note that 192.168.1.123 should be replaced with If you run the IP of your own computer locally, you can use 127.0.0.1, and the user_id should also be replaced with the QQ number you want to send to.

2.1.2 NoneBot

NoneBot is a Python asynchronous QQ robot framework based on CoolQ. It parses and processes the messages received by the QQ robot, and distributes it to the command processor and natural language processor corresponding to the message in the form of plug-in. Complete specific functions.

In addition to the role of parsing messages, NoneBot also provides a large number of practical preset operation and permission control mechanisms for plug-ins, especially for command processors, it also provides a complete and easy-to-use conversation mechanism and internal calling mechanism to distinguish Adapt to the requirements of continuous interaction of commands and internal function reuse of plug-ins.

NoneBot uses the python-aiocqhttp library in its underlying interaction with CoolQ. The latter is a Python asynchronous SDK of the CoolQ HTTP API plug-in, which encapsulates the network interaction with the CoolQ HTTP API plug-in based on Quart.

Install by the following command

pip install none-bot

2.2 Start writing

2.2.1 Set plug-in configuration

There should be a file named <user-id>.json in the app\io.github.richardchien.coolqhttpapi\config\ directory of CoolQ (<user-id> is your QQ account). Modify this file, add the following configuration items, and restart cool Q

{
 "ws_reverse_api_url": "ws://127.0.0.1:8080/ws/api/",
 "ws_reverse_event_url": "ws://127.0.0.1:8080/ws/event/",
 "ws_reverse_reconnect_on_code_1000": true,
 "use_ws_reverse": true
}

2.2.2 Start NoneBot

After startup, you can see the information in the console as shown in the figure below, indicating that the startup has been successful.

import none
if __name__ == '__main__':
 none.init()
 none.load_builtin_plugins()
 none.run(host='127.0.0.1', port=8080)

 

After startup, the console information

The guy wrote a QQ robot in Python and chatted with his girlfriend for an afternoon and was not found?

 

2.2.3 Develop the function of mass messaging

The function of sending information to the current qq through another qq for group message sending only needs the following lines of code. Of course, it can also be changed to obtain user information and send user information in groups


#Monitor the sent message "group sending" event @on_command('send_msg', aliases=('group sending',)) 
async def send_msg(session: CommandSession): 
 message_type=session.ctx['message_type'] 
 user_id=session.ctx ['user_id'] #Judging that the 
 sent message is for private chat, and the sent QQ number is 12345678 
 if message_type=='private' and user_id==12345678: 
 #Get 
 qq group information group_list = await session.bot.get_group_list( ) 
 for group in group_list: #Send 
 information to a qq group 
 await session.bot.send_group_msg(group_id=group['group_id'],message='message to be sent in groups ')

3 Application extension, matters needing attention

What I talked about above is only the most basic function of sending messages, what else can be played?

  • The function of group management can automatically detect newly joined members, and can send group rules and guidelines to newly joined members. It can detect that the information sent by group members does not conform to the group rules, and automatically implement operations such as banning and kicking out
  • Realize group robots, such as @群 Robot, automatic reply, realize the interaction between members and robots; advanced points can be connected to external interfaces such as Turing robots.


I still want to recommend the Python learning group I built by myself : 721195303. All students in the group are learning Python. If you want to learn or are learning Python, you are welcome to join. Everyone is a software development party and shares dry goods from time to time (only Python software development related), including a copy of the latest Python advanced materials and zero-based teaching compiled by myself in 2021. Welcome friends who are in advanced and interested in Python to join!

Guess you like

Origin blog.csdn.net/aaahtml/article/details/114162836