rubirai 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b91aff486d51f20061c25e988fabfb4adcf34b765f30bdd6f118858faafcb693
4
- data.tar.gz: 2508fa6974b69f487098972e89ba2fe04f32fe566f5e04a14aadb595653cabcd
3
+ metadata.gz: 5f41f579f9ae1486c4a8f5af2e12e7e98e2aab20b880b9d2b1781eb57f480c3b
4
+ data.tar.gz: 2044c0aabd41caba57890b30b1951e0440046201e1b4eaa1f91fc766ae404e1a
5
5
  SHA512:
6
- metadata.gz: 9676cbb324b9aed4abaca4d69f1f58791d0edb11434cb90ebc0bb9f1677d18c61d82d3b0709c9b0fca0eda176e2bd924dcaf5bb894f992ed77b27d06385f818a
7
- data.tar.gz: 92e5e0e168ec5b6834beb81d1398c64e5521b15adc869b95f12ea8e3e54e65f9f33f41e6e789377c710ad695357890da2b71a8ccc110327f956dd7195a177c87
6
+ metadata.gz: c34f19d099e6f2d85e403e93d9c03b82b5ad9325f26f7de0714e517b73d0ec041de372bf534bbffa34aef342394411012dd1cbf78da2a3afac87409006a547b7
7
+ data.tar.gz: 8392b2ea57dc0eb3df290a4e2b357bc184102879904fb38bbd5e9ce60bd7071b9be686fad559aff5b9ef4b8711e93bf205949fc9605991c661429425d3b31ebb
data/README.md CHANGED
@@ -23,6 +23,16 @@ mirai <-jvm-> mirai-console <-plugin-> mirai-api-http <-http-> rubirai
23
23
  [mirai-api-http]: https://github.com/project-mirai/mirai-api-http
24
24
  [mirai]: https://github.com/mamoe/mirai
25
25
 
26
+ ## Prerequisite
27
+
28
+ Install [mirai-api-http] and configure its `setting.yml` file.
29
+
30
+ Now its easier to enable `singleMode` if you have only one account
31
+ in the mirai console.
32
+
33
+ Note that you **must** enable Http Adapter with configuration of
34
+ `http` under `adapterSettings`. More mode support to come.
35
+
26
36
  ## Usage
27
37
 
28
38
  First, download the package using `gem`. In your `Gemfile`, add
@@ -38,7 +48,7 @@ require 'rubirai'
38
48
  # assuming your mirai http api address and port
39
49
  # are 127.0.0.1 and 8080
40
50
  bot = Rubirai::Bot.new('127.0.0.1', '8080')
41
- # qq and auth key
51
+ # qq and verify key
42
52
  bot.login 1145141919, 'ikisugi_key'
43
53
 
44
54
  # Add a listener function
@@ -65,7 +75,6 @@ gem install rubirai
65
75
  - [中文 Wiki][wiki]
66
76
  - [Docs][rubydocs]
67
77
 
68
-
69
78
  ## License
70
79
 
71
80
  [AGPL-3.0 License][license]
data/lib/rubirai/auth.rb CHANGED
@@ -2,11 +2,11 @@
2
2
 
3
3
  module Rubirai
4
4
  class Bot
5
- # Start authentication. Will store the session.
6
- # @param auth_key [String] the auth key defined in config file
5
+ # Start verification. Will store the session.
6
+ # @param verify_key [String] the auth key defined in config file
7
7
  # @return [String] the session key which will also be stored in the bot
8
- def auth(auth_key)
9
- v = call :post, '/auth', json: { "authKey": auth_key }
8
+ def verify(verify_key)
9
+ v = call :post, '/verify', json: { "verifyKey": verify_key }
10
10
  @session = v['session']
11
11
  end
12
12
 
@@ -14,11 +14,11 @@ module Rubirai
14
14
  # @param qq [String, Integer] qq id
