ruby-cqhttp 0.0.7 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE +21 -0
- data/README.md +71 -61
- data/lib/Core/Core.rb +19 -0
- data/lib/Core/Http/API.rb +174 -0
- data/lib/Core/Logging/EventLogger.rb +101 -0
- data/lib/Core/Logging/Logger.rb +58 -0
- data/lib/Core/Utils.rb +85 -0
- data/lib/Core/WebSocket/API.rb +137 -0
- data/lib/Core/WebSocket/Bot.rb +69 -0
- data/lib/Core/WebSocket/Client.rb +44 -0
- data/lib/Core/WebSocket/Server.rb +35 -0
- data/lib/onebot-ruby.rb +44 -0
- metadata +92 -27
- data/.gitignore +0 -2
- data/Gemfile +0 -11
- data/lib/Bot/Api.rb +0 -188
- data/lib/Bot/Bot.rb +0 -255
- data/lib/Bot/Utils.rb +0 -119
- data/lib/ruby-cqhttp.rb +0 -17
- data/ruby-cqhttp.gemspec +0 -24
@@ -0,0 +1,137 @@
|
|
1
|
+
module Onebot
|
2
|
+
# WebSocket连接处理部分
|
3
|
+
module WebSocket
|
4
|
+
class API
|
5
|
+
attr_accessor :queueList
|
6
|
+
|
7
|
+
def initialize(ws, eventLogger)
|
8
|
+
@ws = ws
|
9
|
+
@eventLogger = eventLogger
|
10
|
+
@queueList = {}
|
11
|
+
end
|
12
|
+
|
13
|
+
def get_msg(message_id, _url = @apiUrl)
|
14
|
+
ret = sendReq('get_msg', { message_id: })
|
15
|
+
if parseRet(ret)
|
16
|
+
@eventLogger.log "获取消息成功 (#{message_id})"
|
17
|
+
else
|
18
|
+
@eventLogger.log "获取消息失败,错误码: #{ret.msg}, 错误消息: #{ret.wording}", Logger::WARN
|
19
|
+
end
|
20
|
+
ret[:data]
|
21
|
+
end
|
22
|
+
|
23
|
+
# 发送私聊消息
|
24
|
+
#
|
25
|
+
# @param msg [String]
|
26
|
+
# @param user_id [Number]
|
27
|
+
# @return [Hash]
|
28
|
+
def sendPrivateMessage(message, user_id)
|
29
|
+
ret = sendReq('send_private_msg', { user_id:, message: })
|
30
|
+
if parseRet(ret)
|
31
|
+
@eventLogger.log "发送至私聊 #{user_id} 的消息: #{message} (#{ret.data.message_id})"
|
32
|
+
else
|
33
|
+
@eventLogger.log "发送私聊消息失败,错误码: #{ret.msg}, 错误消息: #{ret.wording}", Logger::WARN
|
34
|
+
end
|
35
|
+
ret[:data]
|
36
|
+
end
|
37
|
+
|
38
|
+
# 发送群聊消息
|
39
|
+
#
|
40
|
+
# @param msg [String]
|
41
|
+
# @param group_id [Number]
|
42
|
+
# @return [Hash]
|
43
|
+
def sendGroupMessage(message, group_id)
|
44
|
+
ret = sendReq('send_group_msg', { group_id:, message: })
|
45
|
+
if parseRet(ret)
|
46
|
+
@eventLogger.log "发送至群 #{group_id} 的消息: #{message} (#{ret.data.message_id})"
|
47
|
+
else
|
48
|
+
@eventLogger.log "发送群消息失败,错误码: #{ret.msg}, 错误消息: #{ret.wording}", Logger::WARN
|
49
|
+
end
|
50
|
+
ret[:data]
|
51
|
+
end
|
52
|
+
|
53
|
+
# 接受好友邀请
|
54
|
+
#
|
55
|
+
# @param flag [String]
|
56
|
+
# @param reason [String]
|
57
|
+
# @return [Boolean]
|
58
|
+
def acceptFriendRequest(flag, reason = nil)
|
59
|
+
data = sendReq('set_friend_add_request', { flag:, approve: true, remark: reason })
|
60
|
+
if parseRet(data)
|
61
|
+
@eventLogger.log '已通过好友请求'
|
62
|
+
true
|
63
|
+
else
|
64
|
+
@eventLogger.log '请求通过失败', Logger::WARN
|
65
|
+
false
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
# 拒绝好友邀请
|
70
|
+
#
|
71
|
+
# @param flag [String]
|
72
|
+
# @return [Boolean]
|
73
|
+
def refuseFriendRequest(flag)
|
74
|
+
data = sendReq('set_friend_add_request', { flag:, approve: false })
|
75
|
+
if parseRet(data)
|
76
|
+
@eventLogger.log '已拒绝好友请求'
|
77
|
+
true
|
78
|
+
else
|
79
|
+
@eventLogger.log '请求拒绝失败', Logger::WARN
|
80
|
+
false
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
# 接受加群请求
|
85
|
+
#
|
86
|
+
# @param flag [String]
|
87
|
+
# @param sub_type [String]
|
88
|
+
# @return [Boolean]
|
89
|
+
def acceptGroupRequest(flag, sub_type)
|
90
|
+
data = sendReq('set_group_add_request', { flag:, sub_type:, approve: true })
|
91
|
+
if parseRet(data)
|
92
|
+
@eventLogger.log '已通过加群请求'
|
93
|
+
true
|
94
|
+
else
|
95
|
+
@eventLogger.log '请求通过失败', Logger::WARN
|
96
|
+
false
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
# 拒绝加群请求
|
101
|
+
#
|
102
|
+
# @param flag [String]
|
103
|
+
# @param sub_type [String]
|
104
|
+
# @param reason [String]
|
105
|
+
# @return [Boolean]
|
106
|
+
def refuseGroupRequest(flag, sub_type, reason = nil)
|
107
|
+
data = sendReq('set_group_add_request', { flag:, sub_type:, approve: false, reason: })
|
108
|
+
if parseRet(data)
|
109
|
+
@eventLogger.log '已拒绝加群请求'
|
110
|
+
true
|
111
|
+
else
|
112
|
+
@eventLogger.log '请求拒绝失败', Logger::WARN
|
113
|
+
false
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
private
|
118
|
+
|
119
|
+
#
|
120
|
+
# 解析API返回
|
121
|
+
#
|
122
|
+
def parseRet(ret)
|
123
|
+
return true if ret.status == 'ok'
|
124
|
+
return false if ret.status == 'failed'
|
125
|
+
end
|
126
|
+
|
127
|
+
def sendReq(action, params)
|
128
|
+
echo = Time.now.to_i.to_s
|
129
|
+
@ws.send({ action:, params:, echo: }.to_json)
|
130
|
+
@queueList[echo] = Queue.new
|
131
|
+
ret = @queueList[echo].pop
|
132
|
+
@queueList.delete(echo)
|
133
|
+
ret
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
module Onebot
|
2
|
+
# WebSocket连接处理部分
|
3
|
+
module WebSocket
|
4
|
+
class Bot
|
5
|
+
include EventEmitter
|
6
|
+
# @return [Number] self QQ id
|
7
|
+
attr_accessor :selfID
|
8
|
+
|
9
|
+
def initialize(**args); end
|
10
|
+
|
11
|
+
def method_missing(name, *args, &)
|
12
|
+
return @api.send(name, *args, &) if !@api.nil? && @api.respond_to?(name)
|
13
|
+
|
14
|
+
super
|
15
|
+
end
|
16
|
+
|
17
|
+
def respond_to_missing?(method_name, include_private = false)
|
18
|
+
(!@api.nil? && @api.respond_to?(name)) || super
|
19
|
+
end
|
20
|
+
|
21
|
+
# 发送消息
|
22
|
+
def sendMessage(msg, session)
|
23
|
+
return sendGroupMessage msg, session.group_id if session.message_type == 'group'
|
24
|
+
return sendPrivateMessage msg, session.user_id if session.message_type == 'private'
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
#
|
30
|
+
# 消息解析部分
|
31
|
+
#
|
32
|
+
def dataParse(data)
|
33
|
+
msg = JSON.parse(data, symbolize_names: true)
|
34
|
+
@eventLogger.dataParse(msg)
|
35
|
+
# 连接成功
|
36
|
+
if msg.meta_event_type == 'lifecycle' && msg.sub_type == 'connect'
|
37
|
+
@selfID = msg.self_id
|
38
|
+
emit :logged, @selfID
|
39
|
+
end
|
40
|
+
#
|
41
|
+
# 函数回调
|
42
|
+
#
|
43
|
+
@api.queueList[msg.echo] << msg if msg.include?(:echo) # 往API模块回调返回消息
|
44
|
+
case msg.post_type
|
45
|
+
#
|
46
|
+
# 请求事件
|
47
|
+
#
|
48
|
+
when 'request'
|
49
|
+
emit :request, msg.request_type, msg
|
50
|
+
#
|
51
|
+
# 提醒事件
|
52
|
+
#
|
53
|
+
when 'notice'
|
54
|
+
emit :notice, msg.notice_type, msg
|
55
|
+
#
|
56
|
+
# 消息事件
|
57
|
+
#
|
58
|
+
when 'message'
|
59
|
+
if msg.message_type == 'group' # 判断是否为群聊
|
60
|
+
emit :groupMessage, msg
|
61
|
+
else
|
62
|
+
emit :privateMessage, msg
|
63
|
+
end
|
64
|
+
emit :message, msg
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module Onebot
|
2
|
+
# WebSocket连接处理部分
|
3
|
+
module WebSocket
|
4
|
+
class Client < Bot
|
5
|
+
# @return [URI] WS URL
|
6
|
+
attr_accessor :url
|
7
|
+
# @return [Faye::WebSocket::Client] WS Conn
|
8
|
+
attr_accessor :ws
|
9
|
+
attr_accessor :api
|
10
|
+
|
11
|
+
# 设置 WS URL
|
12
|
+
def initialize(url: nil, logger: nil, **args)
|
13
|
+
super
|
14
|
+
@eventLogger = Logging::EventLogger.new(logger)
|
15
|
+
@url = url
|
16
|
+
end
|
17
|
+
|
18
|
+
# 连接 WS
|
19
|
+
def connect(protocols = nil, options = {})
|
20
|
+
@eventLogger.log '正在连接到 ' << @url
|
21
|
+
EM.run do
|
22
|
+
@ws = Faye::WebSocket::Client.new(@url, protocols, options)
|
23
|
+
@api = API.new(@ws, @eventLogger)
|
24
|
+
|
25
|
+
@ws.on :message do |event|
|
26
|
+
Thread.new { dataParse(event.data) }
|
27
|
+
end
|
28
|
+
|
29
|
+
@ws.on :close do |event|
|
30
|
+
emit :close, event
|
31
|
+
@eventLogger.log '连接断开'
|
32
|
+
@ws = nil
|
33
|
+
EM.stop
|
34
|
+
end
|
35
|
+
|
36
|
+
@ws.on :error do |event|
|
37
|
+
emit :error, event
|
38
|
+
@ws = nil
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module Onebot
|
2
|
+
# WebSocket连接处理部分
|
3
|
+
module WebSocket
|
4
|
+
class Server < Bot
|
5
|
+
# @return [Faye::WebSocket] ws server
|
6
|
+
attr_accessor :ws
|
7
|
+
attr_accessor :api
|
8
|
+
|
9
|
+
require 'rack'
|
10
|
+
|
11
|
+
def initialize(env:, logger: nil, options: { ping: 5 }, &block)
|
12
|
+
super
|
13
|
+
@eventLogger = Logging::EventLogger.new(logger)
|
14
|
+
@ws = Faye::WebSocket.new(env, %w[irc xmpp], options)
|
15
|
+
@api = API.new(@ws, @eventLogger)
|
16
|
+
@eventLogger.log(['客户端', '连接', @ws.url, @ws.version], ::Logger::INFO, 'Server')
|
17
|
+
|
18
|
+
@ws.on :message do |event|
|
19
|
+
Thread.new { dataParse(event.data) }
|
20
|
+
end
|
21
|
+
|
22
|
+
@ws.on :close do |event|
|
23
|
+
@eventLogger.log(['客户端', '断开', event.code].to_s, ::Logger::INFO, 'Server')
|
24
|
+
@ws = nil
|
25
|
+
end
|
26
|
+
|
27
|
+
yield self if block_given?
|
28
|
+
end
|
29
|
+
|
30
|
+
def rack_response
|
31
|
+
@ws.rack_response
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
data/lib/onebot-ruby.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'faye/websocket'
|
2
|
+
require 'json'
|
3
|
+
require 'event_emitter'
|
4
|
+
require 'net/http'
|
5
|
+
require 'logger'
|
6
|
+
|
7
|
+
class Hash
|
8
|
+
def method_missing(m, *a)
|
9
|
+
if m.to_s =~ /=$/
|
10
|
+
self[Regexp.last_match.pre_match] = a[0]
|
11
|
+
elsif a.empty?
|
12
|
+
self[m]
|
13
|
+
else
|
14
|
+
super
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def respond_to_missing?(m, include_private = false)
|
19
|
+
super unless m.to_s =~ /=$/
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
# 一个基于 OneBot 标准的 QQ 机器人框架
|
24
|
+
module Onebot
|
25
|
+
module Logging
|
26
|
+
autoload :Logger, File.expand_path('Core/Logging/Logger', __dir__)
|
27
|
+
autoload :EventLogger, File.expand_path('Core/Logging/EventLogger', __dir__)
|
28
|
+
end
|
29
|
+
|
30
|
+
module WebSocket
|
31
|
+
autoload :Session, File.expand_path('Core/WebSocket/Session', __dir__)
|
32
|
+
autoload :API, File.expand_path('Core/WebSocket/API', __dir__)
|
33
|
+
autoload :Bot, File.expand_path('Core/WebSocket/Bot', __dir__)
|
34
|
+
autoload :Client, File.expand_path('Core/WebSocket/Client', __dir__)
|
35
|
+
autoload :Server, File.expand_path('Core/WebSocket/Server', __dir__)
|
36
|
+
end
|
37
|
+
|
38
|
+
module Http
|
39
|
+
autoload :API, File.expand_path('Core/Http/API', __dir__)
|
40
|
+
end
|
41
|
+
|
42
|
+
autoload :Core, File.expand_path('Core/Core', __dir__)
|
43
|
+
autoload :Utils, File.expand_path('Core/Utils', __dir__)
|
44
|
+
end
|
metadata
CHANGED
@@ -1,17 +1,17 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-cqhttp
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- fantasyzhjk
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-05-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: event_emitter
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
@@ -25,7 +25,7 @@ dependencies:
|
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
28
|
+
name: faye-websocket
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - ">="
|
@@ -39,21 +39,21 @@ dependencies:
|
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
|
-
name:
|
42
|
+
name: json
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- - "
|
45
|
+
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: 0
|
47
|
+
version: '0'
|
48
48
|
type: :runtime
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- - "
|
52
|
+
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: 0
|
54
|
+
version: '0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
|
-
name:
|
56
|
+
name: logger
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
59
|
- - ">="
|
@@ -67,13 +67,13 @@ dependencies:
|
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
|
-
name:
|
70
|
+
name: solargraph
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
73
|
- - ">="
|
74
74
|
- !ruby/object:Gem::Version
|
75
75
|
version: '0'
|
76
|
-
type: :
|
76
|
+
type: :development
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
@@ -81,39 +81,104 @@ dependencies:
|
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0'
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
|
-
name:
|
84
|
+
name: rack
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
86
86
|
requirements:
|
87
87
|
- - ">="
|
88
88
|
- !ruby/object:Gem::Version
|
89
89
|
version: '0'
|
90
|
-
type: :
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: puma
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: 2.0.0
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: 2.0.0
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: thin
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: 1.2.0
|
118
|
+
type: :development
|
91
119
|
prerelease: false
|
92
120
|
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: 1.2.0
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: goliath
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
93
128
|
requirements:
|
94
129
|
- - ">="
|
95
130
|
- !ruby/object:Gem::Version
|
96
131
|
version: '0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: passenger
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: 4.0.0
|
146
|
+
type: :development
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - ">="
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: 4.0.0
|
97
153
|
description: 一个基于 OneBot 标准的 QQ 机器人框架
|
98
154
|
email: fantasyzhjk@outlook.com
|
99
155
|
executables: []
|
100
156
|
extensions: []
|
101
|
-
extra_rdoc_files:
|
157
|
+
extra_rdoc_files:
|
158
|
+
- README.md
|
102
159
|
files:
|
103
|
-
-
|
104
|
-
- Gemfile
|
160
|
+
- LICENSE
|
105
161
|
- README.md
|
106
|
-
- lib/
|
107
|
-
- lib/
|
108
|
-
- lib/
|
109
|
-
- lib/
|
110
|
-
-
|
162
|
+
- lib/Core/Core.rb
|
163
|
+
- lib/Core/Http/API.rb
|
164
|
+
- lib/Core/Logging/EventLogger.rb
|
165
|
+
- lib/Core/Logging/Logger.rb
|
166
|
+
- lib/Core/Utils.rb
|
167
|
+
- lib/Core/WebSocket/API.rb
|
168
|
+
- lib/Core/WebSocket/Bot.rb
|
169
|
+
- lib/Core/WebSocket/Client.rb
|
170
|
+
- lib/Core/WebSocket/Server.rb
|
171
|
+
- lib/onebot-ruby.rb
|
111
172
|
homepage: https://github.com/fantasyzhjk/ruby-cqhttp/
|
112
173
|
licenses:
|
113
174
|
- MIT
|
114
175
|
metadata: {}
|
115
|
-
post_install_message:
|
116
|
-
rdoc_options:
|
176
|
+
post_install_message:
|
177
|
+
rdoc_options:
|
178
|
+
- "--main"
|
179
|
+
- README.md
|
180
|
+
- "--markup"
|
181
|
+
- markdown
|
117
182
|
require_paths:
|
118
183
|
- lib
|
119
184
|
required_ruby_version: !ruby/object:Gem::Requirement
|
@@ -127,8 +192,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
127
192
|
- !ruby/object:Gem::Version
|
128
193
|
version: '0'
|
129
194
|
requirements: []
|
130
|
-
rubygems_version: 3.
|
131
|
-
signing_key:
|
195
|
+
rubygems_version: 3.3.7
|
196
|
+
signing_key:
|
132
197
|
specification_version: 4
|
133
198
|
summary: 一个基于 OneBot 标准的 QQ 机器人框架
|
134
199
|
test_files: []
|
data/.gitignore
DELETED
data/Gemfile
DELETED