Skip to content
This repository was archived by the owner on Aug 2, 2020. It is now read-only.

Commit e94860a

Browse files
committed
Add get_cookies and get_csrf_token API
1 parent 7a59180 commit e94860a

File tree

3 files changed

+186
-135
lines changed

3 files changed

+186
-135
lines changed

docs/API.md

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ Authorization: token kSLuTF2GC2Q4q4ugm3
2121

2222
## 响应说明
2323

24-
对于任何请求,如果请求方式不支持,状态码为 405;如果 token 不符合,状态码为 404;剩下的所有情况,只要成功连接上服务器,无论 API 是否存在、实际操作失败还是成功,状态码都是 200。
24+
~~对于任何请求,如果请求方式不支持,状态码为 405;如果 token 不符合,状态码为 404;剩下的所有情况,只要成功连接上服务器,无论 API 是否存在、实际操作失败还是成功,状态码都是 200。~~
25+
26+
对于任何请求,如果请求方式不支持,状态码为 405;如果 token 不符合,状态码为 401;如何 API 不存在,状态码为 404;剩下的所有情况,无论操作失败还是成功,状态码都是 200。
2527

2628
响应内容为 JSON 格式,基本结构如下:
2729

@@ -44,10 +46,10 @@ Authorization: token kSLuTF2GC2Q4q4ugm3
4446
4547
`status` 字段如果是 `failed` 则表示操作失败,此时 `retcode` 有两种情况,当大于 0 时,表示是 HTTP API 插件判断出的失败(此时根本不用调用酷 Q 的函数就可以确定操作失败,比如调用 API 时没有传入必须的字段),这个目前有如下情况:
4648

47-
| 返回码 | 说明 |
48-
| ---- | ---------------------------------------- |
49-
| 100 | 默认的错误码,通常是因为没有传入必要参数,某些接口中也可能因为参数明显无效,比如传入的 QQ 号小于等于 0 |
50-
| 101 | 调用的接口路径不存在 |
49+
| 返回码 | 说明 |
50+
| ------- | ---------------------------------------- |
51+
| 100 | 默认的错误码,通常是因为没有传入必要参数,某些接口中也可能因为参数明显无效,比如传入的 QQ 号小于等于 0 |
52+
| ~~101~~ | ~~调用的接口路径不存在~~(这种情况现已直接通过 HTTP 状态码 404 来表示) |
5153

