In this tutorial, we will use Python and Mirai to develop a QQ robot . This article provides three teaching videos, including teaching and learning . This article also thoughtfully posts the code and related files . Without further ado, let's start teaching.
Table of contents
1. Install and configure MIrai
Image verification code error:
2. Robot authentication and binding
3. Monitoring friend information
Similarities and differences between http.client and requests library
1. Install and configure MIrai
Install and configure MIrai
Several download addresses in this video are as follows:
https://github.com/mamoe/mirai
https://github.com/project-mirai/mirai-api-http
https://github.com/MrXiaoM/qsign
If you can't open it, you can use my Baidu address, which contains three files.
Link: https://pan.baidu.com/s/1Wi4AcMIwl373Xe8sbbFnJg?pwd=1111
Extraction code: 1111
Image verification code error:
You can read the Mirai documentation. Some logins require verification. You can read this link , which details how to fill in the image verification code return value.
My solution:
- The console will return a slider address, which we open with a browser.
- Open the developer tools, move the slider, and complete the verification.
Network
Find the request named in ,cap_union_new_verify
the lump after the ticket is the content to be input back to mirai- Go back to the console and paste it.
Notice
- When copying, remember to delete the quotation marks (
"
, there is one before and after), that is not the ticket content - Be fast
- Be careful not to miss characters (not copied, there is still a long way to go)
- You can try it out once (without entering the ticket) and then restart the slider
2. Robot authentication and binding
At this point, everyone is 99% successful in logging in. Why do you say that? The next step is to write the code.
Video teaching
Robot authentication and binding
code
In order to make it easier for everyone to learn, I also put the code in the video below, as follows:
class bot:
def __init__(self,host="localhost",port = 8080,verifyKey="ccbot"):
"""
:param host: 监听地址
:param port: 监听端口
:param verifyKey: key
"""
self.VisitHttpPath=http.client.HTTPConnection(host,port)
self.verifyKey=verifyKey
self.sessionKey = self.bind()
Authentication and binding
Here is the http.client method:
def bind(self):
auto = json.dumps({"verifyKey":self.verifyKey})
VisitHttpPath = self.VisitHttpPath
VisitHttpPath.request("POST","/verify",auto)
response = VisitHttpPath.getresponse()
session = response.read().decode("utf-8")
print("认证成功:"+str(session))
sessionKey = json.loads(session)['session']
bind = json.dumps({"sessionKey":sessionKey,"qq":2368214676})
VisitHttpPath.request("POST",'/bind',bind)
response = VisitHttpPath.getresponse().read().decode("utf-8")
print("绑定成功:" + str(response))
return sessionKey
You can use http.client or request method (similarities and differences are introduced at the end of the article). The requests method is introduced below.
url = "http://localhost:8080/verify"
data = {"verifyKey":"ccbot"}
res = requests.post(url,json=data)
print(res.text)
sessionKey = json.loads(res.text)['session']
url = "http://localhost:8080/bind"
bind = {"sessionKey":sessionKey,"qq":填你机器人QQ号}
res = requests.post(url,json=bind)
print(res.text)
main program
if __name__ == '__main__':
b = bot()
3. Monitoring friend information
Once you complete the second step, you will be successful. The next step is to send and receive information. You can study it yourself by referring to the document. Mirai | mirai (mamoe.net)
Video teaching
Friends message monitoring
You can type code along with my video.
4. Send messages to friends
You can directly run the following code to see if your QQ has received the message.
call library
import requests
import json
program
url = "http://localhost:8080/verify"
data = {"verifyKey":"ccbot"}
res = requests.post(url,json=data)
print(res.text)
sessionKey = json.loads(res.text)['session']
url = "http://localhost:8080/bind"
bind = {"sessionKey":sessionKey,"qq":填你机器人QQ号}
res = requests.post(url,json=bind)
print(res.text)
url = "http://localhost:8080/sendFriendMessage"
send_message = {
"sessionKey":sessionKey,
"target":填你要接受消息的QQ号(须是好友),
"messageChain":[
{ "type":"Plain", "text":"你好" },
]
}
print(send_message)
res = requests.post(url,json=send_message)
print(res.text)
5. Summary
After seeing this, I believe you are all successful. If you like it, please give the article a like. If you have any questions, you can join the group discussion or leave a message in the comment area.
Take a look at the renderings:
6. Supplementary knowledge points
At this point, everyone's functions have basically been implemented. It has little to do with building a QQ robot and can be skipped.
Similarities and differences between http.client and requests library
Both the http.client and requests libraries can be used to send HTTP requests, but they have some differences.
Import method:
http.client
Modules arehttp.client
used by importing them.requests
Libraries arerequests
used by importing.How to send a request:
http.client
The module provides low-level HTTP communication functions, which can create different types of connections (such as HTTP, HTTPS, etc.) and userequest()
methods to send requests.requests
The library provides higher-level HTTP request functions, making it easier to send different types of requests (such as GET, POST, etc.) without paying attention to the underlying communication details.Response processing method:
http.client
Modules get responses throughgetresponse()
methods, and can then use the methods and properties of the response object to get the content of the response.requests
The library returns a Response object whose properties and methods can be used to obtain the contents of the response.Asynchronous support:
http.client
Modules provide asynchronous support, but are relatively complex to use.requests
The library itself does not support asynchronous requests, but it can be used in conjunction with an asynchronous library such as asyncio.Other functions:
http.client
Modules provide more low-level control, such as setting headers, handling redirects, etc.requests
The library provides simpler and easier-to-use APIs, such as handling Cookies, Sessions, etc.
In conclusion, choosing which method to use depends on your needs and preferences. If you need more control and flexibility, you can choose to use http.client
modules. If you care more about ease of use and simplicity, you can choose to use requests
a library.