15
15
  # @param session [String, nil] the session key. Set to `nil` will use the saved credentials.
16
16
  # @return [void]
17
- def verify(qq, session = nil)
17
+ def bind(qq, session = nil)
18
18
  check qq, session
19
19
 
20
- call :post, '/verify', json: { "sessionKey": @session || session, "qq": qq.to_i }
21
- @session = session if session
20
+ call :post, '/bind', json: { "sessionKey": @session || session, "qq": qq.to_i }
21
+ @session ||= session
22
22
  @qq = qq
23
23
  nil
24
24
  end
@@ -42,13 +42,16 @@ module Rubirai
42
42
  # Log you in.
43
43
  #
44
44
  # @param qq [String, Integer] qq id
45
- # @param auth_key [String] the auth key set in the settings file for mirai-api-http.
45
+ # @param verify_key [String] the auth key set in the settings file for mirai-api-http.
46
+ # @param single_mode [Boolean] if to skip binding (need to enable `singeMode` for
47
+ # mirai-http-api)
46
48
  # @return [void]
47
- # @see #auth
49
+ # @see #bind
48
50
  # @see #verify
49
- def login(qq, auth_key)
50
- auth auth_key
51
- verify qq
51
+ def login(qq, verify_key, single_mode: false)
52
+ verify verify_key
53
+ single_mode or bind(qq)
54
+ nil
52
55
  end
53
56
 
54
57
  alias connect login
@@ -15,7 +15,7 @@ module Rubirai
15
15
 
16
16
  # Friend message event
17
17
  class FriendMessageEvent < MessageEvent
18
- # @!method initialize(hash, bot = nil)
18
+ # @!method initialize(hash, boxt = nil)
19
19
  # @param hash [Hash]
20
20
  # @param bot [Rubirai::Bot]
21
21
  # @!attribute [r] sender
@@ -7,7 +7,7 @@ module Rubirai
7
7
  class Bot
8
8
  # Start to listen for events
9
9
  #
10
- # @param interval [Numeric] the interval to fetch events in seconds.
10
+ # @param interval [Integer, Float] the interval to fetch events in seconds.
11
11
  # @param is_blocking [Boolean] if the listen thread should block the current thread
12
12
  # @param ignore_error [Boolean] if errors should generate error events (see {RubiraiErrorEvent})
13
13
  # @return [void]
@@ -11,7 +11,7 @@ module Rubirai
11
11
  resp = call :get, '/friendList', params: {
12
12
  sessionKey: @session
13
13
  }
14
- resp.map { |friend| User.new(friend, self) }
14
+ resp['data'].map { |friend| User.new(friend, self) }
15
15
  end
16
16
 
17
17
  # Get group list of the bot
@@ -20,7 +20,7 @@ module Rubirai
20
20
  resp = call :get, '/groupList', params: {
21
21
  sessionKey: @session
22
22
  }
23
- resp.map { |group| Group.new(group, self) }
23
+ resp['data'].map { |group| Group.new(group, self) }
24
24
  end
25
25
 
26
26
  # Get member list of a group
@@ -31,7 +31,7 @@ module Rubirai
31
31
  sessionKey: @session,
32
32
  target: group_id
33
33
  }
34
- resp.map { |member| GroupUser.new(member, self) }
34
+ resp['data'].map { |member| GroupUser.new(member, self) }
35
35
  end
36
36
  end
37
37
  end
@@ -10,15 +10,17 @@ module Rubirai
10
10
  # @param target_qq [Integer] target qq id
11
11
  # @param group_id [Integer] group id
12
12
  # @param msgs [Array<Rubirai::Message, Hash, String, Object>] messages to form a chain, can be any type
13
+ # @param quote [NilClass, Integer] the message id to quote, nil for not quote
13
14
  # @return [Integer] message id
14
- def send_temp_msg(target_qq, group_id, *msgs)
15
+ def send_temp_msg(target_qq, group_id, *msgs, quote: nil)
15
16
  chain = Rubirai::MessageChain.make(*msgs, bot: self)