5254
另外一种情况是 `retcode` 小于 0,此时即为调用酷 Q 函数的返回码,具体含义直接参考 [Pro/开发/Error](https://d.cqp.me/Pro/%E5%BC%80%E5%8F%91/Error)
5355

@@ -340,3 +342,27 @@ Authorization: token kSLuTF2GC2Q4q4ugm3
340342
| `nickname` | string | 昵称 |
341343
| `sex` | string | 性别,`male``female``unknown` |
342344
| `age` | number | 年龄 |
345+
346+
### `/get_cookies` 获取 Cookies
347+
348+
#### 参数
349+
350+
351+
352+
#### 响应数据
353+
354+
| 字段名 | 数据类型 | 说明 |
355+
| --------- | ------ | ------- |
356+
| `cookies` | string | Cookies |
357+
358+
### `/get_csrf_token` 获取 CSRF Token
359+
360+
#### 参数
361+
362+
363+
364+
#### 响应数据
365+
366+
| 字段名 | 数据类型 | 说明 |
367+
| ------- | ------ | ---------- |
368+
| `token` | number | CSRF Token |

io.github.richardchien.coolqhttpapi.json

Lines changed: 126 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -2,129 +2,129 @@
22
// 打包前,应用的 .dll, .json 的文件名须以appid命名,应用AppInfo返回的内容须改为appid
33
// 如 appid=com.example.democ, 则dll及json文件需分别命名为 com.example.democ.dll、com.example.democ.json
44
{
5-
"ret": 1, // 返回码,固定为1
6-
"apiver": 9, // Api版本,本SDK为9
7-
"name": "HTTP API", // 应用名称
8-
"version": "1.0.3", // 应用版本
9-
"version_id": 7, // 应用顺序版本(每次发布时至少+1)
10-
"author": "Richard Chien", // 应用作者
11-
"description": "使酷 Q 支持通过 HTTP 上报事件信息及调用接口",
12-
"event": [ // 事件列表,同一事件类型可重复定义(发布前请删除无用事件)
13-
{
14-
"id": 1, // 事件ID
15-
"type": 21, // 事件类型
16-
"name": "私聊消息处理", // 事件名称
17-
"function": "_eventPrivateMsg", // 事件对应函数
18-
"priority": 30000 // 事件优先级(参见 cqp.im/deveventpriority)
19-
},
20-
{
21-
"id": 2,
22-
"type": 2,
23-
"name": "群消息处理",
24-
"function": "_eventGroupMsg",
25-
"priority": 30000
26-
},
27-
{
28-
"id": 3,
29-
"type": 4,
30-
"name": "讨论组消息处理",
31-
"function": "_eventDiscussMsg",
32-
"priority": 30000
33-
},
34-
{
35-
"id": 5,
36-
"type": 101,
37-
"name": "群管理变动事件处理",
38-
"function": "_eventSystem_GroupAdmin",
39-
"priority": 30000
40-
},
41-
{
42-
"id": 6,
43-
"type": 102,
44-
"name": "群成员减少事件处理",
45-
"function": "_eventSystem_GroupMemberDecrease",
46-
"priority": 30000
47-
},
48-
{
49-
"id": 7,
50-
"type": 103,
51-
"name": "群成员增加事件处理",
52-
"function": "_eventSystem_GroupMemberIncrease",
53-
"priority": 30000
54-
},
55-
{
56-
"id": 10,
57-
"type": 201,
58-
"name": "好友已添加事件处理",
59-
"function": "_eventFriend_Add",
60-
"priority": 30000
61-
},
62-
{
63-
"id": 8,
64-
"type": 301,
65-
"name": "好友添加请求处理",
66-
"function": "_eventRequest_AddFriend",
67-
"priority": 30000
68-
},
69-
{
70-
"id": 9,
71-
"type": 302,
72-
"name": "群添加请求处理",
73-
"function": "_eventRequest_AddGroup",
74-
"priority": 30000
75-
},
76-
{
77-
"id": 1001,
78-
"type": 1001,
79-
"name": "酷Q启动事件",
80-
"priority": 30000,
81-
"function": "_eventStartup"
82-
},
83-
{
84-
"id": 1002,
85-
"type": 1002,
86-
"name": "酷Q关闭事件",
87-
"priority": 30000,
88-
"function": "_eventExit"
89-
},
90-
{
91-
"id": 1003,
92-
"type": 1003,
93-
"name": "应用已被启用",
94-
"priority": 30000,
95-
"function": "_eventEnable"
96-
},
97-
{
98-
"id": 1004,
99-
"type": 1004,
100-
"name": "应用将被停用",
101-
"priority": 30000,
102-
"function": "_eventDisable"
103-
}
104-
],
105-
"menu": [],
106-
"status": [ // 悬浮窗状态(见 com.example.status 样例)
107-
],
108-
"auth": [ // 应用权限(发布前请删除无用权限)
109-
//20, //[敏感]取Cookies getCookies / getCsrfToken
110-
30, //接收语音 getRecord
111-
101, //发送群消息 sendGroupMsg
112-
103, //发送讨论组消息 sendDiscussMsg
113-
106, //发送私聊消息 sendPrivateMsg
114-
110, //发送赞 sendLike
115-
120, //置群员移除 setGroupKick
116-
121, //置群员禁言 setGroupBan
117-
122, //置群管理员 setGroupAdmin
118-
123, //置全群禁言 setGroupWholeBan
119-
124, //置匿名群员禁言 setGroupAnonymousBan
120-
125, //置群匿名设置 setGroupAnonymous
121-
126, //置群成员名片 setGroupCard
122-
127, //[敏感]置群退出 setGroupLeave
123-
128, //置群成员专属头衔 setGroupSpecialTitle
124-
130, //取群成员信息 getGroupMemberInfoV2 / getGroupMemberInfo
125-
131, //取陌生人信息 getStrangerInfo
126-
140, //置讨论组退出 setDiscussLeave
127-
150, //置好友添加请求 setFriendAddRequest
128-
151 //置群添加请求 setGroupAddRequest
129-
]
130-
}
5+
"ret": 1, // 返回码,固定为1
6+
"apiver": 9, // Api版本,本SDK为9
7+
"name": "HTTP API", // 应用名称
8+
"version": "1.0.4", // 应用版本
9+
"version_id": 8, // 应用顺序版本(每次发布时至少+1)
10+
"author": "Richard Chien", // 应用作者
11+
"description": "使酷 Q 支持通过 HTTP 上报事件信息及调用接口",
12+
"event": [// 事件列表,同一事件类型可重复定义(发布前请删除无用事件)
13+
{
14+
"id": 1, // 事件ID
15+
"type": 21, // 事件类型
16+
"name": "私聊消息处理", // 事件名称
17+
"function": "_eventPrivateMsg", // 事件对应函数
18+
"priority": 30000 // 事件优先级(参见 cqp.im/deveventpriority)
19+
},
20+
{
21+
"id": 2,
22+
"type": 2,
23+
"name": "群消息处理",
24+
"function": "_eventGroupMsg",
25+
"priority": 30000
26+
},
27+
{
28+
"id": 3,
29+
"type": 4,
30+
"name": "讨论组消息处理",
31+
"function": "_eventDiscussMsg",
32+
"priority": 30000
33+
},
34+
{
35+
"id": 5,
36+
"type": 101,
37+
"name": "群管理变动事件处理",
38+
"function": "_eventSystem_GroupAdmin",
39+
"priority": 30000
40+
},
41+
{
42+
"id": 6,
43+
"type": 102,
44+
"name": "群成员减少事件处理",
45+
"function": "_eventSystem_GroupMemberDecrease",
46+
"priority": 30000
47+
},
48+
{
49+
"id": 7,
50+
"type": 103,
51+
"name": "群成员增加事件处理",
52+
"function": "_eventSystem_GroupMemberIncrease",
53+
"priority": 30000
54+
},
55+
{
56+
"id": 10,
57+
"type": 201,
58+
"name": "好友已添加事件处理",
59+
"function": "_eventFriend_Add",
60+
"priority": 30000
61+
},
62+
{
63+
"id": 8,
64+
"type": 301,
65+
"name": "好友添加请求处理",
66+
"function": "_eventRequest_AddFriend",
67+
"priority": 30000
68+
},
69+
{
70+
"id": 9,
71+
"type": 302,
72+
"name": "群添加请求处理",
73+
"function": "_eventRequest_AddGroup",
74+
"priority": 30000
75+
},
76+
{
77+
"id": 1001,
78+
"type": 1001,
79+
"name": "酷Q启动事件",
80+
"priority": 30000,
81+
"function": "_eventStartup"
82+
},
83+
{
84+
"id": 1002,
85+
"type": 1002,
86+
"name": "酷Q关闭事件",
87+
"priority": 30000,
88+
"function": "_eventExit"
89+
},
90+
{
91+
"id": 1003,
92+
"type": 1003,
93+
"name": "应用已被启用",
94+
"priority": 30000,
95+
"function": "_eventEnable"
96+
},
97+
{
98+
"id": 1004,
99+
"type": 1004,
100+
"name": "应用将被停用",
101+
"priority": 30000,
102+
"function": "_eventDisable"
103+
}
104+
],
105+
"menu": [],
106+
"status": [// 悬浮窗状态(见 com.example.status 样例)
107+
],
108+
"auth": [// 应用权限(发布前请删除无用权限)
109+
20, //[敏感]取Cookies getCookies / getCsrfToken
110+
30, //接收语音 getRecord
111+
101, //发送群消息 sendGroupMsg
112+
103, //发送讨论组消息 sendDiscussMsg
113+
106, //发送私聊消息 sendPrivateMsg
114+
110, //发送赞 sendLike
115+
120, //置群员移除 setGroupKick
116+
121, //置群员禁言 setGroupBan
117+
122, //置群管理员 setGroupAdmin
118+
123, //置全群禁言 setGroupWholeBan
119+
124, //置匿名群员禁言 setGroupAnonymousBan
120+
125, //置群匿名设置 setGroupAnonymous
121+
126, //置群成员名片 setGroupCard
122+
127, //[敏感]置群退出 setGroupLeave
123+
128, //置群成员专属头衔 setGroupSpecialTitle
124+
130, //取群成员信息 getGroupMemberInfoV2 / getGroupMemberInfo
125+
131, //取陌生人信息 getStrangerInfo
126+
140, //置讨论组退出 setDiscussLeave
127+
150, //置好友添加请求 setFriendAddRequest
128+
151 //置群添加请求 setGroupAddRequest
129+
]
130+
}

