前言

不懂就问,这篇文章的那张图片是go-cqhttp的logo吗

看了go-cqhttp的文档之后,深感我要框架做何用,直接python写个后端和cqhttp用http通讯多简单啊hhh干脆不用nonebot2了

go-cqhttp配置

首先是go-cqhttp的配置文件修改,需要配置反向http连接的路径和端口
config.yml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
- http: # HTTP 通信设置
address: 0.0.0.0:5700 # HTTP监听地址
timeout: 5 # 反向 HTTP 超时时间, 单位秒,<5 时将被忽略
long-polling: # 长轮询拓展
enabled: false # 是否开启
max-queue-size: 2000 # 消息队列大小,0 表示不限制队列大小,谨慎使用
middlewares:
<<: *default # 引用默认中间件
post: # 反向HTTP POST地址列表
#- url: '' # 地址
# secret: '' # 密钥
# max-retries: 3 # 最大重试,0 时禁用
# retries-interval: 1500 # 重试时间,单位毫秒,0 时立即
- url: http://127.0.0.1:8000/ # 地址
# secret: '' # 密钥
# max-retries: 0 # 最大重试,0 时禁用
# retries-interval: 1000 # 重试时间,单位毫秒,0 时立即

这样接收go-cqhttp的消息只要监听8000端口,同时发送请求只需向5700端口发送getpost数据。

qqbot的python代码实现

不使用nonebot2这样的框架,至少在不算复杂的应用都情况下感觉十分简单

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import requests
from flask import Flask, request
import pymysql
import time

app = Flask(__name__)

commands = ['记分', '清零', '查询', '复读', 'add', 'del', 'inquire', '12']


def add_score(target, score):
db = pymysql.connect(host='triority.cc',
user='triority',
password='xxx',
database='qqbot')
cursor = db.cursor()
sql = "select score from scores \
where qq_id = %s" % (target)
cursor.execute(sql)
results = cursor.fetchall()
if len(results) > 0:
try:
sql = "update scores set score = score + " + score + " where qq_id = %s" % (target)
cursor.execute(sql)
db.commit()
except:
db.rollback()
print("rollback")
else:
try:
sql = "insert into scores(qq_id, op, score)\
values (" + target + ", 0, " + score + ")"
cursor.execute(sql)
db.commit()
except:
db.rollback()
print("rollback")
sql = "select score from scores where qq_id = %s" % (target)
cursor.execute(sql)
results = cursor.fetchall()
now_score = results[0][0]
db.close()
return now_score


def inquire(target):
try:
db = pymysql.connect(host='triority.cc',
user='triority',
password='xxx',
database='qqbot')
cursor = db.cursor()
sql = "select score from scores where qq_id = %s" % (target)
cursor.execute(sql)
results = cursor.fetchall()
now_score = results[0][0]
db.close()
except:
now_score = '不存在'
return now_score


def delete(target):
try:
db = pymysql.connect(host='triority.cc',
user='triority',
password='xxx',
database='qqbot')
cursor = db.cursor()
sql = "select score from scores where qq_id = %s" % (target)
cursor.execute(sql)
results = cursor.fetchall()
now_score = results[0][0]
sql = "delete from scores where qq_id = %s" % (target)
try:
cursor.execute(sql)
db.commit()
except:
db.rollback()
print("rollback")
del_message = '分数已清零'
db.close()
except:
del_message = '不存在记录'
return del_message


def private(uid, message):
if message[0] == '复读':
requests.get('http://127.0.0.1:5700/send_private_msg?user_id=' + str(uid) +
'&message=' + str(message[1]) + '字符串长度' + str(len(message[1])))


def group(gid, uid, message):
if message[0] == '记分' or message[0] == 'add':
score = message[1]
if message[2][0] == '[':
target = message[2][10:-1]
else:
target = message[2]
new_score = add_score(target, score)
requests.get(
'http://127.0.0.1:5700/send_group_msg?group_id=' + str(gid) +
'&message=' + str(message[2]) + '已增加' + score + '分,当前分数为' + str(new_score))
elif message[0] == '复读':
requests.get('http://127.0.0.1:5700/send_group_msg?group_id=' + str(gid) +
'&message=' + str(message[1]) + '字符串长度' + str(len(message[1])))
elif message[0] == '查询' or message[0] == 'inquire':
if message[1][0] == '[':
target = message[1][10:-1]
else:
target = message[1]
score = inquire(target)
requests.get('http://127.0.0.1:5700/send_group_msg?group_id=' + str(gid) +
'&message=' + str(message[1]) + '当前分数为' + str(score))
elif message[0] == '清零' or message[0] == 'del':
if message[1][0] == '[':
target = message[1][10:-1]
else:
target = message[1]
del_message = delete(target)
requests.get('http://127.0.0.1:5700/send_group_msg?group_id=' + str(gid) +
'&message=' + str(message[1]) + del_message)


@app.route('/', methods=["POST"])
def post_data():
print(request.get_json()) # 将原始信息传到后台
if request.get_json().get('message_type') == 'private': # 如果是私聊信息
uid = request.get_json().get('sender').get('user_id') # 获取信息发送者的 QQ号码
message = request.get_json().get('raw_message') # 获取原始信息
if message[0] == '/':
message = message[1:].rstrip().split(' ')
if message[0] in commands:
private(uid, message)
elif request.get_json().get('message_type') == 'group': # 如果是群聊信息
gid = request.get_json().get('group_id') # 获取群号
uid = request.get_json().get('sender').get('user_id') # 获取信息发送者的 QQ号码
message = request.get_json().get('raw_message') # 获取原始信息
if message[0] == '/':
message = message[1:].rstrip().split(' ')
if message[0] in commands:
group(gid, uid, message)
return 'OK'


if __name__ == '__main__':
app.run(debug=True, host='127.0.0.1', port=8000)