16
17
  resp = call :post, '/sendTempMessage', json: {
17
18
  sessionKey: @session,
18
19
  qq: target_qq.to_i,
19
20
  group: group_id.to_i,
21
+ quote: quote,
20
22
  messageChain: chain.to_a
21
- }
23
+ }.compact
22
24
  resp['messageId']
23
25
  end
24
26
 
@@ -27,15 +29,17 @@ module Rubirai
27
29
  # @param type [Symbol, String] options: [group, friend]
28
30
  # @param target_id [Integer] target qq/group id
29
31
  # @param msgs [Array<Rubirai::Message, Hash, String, Object>] messages to form a chain, can be any type
32
+ # @param quote [NilClass, Integer] the message id to quote, nil for not quote
30
33
  # @return [Integer] message id
31
- def send_msg(type, target_id, *msgs)
34
+ def send_msg(type, target_id, *msgs, quote: nil)
32
35
  self.class.ensure_type_in type, 'group', 'friend'
33
36
  chain = Rubirai::MessageChain.make(*msgs, bot: self)
34
37
  resp = call :post, "/send#{type.to_s.snake_to_camel}Message", json: {
35
38
  sessionKey: @session,
36
39
  target: target_id.to_i,
40
+ quote: quote,
37
41
  messageChain: chain.to_a
38
- }
42
+ }.compact
39
43
  resp['messageId']
40
44
  end
41
45
 
@@ -43,25 +47,28 @@ module Rubirai
43
47
  #
44
48
  # @param target_qq [Integer] target qq id
45
49
  # @param msgs [Array<Rubirai::Message, Hash, String, Object>] messages to form a chain, can be any type
50
+ # @param quote [NilClass, Integer] the message id to quote, nil for not quote
46
51
  # @return [Integer] message id
47
- def send_friend_msg(target_qq, *msgs)
48
- send_msg :friend, target_qq, *msgs
52
+ def send_friend_msg(target_qq, *msgs, quote: nil)
53
+ send_msg :friend, target_qq, *msgs, quote: quote
49
54
  end
50
55
 
51
56
  # Send group message
52
57
  #
53
58
  # @param target_group_id [Integer] group id
54
59
  # @param msgs [Array<Rubirai::Message, Hash, String, Object>] messages to form a chain, can be any type
60
+ # @param quote [NilClass, Integer] the message id to quote, nil for not quote
55
61
  # @return [Integer] message id
56
- def send_group_msg(target_group_id, *msgs)
57
- send_msg :group, target_group_id, *msgs
62
+ def send_group_msg(target_group_id, *msgs, quote: nil)
63
+ send_msg :group, target_group_id, *msgs, quote: quote
58
64
  end
59
65
 
60
66
  # Recall a message
61
67
  #
62
- # @param msg_id [Integer] message id
68
+ # @param msg_id [Integer, Rubirai::MessageChain] message id
63
69
  # @return [void]
64
70
  def recall(msg_id)