src/request_handlers.cpp

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,6 @@ CQHTTP_REQUEST_HANDLER(send_group_msg)
6060
final_str = enhance_cq_code(msg);
6161
result.retcode = CQ_sendGroupMsg(ac, group_id, utf8_to_gbk(final_str.c_str()).c_str());
6262
}
63-
// else
64-
// result.status = CQHTTP_STATUS_FAILED;
6563
if (msg)
6664
free(msg);
6765
return result;
@@ -296,6 +294,10 @@ void get_string_from_decoded_bytes(const string& bytes, size_t start, size_t* si
296294
get_string_from_decoded_bytes(_bytes, _start, &_size, &field); \
297295
_start += _size;
298296

297+
/**
298+
* See https://cqp.cc/forum.php?mod=viewthread&tid=28730&page=2#pid1010363
299+
* for the structures of stranger and group member info.
300+
*/
299301
struct raw_group_member_info
300302
{
301303
int64_t group_id;
@@ -314,7 +316,7 @@ struct raw_group_member_info
314316
int32_t title_expire_time;
315317
int32_t card_changeable;
316318

317-
json_t* json()
319+
json_t* json() const
318320
{
319321
json_t* data = json_object();
320322
json_object_set_new(data, "group_id", json_integer(group_id));
@@ -336,6 +338,9 @@ struct raw_group_member_info
336338
}
337339
};
338340

341+
/**
342+
* See https://cqp.cc/t/26287 for how to parse the encoded data.
343+
*/
339344
CQHTTP_REQUEST_HANDLER(get_group_member_info)
340345
(const struct cqhttp_request& request)
341346
{
@@ -379,7 +384,7 @@ struct raw_stranger_info
379384
int32_t sex;
380385
int32_t age;
381386

382-
json_t* json()
387+
json_t* json() const
383388
{
384389
json_t* data = json_object();
385390
json_object_set_new(data, "user_id", json_integer(user_id));
@@ -417,3 +422,23 @@ CQHTTP_REQUEST_HANDLER(get_stranger_info)
417422
#undef INIT
418423
#undef INTEGER
419424
#undef STRING
425+
426+
CQHTTP_REQUEST_HANDLER(get_cookies)
427+
(const struct cqhttp_request& request)
428+
{
429+
struct cqhttp_result result;
430+
const char* cookies = CQ_getCookies(ac);
431+
result.retcode = cookies ? CQHTTP_RETCODE_OK : CQHTTP_RETCODE_ERROR_DEFAULT;
432+
result.data = json_pack("{s:s?}", "cookies", gbk_to_utf8(cookies).c_str());
433+
return result;
434+
}
435+
436+
CQHTTP_REQUEST_HANDLER(get_csrf_token)
437+
(const struct cqhttp_request& request)
438+
{
439+
struct cqhttp_result result;
440+
int32_t token = CQ_getCsrfToken(ac);
441+
result.retcode = token ? CQHTTP_RETCODE_OK : CQHTTP_RETCODE_ERROR_DEFAULT;
442+
result.data = json_pack("{s:I}", "token", (int64_t)token);
443+
return result;
444+
}

0 commit comments

Comments
 (0)