ruby-cqhttp 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/Bot/Api.rb +62 -0
- data/lib/Bot/Bot.rb +131 -0
- data/lib/Bot/Utils.rb +19 -0
- data/lib/ruby-cqhttp.rb +11 -0
- data/ruby-cqhttp.gemspec +21 -0
- metadata +47 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: c58389ac3f0bf71861c81ff860e50c4b3b36dad8c6d46f8542888ae556922288
|
4
|
+
data.tar.gz: af00c0a7322aefd90866849ecf28061b0c8fe9aaf9078bce7669f5a1face3a02
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 99cace3bf8ec77f910d22376c4ccf74784d4bb7538a06519fbd2db69acbaac0bf00daf917cd27a99ad96bc703712a1ee72150d047671eb4d95b5e24acb281f78
|
7
|
+
data.tar.gz: 2c11c6eed136f57900e796740900b1dd022c18433291b4ef08dcdd7e2512e653847a2a5f7d57dbbdba64399ef70736099d1b732be0522a55326e8b968bc02d4f
|
data/lib/Bot/Api.rb
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
module CQHttp
|
2
|
+
class Api
|
3
|
+
attr_accessor :apiUrl
|
4
|
+
|
5
|
+
@apiUrl = 'http://127.0.0.1:5700'
|
6
|
+
class << self
|
7
|
+
def sendPrivateMessage(msg, user_id, url=@apiUrl)
|
8
|
+
ret = { user_id: user_id, message: msg }.to_json
|
9
|
+
message_id = JSON.parse(Utils.httpPost("#{url}/send_private_msg", ret))['data']['message_id']
|
10
|
+
Utils.log Time.new, '↑', "发送至私聊 #{user_id} 的消息: #{msg} (#{message_id})"
|
11
|
+
message_id
|
12
|
+
end
|
13
|
+
|
14
|
+
def sendGroupMessage(msg, group_id, url=@apiUrl)
|
15
|
+
ret = { group_id: group_id, message: msg }.to_json
|
16
|
+
message_id = JSON.parse(Utils.httpPost("#{url}/send_group_msg", ret))['data']['message_id']
|
17
|
+
Utils.log Time.new, '↑', "发送至群 #{group_id} 的消息: #{msg} (#{message_id})"
|
18
|
+
message_id
|
19
|
+
end
|
20
|
+
|
21
|
+
def acceptFriendRequest(flag, url=@apiUrl)
|
22
|
+
ret = { flag: flag, approve: true }.to_json
|
23
|
+
data = JSON.parse(Utils.httpPost("#{url}/set_friend_add_request", ret))
|
24
|
+
if data['status'] == 'ok'
|
25
|
+
Utils.log Time.new, '√', '已通过好友请求'
|
26
|
+
else
|
27
|
+
Utils.log Time.new, '!', '请求通过失败'
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def refuseFriendRequest(flag, url=@apiUrl)
|
32
|
+
ret = { flag: flag, approve: false }.to_json
|
33
|
+
user_id = JSON.parse(Utils.httpPost("#{url}/set_friend_add_request", ret))
|
34
|
+
if data['status'] == 'ok'
|
35
|
+
Utils.log Time.new, '√', '已拒绝好友请求'
|
36
|
+
else
|
37
|
+
Utils.log Time.new, '!', '请求拒绝失败'
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def acceptGroupRequest(flag, sub_type, url=@apiUrl)
|
42
|
+
ret = { flag: flag, sub_type: sub_type, approve: true }.to_json
|
43
|
+
data = JSON.parse(Utils.httpPost("#{url}/set_group_add_request", ret))
|
44
|
+
if data['status'] == 'ok'
|
45
|
+
Utils.log Time.new, '√', '已通过加群请求'
|
46
|
+
else
|
47
|
+
Utils.log Time.new, '!', '请求通过失败'
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def refuseGroupRequest(flag, sub_type, url=@apiUrl)
|
52
|
+
ret = { flag: flag, sub_type: sub_type, approve: false }.to_json
|
53
|
+
data = JSON.parse(Utils.httpPost("#{url}/set_group_add_request", ret))
|
54
|
+
if data['status'] == 'ok'
|
55
|
+
Utils.log Time.new, '√', '已拒绝加群请求'
|
56
|
+
else
|
57
|
+
Utils.log Time.new, '!', '请求拒绝失败'
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
data/lib/Bot/Bot.rb
ADDED
@@ -0,0 +1,131 @@
|
|
1
|
+
module CQHttp
|
2
|
+
class Bot
|
3
|
+
Sender = Struct.new(:age, :member_role, :card, :qqlevel, :nickname, :title, :sex)
|
4
|
+
Target = Struct.new(:messagetype, :time, :group_id, :user_id, :message_id, :message)
|
5
|
+
|
6
|
+
def self.connect(url, debugmode=false)
|
7
|
+
client = ::CQHttp::Bot::WebSocket.new(url, debugmode)
|
8
|
+
yield client if block_given?
|
9
|
+
client.connect
|
10
|
+
client
|
11
|
+
end
|
12
|
+
|
13
|
+
class WebSocket
|
14
|
+
attr_accessor :url
|
15
|
+
attr_accessor :debugmode
|
16
|
+
attr_accessor :selfID
|
17
|
+
|
18
|
+
include EventEmitter
|
19
|
+
|
20
|
+
def initialize(url, debugmode)
|
21
|
+
@url = url
|
22
|
+
@debugmode = debugmode
|
23
|
+
end
|
24
|
+
|
25
|
+
def connect
|
26
|
+
EM.run do
|
27
|
+
@ws = Faye::WebSocket::Client.new(@url)
|
28
|
+
|
29
|
+
@ws.on :open do
|
30
|
+
end
|
31
|
+
|
32
|
+
@ws.on :message do |event|
|
33
|
+
Thread.new { dataParse(event.data)}
|
34
|
+
end
|
35
|
+
|
36
|
+
@ws.on :close do
|
37
|
+
emit :close
|
38
|
+
Utils.log Time.new, '!', '已断开连接'
|
39
|
+
@ws = nil
|
40
|
+
exit
|
41
|
+
end
|
42
|
+
|
43
|
+
@ws.on :error do |event|
|
44
|
+
emit :error, event
|
45
|
+
@ws = nil
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
|
51
|
+
def sendPrivateMessage(msg, user_id)
|
52
|
+
ret = { action: 'send_private_msg', params: { user_id: user_id, message: msg }, echo: 'BotPrivateMessage' }.to_json
|
53
|
+
Utils.log Time.new, '↑', "发送至私聊 #{user_id} 的消息: #{msg}"
|
54
|
+
@ws.send ret
|
55
|
+
end
|
56
|
+
|
57
|
+
def sendGroupMessage(msg, group_id)
|
58
|
+
ret = { action: 'send_group_msg', params: { group_id: group_id, message: msg }, echo: 'BotGroupMessage' }.to_json
|
59
|
+
Utils.log Time.new, '↑', "发送至群 #{group_id} 的消息: #{msg}"
|
60
|
+
@ws.send ret
|
61
|
+
end
|
62
|
+
|
63
|
+
private
|
64
|
+
|
65
|
+
def dataParse(data)
|
66
|
+
msg = JSON.parse(data)
|
67
|
+
sdr = Sender.new
|
68
|
+
tar = Target.new
|
69
|
+
tar.time = msg['time']
|
70
|
+
if msg['meta_event_type'] == 'lifecycle' && msg['sub_type'] == 'connect'
|
71
|
+
@selfID = msg['self_id']
|
72
|
+
Utils.log Time.at(tar.time), '!', "连接成功, BotQQ: #{@selfID}"
|
73
|
+
emit :logged, @selfID
|
74
|
+
end
|
75
|
+
if @debugmode == true # 判断是否为debug模式
|
76
|
+
puts msg if msg['meta_event_type'] != 'heartbeat' # 过滤心跳
|
77
|
+
end
|
78
|
+
case msg['post_type']
|
79
|
+
#
|
80
|
+
# 请求事件
|
81
|
+
#
|
82
|
+
when 'request'
|
83
|
+
case msg['request_type']
|
84
|
+
when 'group'
|
85
|
+
if msg['sub_type'] == 'invite' # 加群邀请
|
86
|
+
Utils.log Time.at(tar.time), '↓', "收到用户 #{msg['user_id']} 的加群 #{msg['group_id']} 请求 (#{msg['flag']})"
|
87
|
+
end
|
88
|
+
when 'friend' # 加好友邀请
|
89
|
+
Utils.log Time.at(tar.time), '↓', "收到用户 #{msg['user_id']} 的好友请求 (#{msg['flag']})"
|
90
|
+
end
|
91
|
+
emit :request, msg['request_type'], msg['sub_type'], msg['flag']
|
92
|
+
#
|
93
|
+
# 提醒事件
|
94
|
+
#
|
95
|
+
when 'notice'
|
96
|
+
case msg['notice_type']
|
97
|
+
when 'group_decrease' # 群数量减少
|
98
|
+
if msg['sub_type'] == 'kick_me' # 被踢出
|
99
|
+
Utils.log Time.at(tar.time), '!', "被 #{msg['operator_id']} 踢出群 #{msg['group_id']}"
|
100
|
+
end
|
101
|
+
end
|
102
|
+
#
|
103
|
+
# 消息事件
|
104
|
+
#
|
105
|
+
when 'message'
|
106
|
+
tar.user_id = msg['user_id'] # 用户id
|
107
|
+
tar.message_id = msg['message_id'] # 消息id
|
108
|
+
tar.message = msg['message'] # 消息内容
|
109
|
+
sdr.age = msg['sender']['age'] # 年龄
|
110
|
+
sdr.nickname = msg['sender']['nickname'] # 原有用户名
|
111
|
+
sdr.sex = msg['sender']['sex'] # 性别
|
112
|
+
tar.messagetype = msg['message_type'] # 消息类型
|
113
|
+
# 下面仅群聊
|
114
|
+
tar.group_id = msg['group_id'] # 群id
|
115
|
+
sdr.card = msg['sender']['card'] # 群昵称
|
116
|
+
sdr.title = msg['sender']['title'] # 头衔
|
117
|
+
sdr.member_role = msg['sender']['role'] # 群成员地位
|
118
|
+
sdr.qqlevel = msg['sender']['level'] # 群成员等级
|
119
|
+
if tar.messagetype == 'group' # 判断是否为群聊
|
120
|
+
Utils.log Time.at(tar.time), '↓', "收到群 #{tar.group_id} 内 #{sdr.nickname}(#{tar.user_id}) 的消息: #{tar.message} (#{tar.message_id})"
|
121
|
+
emit :groupMessage, tar.message, sdr, tar
|
122
|
+
else
|
123
|
+
Utils.log Time.at(tar.time), '↓', "收到好友 #{sdr.nickname}(#{tar.user_id}) 的消息: #{tar.message} (#{tar.message_id})"
|
124
|
+
emit :privateMessage, tar.message, sdr, tar
|
125
|
+
end
|
126
|
+
emit :message, tar.message, sdr, tar
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
data/lib/Bot/Utils.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
module CQHttp
|
2
|
+
class Utils
|
3
|
+
class << self
|
4
|
+
def log(time, status, str)
|
5
|
+
puts "[#{time.strftime('%Y-%m-%d %H:%M:%S')}][#{status}]: #{str}"
|
6
|
+
end
|
7
|
+
|
8
|
+
def httpPost(*args)
|
9
|
+
url = URI.parse args[0]
|
10
|
+
req = Net::HTTP::Post.new(url.path, { 'Content-Type' => 'application/json' })
|
11
|
+
req.body = args[1]
|
12
|
+
res = Net::HTTP.start(url.hostname, url.port) do |http|
|
13
|
+
http.request(req)
|
14
|
+
end
|
15
|
+
res.body
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/lib/ruby-cqhttp.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'faye/websocket'
|
2
|
+
require 'eventmachine'
|
3
|
+
require 'json'
|
4
|
+
require 'event_emitter'
|
5
|
+
require 'net/http'
|
6
|
+
|
7
|
+
module CQHttp
|
8
|
+
autoload :Api, File.expand_path('Bot/Api', __dir__)
|
9
|
+
autoload :Bot, File.expand_path('Bot/Bot', __dir__)
|
10
|
+
autoload :Utils, File.expand_path('Bot/Utils', __dir__)
|
11
|
+
end
|
data/ruby-cqhttp.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
lib = File.expand_path('lib', __dir__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = 'ruby-cqhttp'
|
6
|
+
s.version = '0.0.2'
|
7
|
+
s.date = '2020-11-04'
|
8
|
+
s.summary = 'ruby-cqhttp for osucat'
|
9
|
+
s.description = 'ruby-cqhttp for osucat'
|
10
|
+
s.authors = ['fantasyzhjk']
|
11
|
+
s.email = 'fantasyzhjk@outlook.com'
|
12
|
+
s.files = ['lib/ruby-cqhttp.rb']
|
13
|
+
s.homepage = 'http://rubygems.org/gems/ruby-cqhttp'
|
14
|
+
s.license = 'MIT'
|
15
|
+
s.files = `git ls-files -z`.split("\x0")
|
16
|
+
s.require_paths = ['lib']
|
17
|
+
|
18
|
+
# s.add_runtime_dependency 'json', '~> 1.1'
|
19
|
+
# s.add_runtime_dependency 'eventmachine', '~> 1.1'
|
20
|
+
# s.add_runtime_dependency 'faye-websocket', '~> 1.1'
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ruby-cqhttp
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- fantasyzhjk
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-11-04 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: ruby-cqhttp for osucat
|
14
|
+
email: fantasyzhjk@outlook.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/Bot/Api.rb
|
20
|
+
- lib/Bot/Bot.rb
|
21
|
+
- lib/Bot/Utils.rb
|
22
|
+
- lib/ruby-cqhttp.rb
|
23
|
+
- ruby-cqhttp.gemspec
|
24
|
+
homepage: http://rubygems.org/gems/ruby-cqhttp
|
25
|
+
licenses:
|
26
|
+
- MIT
|
27
|
+
metadata: {}
|
28
|
+
post_install_message:
|
29
|
+
rdoc_options: []
|
30
|
+
require_paths:
|
31
|
+
- lib
|
32
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
33
|
+
requirements:
|
34
|
+
- - ">="
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '0'
|
37
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
requirements: []
|
43
|
+
rubygems_version: 3.0.3
|
44
|
+
signing_key:
|
45
|
+
specification_version: 4
|
46
|
+
summary: ruby-cqhttp for osucat
|
47
|
+
test_files: []
|