71
+ msg_id = msg_id.id if msg_id.is_a? Rubirai::MessageChain
65
72
  call :post, '/recall', json: {
66
73
  sessionKey: @session,
67
74
  target: msg_id
@@ -117,33 +124,19 @@ module Rubirai
117
124
  # @return [Integer] message id
118
125
  def respond(*msgs, quote: false)
119
126
  check_bot
120
- msgs.prepend(gen_quote) if quote
127
+ quote_id = quote ? (@message_chain.id || nil) : nil
121
128
  case self
122
129
  when FriendMessageEvent
123
- @bot.send_friend_msg(@sender.id, *msgs)
130
+ @bot.send_friend_msg(@sender.id, *msgs, quote: quote_id)
124
131
  when GroupMessageEvent
125
- @bot.send_group_msg(@sender.group.id, *msgs)
132
+ @bot.send_group_msg(@sender.group.id, *msgs, quote: quote_id)
126
133
  when TempMessageEvent
127
- @bot.send_temp_msg(@sender.id, @sender.group.id, *msgs)
134
+ @bot.send_temp_msg(@sender.id, @sender.group.id, *msgs, quote: quote_id)
128
135
  else
129
136
  raise 'undefined error'
130
137
  end
131
138
  end
132
139
 
133
- # Generates a quote message from this event
134
- #
135
- # @return [QuoteMessage] the quote message
136
- def gen_quote
137
- QuoteMessage.from(
138
- id: @message_chain.id,
139
- group_id: @sender.is_a?(GroupUser) ? @sender.group.id : 0,
140
- sender_id: @sender.id,
141
- target_id: @sender.is_a?(GroupUser) ? @sender.group.id : @bot.qq,
142
- origin: @message_chain.raw,
143
- bot: @bot
144
- )
145
- end
146
-
147
140
  private
148
141
 
149
142
  def check_bot
@@ -2,8 +2,8 @@
2
2
 
3
3
  module Rubirai
4
4
  # Rubirai version
5
- VERSION = '0.1.1'
5
+ VERSION = '0.2.0'
6
6
 
7
7
  # mirai-api-http version
8
- MIRAI_API_VERSION = '1.10.0'
8
+ MIRAI_API_VERSION = '2.5.0'
9
9
  end
data/lib/rubirai.rb CHANGED
@@ -58,7 +58,7 @@ module Rubirai
58
58
 
59
59
  body = JSON.parse(resp.body)
60
60
  if (body.is_a? Hash) && (body.include? 'code') && (body['code'] != 0)
61
- raise MiraiError.new(body['code'], body['msg'] || body['errorMessage'])
61
+ raise MiraiError.new(body['code'], body['msg'])
62
62
  end
63
63
 
64
64
  body
data/spec/auth_spec.rb CHANGED
@@ -12,38 +12,38 @@ describe 'auth api' do
12
12
  end
13
13
 
14
14
  it 'should be able to authenticate' do
15
- stub_request(:post, @mirai_bot.gen_uri('/auth'))
15
+ stub_request(:post, @mirai_bot.gen_uri('/verify'))
16
16
  .with(body: {
17
- "authKey": @auth_key
17
+ "verifyKey": @verify_key
18
18
  })
19
19
  .to_return(status: 200, body: %({
20
20
  "code": 0,
21
21
  "session": "#{@session_key}"
22
22
  }))
23
- res = @mirai_bot.auth @auth_key
23
+ res = @mirai_bot.verify @verify_key
24
24
  expect(res).to be_a_kind_of(String)
25
25
  expect(res).to eq(@session_key)
26
26
  end
27
27
 
28
28
  it 'should raise error if authenticate fails' do
29
29
  mirai_bot = new_bot
30
- stub_request(:post, @mirai_bot.gen_uri('/auth'))
30
+ stub_request(:post, @mirai_bot.gen_uri('/verify'))
31
31
  .with(body: {
32
- "authKey": @auth_key
32
+ "verifyKey": @verify_key
33
33
  })
34
34
  .to_return(status: 200, body: %({
35
35
  "code": 1,
36
36
  "session": ""
37
37
  }))
38
38
 
39
- expect { mirai_bot.auth @auth_key }.to raise_error(
39
+ expect { mirai_bot.verify @verify_key }.to raise_error(
40
40
  Rubirai::MiraiError,
41
41
  'Mirai error: 1 - Wrong auth key'
42
42
  )
43
43
  end
44
44
 
45
45
  it 'should be able to verify' do
46
- stub_request(:post, @mirai_bot.gen_uri('/verify'))
46
+ stub_request(:post, @mirai_bot.gen_uri('/bind'))
47
47
  .with(body: {
48
48
  "sessionKey": @session_key,
49
49
  "qq": @qq
@@ -53,11 +53,11 @@ describe 'auth api' do
53
53
  "session": "success"
54
54
  }))
55
55
 
56
- expect { @mirai_bot.verify @qq }.not_to raise_error
57
- expect(@mirai_bot.verify(@qq)).to be_nil
56
+ expect { @mirai_bot.bind @qq }.not_to raise_error
57
+ expect(@mirai_bot.bind(@qq)).to be_nil
58
58
 
59
- expect { @mirai_bot.verify '1ab39cde' }.to raise_error(Rubirai::RubiraiError, 'Wrong format for qq')
60
- expect { new_bot.verify @qq }.to raise_error(Rubirai::RubiraiError, 'No session provided')
59
+ expect { @mirai_bot.bind '1ab39cde' }.to raise_error(Rubirai::RubiraiError, 'Wrong format for qq')
60
+ expect { new_bot.bind @qq }.to raise_error(Rubirai::RubiraiError, 'No session provided')
61
61
  end
62
62
 
63
63
  it 'should be able to release' do
@@ -90,7 +90,7 @@ describe 'auth api' do
90
90
  "msg": "success"
91
91
  }))
