CQHTTP 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: de2676b9e75e72384e908cd5e024390ed4c34b7a
4
+ data.tar.gz: 21b512289cf71ea8842d5eed659fc0f7e03c522a
5
+ SHA512:
6
+ metadata.gz: 6c9a2c6fae74f7cee911b84f50b85f0e78995d4c92ec578a9c2483937b223b56a06fa157dacea534a1797414e600222c6c1fbfe1a97fdd6c41801557388c921b
7
+ data.tar.gz: b501e2bf245e422d41dfada6e17427501790ee8023d6c4c746667c27cf59a44e99945f2e4fe1b817cf34c7ae0185e940d982a35ca3c84810a9217f5e6ef11a93
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/CQHTTP.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'CQHTTP/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'CQHTTP'
7
+ spec.version = CQHTTP::VERSION
8
+ spec.authors = ['71e6fd52']
9
+ spec.email = ['DAStudio.71e6fd52@gmail.com']
10
+
11
+ spec.summary = 'CoolQ HTTP API ruby bind'
12
+ spec.homepage = 'https://github.com/71e6fd52/cqhttp-ruby'
13
+
14
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
15
+ f.match(%r{^(test|spec|features)/})
16
+ end
17
+ spec.bindir = 'exe'
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.15'
22
+ spec.add_development_dependency 'rake', '~> 10.0'
23
+ spec.add_development_dependency 'rspec', '~> 3.0'
24
+ end
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in CQHTTP.gemspec
6
+ gemspec
data/README.en.md ADDED
@@ -0,0 +1,33 @@
1
+ # CQHTTP ([中文](/README.md))
2
+
3
+ This gem can make you use [richardchien/coolq-http-api](https://github.com/richardchien/coolq-http-api) in ruby.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's `Gemfile`:
8
+
9
+ ```ruby
10
+ gem 'CQHTTP'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install CQHTTP
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Development
26
+
27
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
28
+
29
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
30
+
31
+ ## Contributing
32
+
33
+ Bug reports and pull requests are welcome on GitHub at https://github.com/71e6fd52/cqhttp-ruby
data/README.md ADDED
@@ -0,0 +1,33 @@
1
+ # CQHTTP ([English](/README.en,md))
2
+
3
+ 在 ruby 中使用 [richardchien/coolq-http-api](https://github.com/richardchien/coolq-http-api)。
4
+
5
+ ## 安装
6
+
7
+ 在你的应用的 `Gemfile` 加入这行:
8
+
9
+ ```ruby
10
+ gem 'CQHTTP'
11
+ ```
12
+
13
+ 然后运行:
14
+
15
+ $ bundle
16
+
17
+ 或者你自己安装
18
+
19
+ $ gem install CQHTTP
20
+
21
+ ## 使用
22
+
23
+ TODO: 写使用说明
24
+
25
+ ## 开发
26
+
27
+ 检出这个仓库后,运行 `bin/setup` 来安装依赖关系。然后,运行 `rake spec` 来运行测试。 您还可以运行 `bin/console` 启动交互式提示符(`irb`),让您进行实验。
28
+
29
+ 要将此 gem 安装到本机上,请运行 `bundle exec rake install`。 要释出新版本,请在 `version.rb` 中更新版本号,然后运行 `bundle exec rake release`,该版本将为该版本创建一个 git 标签,并推送 git 提交和标签,然后将 `.gem` 文件提交到 [rubygems.org](https://rubygems.org)。
30
+
31
+ ## 贡献
32
+
33
+ 欢迎来 [GitHub](https://github.com/71e6fd52/cqhttp-ruby) 上发起 issues 和 pull requests
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "CQHTTP"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/gen_api_list ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'CQHTTP'
4
+ require 'json'
5
+
6
+ a = CQHTTP::API.new
7
+ puts a.func_list.to_json
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/lib/CQHTTP/API.md ADDED
@@ -0,0 +1,429 @@
1
+ # API 描述
2
+
3
+ ## 请求方式
4
+
5
+ 所有 API 都同时支持 GET 和 POST 两种请求方式,参数可通过 URL 参数、表单或 JSON 传入,后两者分别对应 Content-Type `application/x-www-form-urlencoded` 和 `application/json`。如果使用 JSON 传入,参数要放在根级别的 JSON 对象中,且参数的数据类型必须符合 API 列表中给出的每个参数所要求的数据类型,大致如下:
6
+
7
+ ```json
8
+ {"user_id": 123456, "message": "hello"}
9
+ ```
10
+
11
+ API 描述中没有给出默认值的参数均为必填项。
12
+
13
+ 如果配置文件中填写了 token,则每次请求需要在请求头中加入验证头,如:
14
+
15
+ ```http
16
+ GET /send_private_msg?user_id=123456&message=hello HTTP/1.1
17
+ Authorization: token kSLuTF2GC2Q4q4ugm3
18
+ ```
19
+
20
+ `kSLuTF2GC2Q4q4ugm3` 换成你填写的 token。
21
+
22
+ 从 2.1.3 版本开始支持以 query 参数 `access_token` 的形式传入 token,以便在无法修改请求头的情况下使用,例如:
23
+
24
+ ```http
25
+ GET /send_private_msg?access_token=kSLuTF2GC2Q4q4ugm3&user_id=123456&message=hello HTTP/1.1
26
+ ```
27
+
28
+ ## 响应说明
29
+
30
+ 对于任何请求:
31
+
32
+ - 如果请求方式不支持,状态码为 405;
33
+ - 如果 token 不符合,状态码为 401;
34
+ - 如果 POST 请求的 Content-Type 不正确,状态码为 400;
35
+ - 如果 API 不存在,状态码为 404;
36
+ - 剩下的所有情况,无论操作失败还是成功,状态码都是 200。
37
+
38
+ 响应内容为 JSON 格式,基本结构如下:
39
+
40
+ ```json
41
+ {
42
+ "status": "ok",
43
+ "retcode": 0,
44
+ "data": {
45
+ "id": 123456,
46
+ "nickname": "滑稽"
47
+ }
48
+ }
49
+ ```
50
+
51
+ `status` 字段如果是 `ok` 则表示操作成功,同时 `retcode` (返回码)会等于 0,即酷 Q 函数返回了 0,也就是酷 Q 认为成功了,但实际上有某些情况下其实是没有成功的,比如向没有加入的群发送消息,返回码是 0,日志里也没有提示发送失败,但显然是发送不了的。这一点在酷 Q 的 [官方文档](https://d.cqp.me/Pro/%E5%BC%80%E5%8F%91/Error) 也说明了:
52
+
53
+ > 需要指出的是,某些接口暂未进行错误代码的处理,此时即使发生错误,仍返回0。
54
+
55
+ `status` 字段如果是 `failed` 则表示操作失败,此时 `retcode` 有两种情况,当大于 0 时,表示是 HTTP API 插件判断出的失败,这个目前有如下情况:
56
+
57
+ | 返回码 | 说明 |
58
+ | ------- | ---------------------------------------- |
59
+ | 100 | 默认的错误码,通常是因为没有传入必要参数,某些接口中也可能因为参数明显无效,比如传入的 QQ 号小于等于 0,此时无需调用酷 Q 函数即可确定失败 |
60
+ | 102 | 酷 Q 函数返回的数据无效,一般是因为传入参数有效但没有权限,比如试图获取没有加入的群组的成员列表 |
61
+
62
+ 另外一种情况是 `retcode` 小于 0,此时即为调用酷 Q 函数的返回码,具体含义直接参考 [Pro/开发/Error](https://d.cqp.me/Pro/%E5%BC%80%E5%8F%91/Error) 和酷 Q 的日志。
63
+
64
+ `data` 字段为 API 返回数据的内容,对于发送消息、踢人等不需要返回数据的操作,这里为 null,对于获取群成员信息这类操作,这里为所获取的数据的对象,具体的数据内容将会在相应的 API 描述中给出。
65
+
66
+ 后面的 API 描述中将只给出 `data` 字段的内容,放在「响应数据」标题下。
67
+
68
+ ## 发送消息格式
69
+
70
+ 从 2.0.0 版本开始发送消息格式有了较大变化(向下兼容),具体请查看 [消息格式](https://richardchien.github.io/coolq-http-api/#/Message)。
71
+
72
+ 后面提到消息的地方(`message` 字段),将不再具体解释其格式。
73
+
74
+ ## API 列表
75
+
76
+ ### `/get_login_info` 获取登录号信息
77
+
78
+ #### 参数
79
+
80
+
81
+
82
+ #### 响应数据
83
+
84
+ | 字段名 | 数据类型 | 说明 |
85
+ | ----- | ------- | --- |
86
+ | `user_id` | number | QQ 号 |
87
+ | `nickname` | string | QQ 昵称 |
88
+
89
+ ### `/send_private_msg` 发送私聊消息
90
+
91
+ #### 参数
92
+
93
+ | 字段名 | 数据类型 | 默认值 | 说明 |
94
+ | ----- | ------- | ----- | --- |
95
+ | `user_id` | number | - | 对方 QQ 号 |
96
+ | `message` | string/array | - | 要发送的内容 |
97
+ | `is_raw` | bool | false | 消息内容是否作为纯文本发送(即不解析 CQ 码),`message` 数据类型为 `array` 时无效 |
98
+
99
+ #### 响应数据
100
+
101
+
102
+
103
+ ### `/send_group_msg` 发送群消息
104
+
105
+ #### 参数
106
+
107
+ | 字段名 | 数据类型 | 默认值 | 说明 |
108
+ | ----- | ------- | ----- | --- |
109
+ | `group_id` | number | - | 群号 |
110
+ | `message` | string/array | - | 要发送的内容 |
111
+ | `is_raw` | bool | false | 消息内容是否作为纯文本发送(即不解析 CQ 码),`message` 数据类型为 `array` 时无效 |
112
+
113
+ #### 响应数据
114
+
115
+
116
+
117
+ ### `/send_discuss_msg` 发送讨论组消息
118
+
119
+ #### 参数
120
+
121
+ | 字段名 | 数据类型 | 默认值 | 说明 |
122
+ | ----- | ------- | ----- | --- |
123
+ | `discuss_id` | number | - | 讨论组 ID(正常情况下看不到,需要从讨论组消息上报的数据中获得) |
124
+ | `message` | string/array | - | 要发送的内容 |
125
+ | `is_raw` | bool | false | 消息内容是否作为纯文本发送(即不解析 CQ 码),`message` 数据类型为 `array` 时无效 |
126
+
127
+ #### 响应数据
128
+
129
+
130
+
131
+ ### `/send_like` 发送好友赞
132
+
133
+ #### 参数
134
+
135
+ | 字段名 | 数据类型 | 默认值 | 说明 |
136
+ | ----- | ------- | ----- | --- |
137
+ | `user_id` | number | - | 对方 QQ 号 |
138
+ | `times` | number | 1 | 赞的次数,每个好友每天最多 10 次 |
139
+
140
+ #### 响应数据
141
+
142
+
143
+
144
+ ### `/set_group_kick` 群组踢人
145
+
146
+ #### 参数
147
+
148
+ | 字段名 | 数据类型 | 默认值 | 说明 |
149
+ | ----- | ------- | ----- | --- |
150
+ | `group_id` | number | - | 群号 |
151
+ | `user_id` | number | - | 要踢的 QQ 号 |
152
+ | `reject_add_request` | bool | `false` | 拒绝此人的加群请求 |
153
+
154
+ #### 响应数据
155
+
156
+
157
+
158
+ ### `/set_group_ban` 群组单人禁言
159
+
160
+ #### 参数
161
+
162
+ | 字段名 | 数据类型 | 默认值 | 说明 |
163
+ | ----- | ------- | ----- | --- |
164
+ | `group_id` | number | - | 群号 |
165
+ | `user_id` | number | - | 要禁言的 QQ 号 |
166
+ | `duration` | number | `30 * 60` | 禁言时长,单位秒,0 表示取消禁言 |
167
+
168
+ #### 响应数据
169
+
170
+
171
+
172
+ ### `/set_group_anonymous_ban` 群组匿名用户禁言
173
+
174
+ #### 参数
175
+
176
+ | 字段名 | 数据类型 | 默认值 | 说明 |
177
+ | ----- | ------- | ----- | --- |
178
+ | `group_id` | number | - | 群号 |
179
+ | `flag` | string | - | 要禁言的匿名用户的 flag(需从群消息上报的数据中获得) |
180
+ | `duration` | number | `30 * 60` | 禁言时长,单位秒,无法取消匿名用户禁言 |
181
+
182
+ #### 响应数据
183
+
184
+
185
+
186
+ ### `/set_group_whole_ban` 群组全员禁言
187
+
188
+ #### 参数
189
+
190
+ | 字段名 | 数据类型 | 默认值 | 说明 |
191
+ | ----- | ------- | ----- | --- |
192
+ | `group_id` | number | - | 群号 |
193
+ | `enable` | bool | `true` | 是否禁言 |
194
+
195
+ #### 响应数据
196
+
197
+
198
+
199
+ ### `/set_group_admin` 群组设置管理员
200
+
201
+ #### 参数
202
+
203
+ | 字段名 | 数据类型 | 默认值 | 说明 |
204
+ | ----- | ------- | ----- | --- |
205
+ | `group_id` | number | - | 群号 |
206
+ | `user_id` | number | - | 要设置管理员的 QQ 号 |
207
+ | `enable` | bool | `true` | true 为设置,false 为取消 |
208
+
209
+ #### 响应数据
210
+
211
+
212
+
213
+ ### `/set_group_anonymous` 群组匿名
214
+
215
+ #### 参数
216
+
217
+ | 字段名 | 数据类型 | 默认值 | 说明 |
218
+ | ----- | ------- | ----- | --- |
219
+ | `group_id` | number | - | 群号 |
220
+ | `enable` | bool | `true` | 是否允许匿名聊天 |
221
+
222
+ #### 响应数据
223
+
224
+
225
+
226
+ ### `/set_group_card` 设置群名片(群备注)
227
+
228
+ #### 参数
229
+
230
+ | 字段名 | 数据类型 | 默认值 | 说明 |
231
+ | ----- | ------- | ----- | --- |
232
+ | `group_id` | number | - | 群号 |
233
+ | `user_id` | number | - | 要设置的 QQ 号 |
234
+ | `card` | string | 空 | 群名片内容,不填或空字符串表示删除群名片 |
235
+
236
+ #### 响应数据
237
+
238
+
239
+
240
+ ### `/set_group_leave` 退出群组
241
+
242
+ #### 参数
243
+
244
+ | 字段名 | 数据类型 | 默认值 | 说明 |
245
+ | ----- | ------- | ----- | --- |
246
+ | `group_id` | number | - | 群号 |
247
+ | `is_dismiss` | bool | `false` | 是否解散,如果登录号是群主,则仅在此项为 true 时能够解散 |
248
+
249
+ #### 响应数据
250
+
251
+
252
+
253
+ ### `/set_group_special_title` 设置群组专属头衔
254
+
255
+ #### 参数
256
+
257
+ | 字段名 | 数据类型 | 默认值 | 说明 |
258
+ | ----- | ------- | ----- | --- |
259
+ | `group_id` | number | - | 群号 |
260
+ | `user_id` | number | - | 要设置的 QQ 号 |
261
+ | `special_title` | string | 空 | 专属头衔,不填或空字符串表示删除专属头衔 |
262
+ | `duration` | number | `-1` | 专属头衔有效期,单位秒,-1 表示永久,不过此项似乎没有效果,可能是只有某些特殊的时间长度有效,有待测试 |
263
+
264
+ #### 响应数据
265
+
266
+
267
+
268
+ ### `/set_discuss_leave` 退出讨论组
269
+
270
+ #### 参数
271
+
272
+ | 字段名 | 数据类型 | 默认值 | 说明 |
273
+ | ----- | ------- | ----- | --- |
274
+ | `discuss_id` | number | - | 讨论组 ID(正常情况下看不到,需要从讨论组消息上报的数据中获得) |
275
+
276
+ #### 响应数据
277
+
278
+
279
+
280
+ ### `/set_friend_add_request` 处理加好友请求
281
+
282
+ #### 参数
283
+
284
+ | 字段名 | 数据类型 | 默认值 | 说明 |
285
+ | ----- | ------- | ----- | --- |
286
+ | `flag` | string | - | 加好友请求的 flag(需从上报的数据中获得) |
287
+ | `approve` | bool | `true` | 是否同意请求 |
288
+ | `remark` | string | 空 | 添加后的好友备注(仅在同意时有效) |
289
+
290
+ #### 响应数据
291
+
292
+
293
+
294
+ ### `/set_group_add_request` 处理加群请求/邀请
295
+
296
+ #### 参数
297
+
298
+ | 字段名 | 数据类型 | 默认值 | 说明 |
299
+ | ----- | ------- | ----- | --- |
300
+ | `flag` | string | - | 加好友请求的 flag(需从上报的数据中获得) |
301
+ | `type` | string | - | `add` 或 `invite`,请求类型(需要和上报消息中的 `sub_type` 字段相符) |
302
+ | `approve` | bool | `true` | 是否同意请求/邀请 |
303
+ | `reason` | string | 空 | 拒绝理由(仅在拒绝时有效) |
304
+
305
+ #### 响应数据
306
+
307
+
308
+
309
+ ### `/get_group_list` 获取群列表
310
+
311
+ #### 参数
312
+
313
+
314
+
315
+ #### 响应数据
316
+
317
+ 响应内容为 JSON 数组,每个元素如下:
318
+
319
+ | 字段名 | 数据类型 | 说明 |
320
+ | ----- | ------- | --- |
321
+ | `group_id` | number | 群号 |
322
+ | `group_name` | string | 群名称 |
323
+
324
+ ### `/get_group_member_info` 获取群成员信息
325
+
326
+ #### 参数
327
+
328
+ | 字段名 | 数据类型 | 默认值 | 说明 |
329
+ | ----- | ------- | ----- | --- |
330
+ | `group_id` | number | - | 群号 |
331
+ | `user_id` | number | - | QQ 号(不可以是登录号) |
332
+ | `no_cache` | bool | `false` | 是否不使用缓存(使用缓存可能更新不及时,但响应更快) |
333
+
334
+ #### 响应数据
335
+
336
+ | 字段名 | 数据类型 | 说明 |
337
+ | ----- | ------- | --- |
338
+ | `group_id` | number | 群号 |
339
+ | `user_id` | number | QQ 号 |
340
+ | `nickname` | string | 昵称 |
341
+ | `card` | string | 群名片/备注 |
342
+ | `sex` | string | 性别,`male` 或 `female` 或 `unknown` |
343
+ | `age` | number | 年龄 |
344
+ | `area` | string | 地区 |
345
+ | `join_time` | number | 加群时间戳 |
346
+ | `last_sent_time` | number | 最后发言时间戳 |
347
+ | `level` | string | 成员等级 |
348
+ | `role` | string | 角色,`owner` 或 `admin` 或 `member` |
349
+ | `unfriendly` | bool | 是否不良记录成员 |
350
+ | `title` | string | 专属头衔 |
351
+ | `title_expire_time` | number | 专属头衔过期时间戳 |
352
+ | `card_changeable` | bool | 是否允许修改群名片 |
353
+
354
+ ### `/get_group_member_list` 获取群成员列表
355
+
356
+ #### 参数
357
+
358
+ | 字段名 | 数据类型 | 默认值 | 说明 |
359
+ | ----- | ------- | ----- | --- |
360
+ | `group_id` | number | - | 群号 |
361
+
362
+ #### 响应数据
363
+
364
+ 响应内容为 JSON 数组,每个元素的内容和上面的 `/get_group_member_info` 接口相同,但对于同一个群组的同一个成员,获取列表时和获取单独的成员信息时,某些字段可能有所不同,例如 `area`、`title` 等字段在获取列表时无法获得,具体应以单独的成员信息为准。
365
+
366
+ ### `/get_stranger_info` 获取陌生人信息
367
+
368
+ #### 参数
369
+
370
+ | 字段名 | 数据类型 | 默认值 | 说明 |
371
+ | ----- | ------- | ----- | --- |
372
+ | `user_id` | number | - | QQ 号(不可以是登录号) |
373
+ | `no_cache` | bool | `false` | 是否不使用缓存(使用缓存可能更新不及时,但响应更快) |
374
+
375
+ #### 响应数据
376
+
377
+ | 字段名 | 数据类型 | 说明 |
378
+ | ----- | ------- | --- |
379
+ | `user_id` | number | QQ 号 |
380
+ | `nickname` | string | 昵称 |
381
+ | `sex` | string | 性别,`male` 或 `female` 或 `unknown` |
382
+ | `age` | number | 年龄 |
383
+
384
+ ### `/get_cookies` 获取 Cookies
385
+
386
+ #### 参数
387
+
388
+
389
+
390
+ #### 响应数据
391
+
392
+ | 字段名 | 数据类型 | 说明 |
393
+ | ----- | ------- | --- |
394
+ | `cookies` | string | Cookies |
395
+
396
+ ### `/get_csrf_token` 获取 CSRF Token
397
+
398
+ #### 参数
399
+
400
+
401
+
402
+ #### 响应数据
403
+
404
+ | 字段名 | 数据类型 | 说明 |
405
+ | ----- | ------- | --- |
406
+ | `token` | number | CSRF Token |
407
+
408
+ ### `/get_version_info` 获取酷 Q 及 HTTP API 插件的版本信息
409
+
410
+ #### 参数
411
+
412
+
413
+
414
+ #### 响应数据
415
+
416
+ | 字段名 | 数据类型 | 说明 |
417
+ | ----- | ------- | --- |
418
+ | `coolq_edition` | string | 酷 Q 版本,`air` 或 `pro` |
419
+ | `plugin_version` | string | HTTP API 插件版本,例如 `2.1.3` |
420
+
421
+ ## 获取 `data` 目录中的文件的接口
422
+
423
+ 2.1.0 版本开始提供一个简单的静态文件获取服务,请求方式只支持 GET,URL 路径为 `/data/` 加上要请求的文件相对于酷 Q `data` 目录的路径。例如,假设酷 Q 主目录在 `C:\Apps\CQA`,则要获取 `C:\Apps\CQA\data\image\ABCD.jpg.cqimg` 的话,只需请求 `/data/image/ABCD.jpg.cqimg`,响应内容即为要请求的文件。
424
+
425
+ 和上面的其它请求一样,如果配置文件中指定了 token,则每次请求需要在请求头中加入验证头 `Authorization: token your-token`。
426
+
427
+ 另外,请求的路径中不允许出现 `..`,即上级目录的标记,以防止恶意或错误的请求到系统中的其它文件。
428
+
429
+ 本功能默认情况下不开启,在配置文件中将 `serve_data_file` 设置为 `yes` 或 `true` 即可开启,见 [配置文件说明](https://richardchien.github.io/coolq-http-api/#/Configuration)。
data/lib/CQHTTP/api.rb ADDED
@@ -0,0 +1,133 @@
1
+ module CQHTTP
2
+ # All API
3
+ class API
4
+ attr_reader :func_list
5
+
6
+ # init
7
+ #
8
+ # host: String: API address, like 'http://localhost:5700'
9
+ # type: Symbol or String, 'get', 'form' or 'json;
10
+ def initialize(host = 'http://localhost:5700', way = :get)
11
+ @func_list = {}
12
+ gen
13
+ @func_list.freeze
14
+ @network = CQHTTP::Network.gen way, host
15
+ end
16
+
17
+ def respond_to_missing?(method, include_private = false)
18
+ @func_list.include?(method) || super
19
+ end
20
+
21
+ def method_missing(name, *user_args)
22
+ return super unless respond_to_missing? name
23
+
24
+ args = gen_args name, user_args
25
+ args.freeze
26
+ return super if args.nil?
27
+ return super if args.value? nil
28
+ call_network name, args
29
+ end
30
+
31
+ private
32
+
33
+ ####################################################################
34
+ # api_call #
35
+ ####################################################################
36
+
37
+ def gen_args(name, user_args)
38
+ args = @func_list[name.to_sym]
39
+ return {} if args == {}
40
+ return hash_to_args(args, user) if user_args[0].class == {}.class
41
+ array_to_args(args, user_args)
42
+ end
43
+
44
+ def hash_to_args(default, user)
45
+ return unless user.size == 1
46
+ default.merge(user[0].delete_if { |key, _value| !default.key? key })
47
+ end
48
+
49
+ def array_to_args(default, user)
50
+ return if user.size > default.size
51
+ it = default.each_key
52
+ args = user.each_with_object({}) { |value, obj| obj[it.next] = value }
53
+ hash_to_args default, [args]
54
+ end
55
+
56
+ def call_network(name, args)
57
+ url = '/' + name.to_s
58
+ @network.call(url, args)
59
+ end
60
+
61
+ ####################################################################
62
+ # gen #
63
+ ####################################################################
64
+
65
+ def skip_head(file)
66
+ until file.gets.chomp == '## API 列表'; end
67
+ end
68
+
69
+ def skip_to_next_api(file)
70
+ while (line = file.gets)
71
+ return nil if line =~ /^## /
72
+ return line.gsub(%r{^### `\/(.+)`.*$}, '\1').chomp if line =~ /^### /
73
+ end
74
+ end
75
+
76
+ def skip_to_args(file)
77
+ until file.gets.chomp == '#### 参数'; end
78
+ file.gets
79
+ file.gets.chomp
80
+ end
81
+
82
+ def skip_to_table_head(file)
83
+ until file.gets =~ /^\|\s*-+\s*\|\s*-+\s*\|\s*-+\s*\|\s*-+\s*\|$/; end
84
+ end
85
+
86
+ def non_arg_check(name, line)
87
+ return unless line == '无'
88
+ @func_list[name.to_sym] = {}
89
+ true
90
+ end
91
+
92
+ def get_default(str)
93
+ str.sub!(/^`(.+)`$/, '\1')
94
+ return str.to_i if str =~ /^[+-]?\d+$/
95
+ return true if str == 'true'
96
+ return false if str == 'false'
97
+ m = str.match(/^\s*(\d+)\s*\*\s*(\d+)\s*/)
98
+ return m[1].to_i * m[2].to_i if m
99
+ str
100
+ end
101
+
102
+ def get_args(file)
103
+ args = {}
104
+ until (line = file.gets) =~ /^$/
105
+ m = line.match(/^\|\s*`(\w+)`\s*\|\s+.+?\s+\|\s+(.+?)\s+\|/)
106
+ args[m[1].to_sym] = case m[2]
107
+ when '空' then ''
108
+ when '-' then nil
109
+ else get_default m[2]
110
+ end
111
+ end
112
+ args
113
+ end
114
+
115
+ def add_to_func_list(name, file)
116
+ return if non_arg_check(name, skip_to_args(file))
117
+ skip_to_table_head file
118
+ @func_list[name.to_sym] = get_args file
119
+ end
120
+
121
+ def gen
122
+ File.open File.join(File.dirname(__FILE__), 'API.md') do |f|
123
+ skip_head f
124
+
125
+ loop do
126
+ name = skip_to_next_api f
127
+ return @func_list unless name
128
+ add_to_func_list name, f
129
+ end
130
+ end
131
+ end
132
+ end
133
+ end
@@ -0,0 +1,79 @@
1
+ module CQHTTP
2
+ # get, post
3
+ #
4
+ # Example:
5
+ # get = Network.gen :get, 'http://localhost:5700'
6
+ # json = get.call '/get_login_info'
7
+ module Network
8
+ # gen lambda
9
+ #
10
+ # type: Symbol or String, 'get', 'form' or 'json;
11
+ # host: String: API address, like 'http://localhost:5700'
12
+ def self.gen(type, host) # => lambda
13
+ case type.to_sym
14
+ when :get then ->(url, param = nil) { Network.get URI(host + url), param }
15
+ when :form then ->(url, body) { Network.post_form URI(host + url), body }
16
+ when :json then ->(url, body) { Network.post_json URI(host + url), body }
17
+ else raise type
18
+ end
19
+ end
20
+
21
+ # get url
22
+ #
23
+ # uri: URI
24
+ # params (optional): Hash, url query
25
+ def self.get(uri, params = nil) # => Hash
26
+ puts 'GET URL:', uri if $DEBUG
27
+ uri.query = URI.encode_www_form params if params
28
+ error Net::HTTP.get_response(uri)
29
+ end
30
+
31
+ # post to url by form
32
+ #
33
+ # uri: URI
34
+ # body: Hash, post body
35
+ def self.post_form(uri, body) # => Hash
36
+ puts 'POST URL:', uri if $DEBUG
37
+ error Net::HTTP.post_form(uri, body)
38
+ end
39
+
40
+ # post to url by json
41
+ #
42
+ # uri: URI
43
+ # body: Hash, post body
44
+ def self.post_json(uri, body)
45
+ puts 'POST URL:', uri if $DEBUG
46
+ error Net::HTTP.post(
47
+ uri,
48
+ body.to_json,
49
+ 'Content-Type' => 'application/json'
50
+ )
51
+ end
52
+
53
+ private_class_method
54
+
55
+ def self.error(res)
56
+ coolq_error http_error res
57
+ end
58
+
59
+ def self.http_error(res)
60
+ case res.code.to_i
61
+ when 200 then return JSON.parse res.body
62
+ when 405 then raise '请求方式不支持'
63
+ when 401 then raise 'token 不符合'
64
+ when 400 then raise 'POST 请求的 Content-Type 不正确'
65
+ when 404 then raise 'API 不存在'
66
+ end
67
+ raise res.code.to_s
68
+ end
69
+
70
+ def self.coolq_error(json)
71
+ case json['retcode'].to_i
72
+ when 0 then return json
73
+ when 100 then raise '参数错误'
74
+ when 102 then raise '没有权限'
75
+ end
76
+ raise json['retcode'].to_s
77
+ end
78
+ end
79
+ end
data/lib/CQHTTP/qq.rb ADDED
@@ -0,0 +1,45 @@
1
+ #
2
+ class QQ
3
+ include QQAPI
4
+ attr_reader :json
5
+
6
+ def initialize(hostname: '0.0.0.0', port: 9455)
7
+ @server = TCPServer.new(hostname, port)
8
+ @event_method = []
9
+ end
10
+
11
+ def bind(func)
12
+ @event_method << func
13
+ end
14
+
15
+ def run
16
+ loop do
17
+ socket = @server.accept
18
+
19
+ head socket
20
+
21
+ socket.print "HTTP/1.1 204\r\nContent-Type: application/json\r\n\r\n"
22
+ data = socket.gets
23
+ @json = JSON.parse data
24
+
25
+ @event_method.each { |func| func.call self }
26
+
27
+ socket.close
28
+ end
29
+ end
30
+
31
+ private
32
+
33
+ def head(socket)
34
+ puts 'head'
35
+ while (line = socket.gets) != "\r\n"
36
+ put line
37
+ end
38
+ puts 'end'
39
+ end
40
+
41
+ def put(text)
42
+ print ' '
43
+ puts text
44
+ end
45
+ end
@@ -0,0 +1,3 @@
1
+ module CQHTTP
2
+ VERSION = "0.1.1"
3
+ end
data/lib/CQHTTP.rb ADDED
@@ -0,0 +1,8 @@
1
+ require 'CQHTTP/version'
2
+
3
+ require 'CQHTTP/api.rb'
4
+ require 'CQHTTP/network.rb'
5
+
6
+ require 'uri'
7
+ require 'net/http'
8
+ require 'json'
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: CQHTTP
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - 71e6fd52
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-09-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description:
56
+ email:
57
+ - DAStudio.71e6fd52@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - CQHTTP.gemspec
65
+ - Gemfile
66
+ - README.en.md
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - bin/gen_api_list
71
+ - bin/setup
72
+ - lib/CQHTTP.rb
73
+ - lib/CQHTTP/API.md
74
+ - lib/CQHTTP/api.rb
75
+ - lib/CQHTTP/network.rb
76
+ - lib/CQHTTP/qq.rb
77
+ - lib/CQHTTP/version.rb
78
+ homepage: https://github.com/71e6fd52/cqhttp-ruby
79
+ licenses: []
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.6.13
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: CoolQ HTTP API ruby bind
101
+ test_files: []