92
92
  expect do
93
- expect(mirai_bot.login(@qq, @auth_key)).to be_nil
93
+ expect(mirai_bot.login(@qq, @verify_key)).to be_nil
94
94
  end.not_to raise_error
95
95
  expect(mirai_bot.instance_variable_get('@session')).to eq(@session_key)
96
96
 
data/spec/message_spec.rb CHANGED
@@ -6,7 +6,7 @@ describe 'message api' do
6
6
  before :all do
7
7
  @mirai_bot = new_bot
8
8
  stub_login
9
- @mirai_bot.login @qq, @auth_key
9
+ @mirai_bot.login @qq, @verify_key
10
10
  end
11
11
 
12
12
  after do
@@ -114,22 +114,8 @@ describe 'message api' do
114
114
  .with(body: {
115
115
  "sessionKey": 'test_session_key',
116
116
  "target": 1234567890,
117
+ "quote": 123456,
117
118
  "messageChain": [
118
- {
119
- "id": 123456,
120
- "groupId": 1234567890,
121
- "senderId": 123456789,
122
- "targetId": 1234567890,
123
- "origin": [
124
- {
125
- "type": 'Source',
126
- "id": 123456,
127
- "time": 123456789
128
- },
129
- { "text": 'Miral牛逼', "type": 'Plain' }
130
- ],
131
- "type": 'Quote'
132
- },
133
119
  { "text": 'hi', "type": 'Plain' }
134
120
  ]
135
121
  })
data/spec/spec_helper.rb CHANGED
@@ -24,22 +24,22 @@ WebMock.disable_net_connect!(allow_localhost: false)
24
24
 
25
25
  RSpec.configure do |config|
26
26
  config.before :all do
27
- @auth_key = 'test_auth_key'
27
+ @verify_key = 'test_verify_key'
28
28
  @session_key = 'test_session_key'
29
29
  @qq = 1145141919
30
30
  end
31
31
  end
32
32
 
33
33
  def stub_login
34
- stub_request(:post, @mirai_bot.gen_uri('/auth'))
34
+ stub_request(:post, @mirai_bot.gen_uri('/verify'))
35
35
  .with(body: {
36
- "authKey": @auth_key
36
+ "verifyKey": @verify_key
37
37
  })
38
38
  .to_return(status: 200, body: %({
39
39
  "code": 0,
40
40
  "session": "#{@session_key}"
41
41
  }))
42
- stub_request(:post, @mirai_bot.gen_uri('/verify'))
42
+ stub_request(:post, @mirai_bot.gen_uri('/bind'))
43
43
  .with(body: {
44
44
  "sessionKey": @session_key,
45
45
  "qq": @qq
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubirai
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rebuild
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-07-05 00:00:00.000000000 Z
11
+ date: 2022-02-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: concurrent-ruby