rubirai 0.0.3.pre.a1 → 0.1.0
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 +4 -4
- data/.github/workflows/package.yml +0 -1
- data/README.md +4 -0
- data/lib/rubirai.rb +17 -6
- data/lib/rubirai/events/event.rb +22 -16
- data/lib/rubirai/message.rb +3 -3
- data/lib/rubirai/messages/message.rb +215 -21
- data/lib/rubirai/messages/message_chain.rb +7 -2
- data/lib/rubirai/objects/group_info.rb +5 -4
- data/lib/rubirai/version.rb +1 -1
- data/spec/events/event_spec.rb +1 -0
- data/spec/message_spec.rb +58 -0
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 174008ec842a298001749f9dc9650708cf789be436d9bdcde347270edc14b435
|
4
|
+
data.tar.gz: 31d528b994fe981b958e02a3847657d4c6b60848ce9778ee6126c1068d90a410
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a512e0d593a4d59d6988deeab66f671529bd2651e4fecc984c92f6b7da26317b7a87f1259a348fc38dad13bee02600457e5e832f2b947fded5f2439417c3d1d8
|
7
|
+
data.tar.gz: '0976747106fbe368257515a5f3164fc190b3f751631fdef68f059655ac624ee7b3d55898b543adaf404e1fa30ae7f4c20a7bfe6283ef00cc8bdf80fe2592c09c'
|
data/README.md
CHANGED
@@ -55,8 +55,12 @@ gem install rubirai
|
|
55
55
|
|
56
56
|
|
57
57
|
## License
|
58
|
+
|
59
|
+
[AGPL-3.0 License][license]
|
60
|
+
|
58
61
|
[](https://app.fossa.com/projects/git%2Bgithub.com%2FShimogawa%2Frubirai?ref=badge_large)
|
59
62
|
|
60
63
|
|
61
64
|
[wiki]: https://github.com/Shimogawa/rubirai/wiki
|
62
65
|
[rubydocs]: https://www.rebuild.moe/rubirai/
|
66
|
+
[license]: LICENSE
|
data/lib/rubirai.rb
CHANGED
@@ -1,3 +1,11 @@
|
|
1
|
+
# Copyright 2021 Rebuild.
|
2
|
+
#
|
3
|
+
# 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
|
4
|
+
# Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
|
5
|
+
#
|
6
|
+
# https://github.com/Shimogawa/rubirai/blob/master/LICENSE
|
7
|
+
#
|
8
|
+
|
1
9
|
# frozen_string_literal: true
|
2
10
|
|
3
11
|
require 'rubirai/errors'
|
@@ -8,13 +16,14 @@ module Rubirai
|
|
8
16
|
require 'http'
|
9
17
|
|
10
18
|
# Bot represents a QQ bot at mirai side. All functions are API calls to the http plugin.
|
19
|
+
#
|
20
|
+
# @!attribute [r] base_uri
|
21
|
+
# @return [String] the base uri of mirai-api-http which the bot will send messages to
|
22
|
+
# @!attribute [r] session
|
23
|
+
# @return [String] the session key
|
24
|
+
# @!attribute [r] qq
|
25
|
+
# @return [String, Integer] the qq of the bot
|
11
26
|
class Bot
|
12
|
-
# @!attribute [r] base_uri
|
13
|
-
# @return [String] the base uri of mirai-api-http which the bot will send messages to
|
14
|
-
# @!attribute [r] session
|
15
|
-
# @return [String] the session key
|
16
|
-
# @!attribute [r] qq
|
17
|
-
# @return [String, Integer] the qq of the bot
|
18
27
|
attr_reader :base_uri, :session, :qq
|
19
28
|
|
20
29
|
alias id qq
|
@@ -28,10 +37,12 @@ module Rubirai
|
|
28
37
|
@listener_funcs = []
|
29
38
|
end
|
30
39
|
|
40
|
+
# @private
|
31
41
|
def gen_uri(path)
|
32
42
|
URI.join(base_uri, path)
|
33
43
|
end
|
34
44
|
|
45
|
+
# @private
|
35
46
|
def self.ensure_type_in(type, *types)
|
36
47
|
types = types.map { |x| x.to_s.downcase }
|
37
48
|
type.to_s.downcase.must_be_one_of! types, RubiraiError, "not valid type: should be one of #{types}"
|
data/lib/rubirai/events/event.rb
CHANGED
@@ -58,22 +58,7 @@ module Rubirai
|
|
58
58
|
attr_keys.each do |k|
|
59
59
|
k2 = k.to_s.snake_to_camel(lower: true)
|
60
60
|
val = hash[k2]
|
61
|
-
val =
|
62
|
-
when 'group'
|
63
|
-
Group.new val, bot
|
64
|
-
when 'operator', 'member'
|
65
|
-
GroupUser.new val, bot
|
66
|
-
when 'sender'
|
67
|
-
if val.key? 'group'
|
68
|
-
GroupUser.new val, bot
|
69
|
-
else
|
70
|
-
User.new val, bot
|
71
|
-
end
|
72
|
-
when 'messageChain'
|
73
|
-
MessageChain.new bot, val
|
74
|
-
else
|
75
|
-
val
|
76
|
-
end
|
61
|
+
val = parse_val_from_key k2, val, bot
|
77
62
|
instance_variable_set("@#{k}", val)
|
78
63
|
end
|
79
64
|
end
|
@@ -107,6 +92,27 @@ module Rubirai
|
|
107
92
|
@raw = hash
|
108
93
|
@bot = bot
|
109
94
|
end
|
95
|
+
|
96
|
+
protected
|
97
|
+
|
98
|
+
def parse_val_from_key(key, val, bot)
|
99
|
+
case key
|
100
|
+
when 'group'
|
101
|
+
Group.new val, bot
|
102
|
+
when 'operator', 'member'
|
103
|
+
GroupUser.new val, bot
|
104
|
+
when 'sender'
|
105
|
+
if val.key? 'group'
|
106
|
+
GroupUser.new val, bot
|
107
|
+
else
|
108
|
+
User.new val, bot
|
109
|
+
end
|
110
|
+
when 'messageChain'
|
111
|
+
MessageChain.new bot, val
|
112
|
+
else
|
113
|
+
val
|
114
|
+
end
|
115
|
+
end
|
110
116
|
end
|
111
117
|
end
|
112
118
|
|
data/lib/rubirai/message.rb
CHANGED
@@ -114,7 +114,7 @@ module Rubirai
|
|
114
114
|
#
|
115
115
|
# @param msgs [Array<Rubirai::Message, Hash, String, Object>] messages to form a chain
|
116
116
|
# @param quote [Boolean] if to quote the original message
|
117
|
-
# @return [
|
117
|
+
# @return [Integer] message id
|
118
118
|
def respond(*msgs, quote: false)
|
119
119
|
check_bot
|
120
120
|
msgs.prepend(gen_quote) if quote
|
@@ -128,7 +128,6 @@ module Rubirai
|
|
128
128
|
else
|
129
129
|
raise 'undefined error'
|
130
130
|
end
|
131
|
-
nil
|
132
131
|
end
|
133
132
|
|
134
133
|
# Generates a quote message from this event
|
@@ -140,7 +139,8 @@ module Rubirai
|
|
140
139
|
group_id: @sender.is_a?(GroupUser) ? @sender.group.id : 0,
|
141
140
|
sender_id: @sender.id,
|
142
141
|
target_id: @sender.is_a?(GroupUser) ? @sender.group.id : @bot.qq,
|
143
|
-
origin: @message_chain
|
142
|
+
origin: @message_chain.raw,
|
143
|
+
bot: @bot
|
144
144
|
)
|
145
145
|
end
|
146
146
|
|
@@ -2,25 +2,75 @@
|
|
2
2
|
|
3
3
|
require 'rubirai/utils'
|
4
4
|
|
5
|
+
# @!method self.AtMessage(**kwargs)
|
6
|
+
# Form an {Rubirai::AtMessage}. The `display` option has no effect when
|
7
|
+
# sending at messages.
|
8
|
+
# @option kwargs [Integer] :target the target id
|
9
|
+
# @return [Rubirai::AtMessage] the message object
|
10
|
+
# @see Rubirai::AtMessage.from
|
11
|
+
# @!method self.QuoteMessage(**kwargs)
|
12
|
+
# Form a {Rubirai::QuoteMessage}.
|
13
|
+
# @return [Rubirai::QuoteMessage] the message object
|
14
|
+
# @see Rubirai::QuoteMessage.from
|
15
|
+
# @!method self.AtAllMessage()
|
16
|
+
# Form an {Rubirai::AtAllMessage}.
|
17
|
+
# @return [Rubirai::AtAllMessage] the message object
|
18
|
+
# @see Rubirai::AtAllMessage.from
|
19
|
+
# @!method self.FaceMessage(**kwargs)
|
20
|
+
# Form a {Rubirai::FaceMessage}. Only needs to give one of the two arguments.
|
21
|
+
# @option kwargs [Integer] :face_id the face id (high priority)
|
22
|
+
# @option kwargs [String] :name the face's name (low priority)
|
23
|
+
# @return [Rubirai::FaceMessage] the message object
|
24
|
+
# @see Rubirai::FaceMessage.from
|
25
|
+
# @!method self.PlainMessage(**kwargs)
|
26
|
+
# @option kwargs [String] :text the plain text
|
27
|
+
# @return [Rubirai::PlainMessage] the message object
|
28
|
+
# @see Rubirai::PlainMessage.from
|
29
|
+
# @!method self.ImageMessage(**kwargs)
|
30
|
+
# Form an {Rubirai::ImageMessage}. Only needs to give one of the three arguments.
|
31
|
+
# @option kwargs [String] :image_id the image id
|
32
|
+
# @option kwargs [String] :url the url of the image
|
33
|
+
# @option kwargs [String] :path the local path of the image
|
34
|
+
# @return [Rubirai::ImageMessage] the message object
|
35
|
+
# @see Rubirai::ImageMessage.from
|
36
|
+
# @!method self.FlashImageMessage(**kwargs)
|
37
|
+
# Form a {Rubirai::FlashImageMessage}. Only needs to give one of the three arguments.
|
38
|
+
# @option kwargs [String] :image_id the image id
|
39
|
+
# @option kwargs [String] :url the url of the image
|
40
|
+
# @option kwargs [String] :path the local path of the image
|
41
|
+
# @return [Rubirai::FlashImageMessage] the message object
|
42
|
+
# @see Rubirai::FlashImageMessage.from
|
43
|
+
# @!method self.VoiceMessage(**kwargs)
|
44
|
+
# Form a {Rubirai::VoiceMessage}. Only needs to give one of the three arguments.
|
45
|
+
# @option kwargs [String] :voice_id the voice id
|
46
|
+
# @option kwargs [String] :url the url of the voice
|
47
|
+
# @option kwargs [String] :path the local path of the voice
|
48
|
+
# @return [Rubirai::VoiceMessage] the message object
|
49
|
+
# @see Rubirai::VoiceMessage.from
|
50
|
+
# @!method self.XmlMessage(**kwargs)
|
51
|
+
# Form a {Rubirai::XmlMessage}.
|
52
|
+
# @option kwargs [String] :xml the xml body
|
53
|
+
# @return [Rubirai::XmlMessage] the message object
|
54
|
+
# @see Rubirai::XmlMessage.from
|
55
|
+
# @!method self.JsonMessage(**kwargs)
|
56
|
+
# Form a {Rubirai::JsonMessage}.
|
57
|
+
# @option kwargs [String] :json the json body
|
58
|
+
# @return [Rubirai::JsonMessage] the message object
|
59
|
+
# @see Rubirai::JsonMessage.from
|
60
|
+
# @!method self.AppMessage(**kwargs)
|
61
|
+
# Form an {Rubirai::AppMessage}.
|
62
|
+
# @option kwargs [String] :content the app body
|
63
|
+
# @return [Rubirai::AppMessage] the message object
|
64
|
+
# @see Rubirai::AppMessage.from
|
65
|
+
# @!method self.PokeMessage(**kwargs)
|
66
|
+
# Form a {Rubirai::PokeMessage}.
|
67
|
+
# @option kwargs [String] :name the poke name
|
68
|
+
# @return [Rubirai::PokeMessage] the message object
|
69
|
+
# @see Rubirai::PokeMessage.from
|
5
70
|
module Rubirai
|
6
71
|
# The message abstract class.
|
7
72
|
#
|
8
73
|
# @abstract
|
9
|
-
# @!method self.AtMessage(**kwargs)
|
10
|
-
# @param kwargs [Hash{Symbol => Object}] arguments
|
11
|
-
# @return [Rubirai::AtMessage]
|
12
|
-
# @!method self.QuoteMessage(**kwargs)
|
13
|
-
# @param kwargs [Hash{Symbol => Object}] arguments
|
14
|
-
# @return [Rubirai::QuoteMessage]
|
15
|
-
# @!method self.AtAllMessage(**kwargs)
|
16
|
-
# @param kwargs [Hash{Symbol => Object}] arguments
|
17
|
-
# @return [Rubirai::AtAllMessage]
|
18
|
-
# @!method self.FaceMessage(**kwargs)
|
19
|
-
# @param kwargs [Hash{Symbol => Object}] arguments
|
20
|
-
# @return [Rubirai::FaceMessage]
|
21
|
-
# @!method self.PlainMessage(**kwargs)
|
22
|
-
# @option text [String] the plain text
|
23
|
-
# @return [Rubirai::PlainMessage]
|
24
74
|
class Message
|
25
75
|
# @!attribute [r] bot
|
26
76
|
# @return [Bot] the bot
|
@@ -30,7 +80,7 @@ module Rubirai
|
|
30
80
|
|
31
81
|
# Objects to {Rubirai::Message}
|
32
82
|
#
|
33
|
-
# @param msg [Rubirai::Message, Hash, Object] the object to transform to a message
|
83
|
+
# @param msg [Rubirai::Message, Hash{String => Object}, Object] the object to transform to a message
|
34
84
|
# @return [Rubirai::Message] the message
|
35
85
|
def self.to_message(msg, bot = nil)
|
36
86
|
# noinspection RubyYardReturnMatch
|
@@ -150,6 +200,10 @@ module Rubirai
|
|
150
200
|
end
|
151
201
|
end
|
152
202
|
|
203
|
+
# {include:Rubirai::Message.to_message}
|
204
|
+
# @param obj [Message, Hash{String => Object}, Object] the object
|
205
|
+
# @return [Message] the message
|
206
|
+
# @see Rubirai::Message.to_message
|
153
207
|
def self.Message(obj, bot = nil)
|
154
208
|
Message.to_message obj, bot
|
155
209
|
end
|
@@ -185,7 +239,9 @@ module Rubirai
|
|
185
239
|
# @return [Integer] the original receiver's (group or user) id
|
186
240
|
# @!attribute [r] origin
|
187
241
|
# @return [MessageChain] the original message chain
|
188
|
-
|
242
|
+
# @!method from(**kwargs)
|
243
|
+
# Form a {QuoteMessage}.
|
244
|
+
set_message :Quote, :id, :group_id, :sender_id, :target_id, :origin, :origin_raw
|
189
245
|
|
190
246
|
# @private
|
191
247
|
def initialize(hash, bot = nil)
|
@@ -195,6 +251,18 @@ module Rubirai
|
|
195
251
|
@sender_id = hash['senderId']
|
196
252
|
@target_id = hash['targetId']
|
197
253
|
@origin = MessageChain.make(*hash['origin'], bot: bot)
|
254
|
+
@origin_raw = hash['origin']
|
255
|
+
end
|
256
|
+
|
257
|
+
def to_h
|
258
|
+
{
|
259
|
+
'type' => 'Quote',
|
260
|
+
'id' => @id,
|
261
|
+
'groupId' => @group_id,
|
262
|
+
'senderId' => @sender_id,
|
263
|
+
'targetId' => @target_id,
|
264
|
+
'origin' => @origin_raw || @origin.to_a
|
265
|
+
}.compact
|
198
266
|
end
|
199
267
|
end
|
200
268
|
|
@@ -205,15 +273,16 @@ module Rubirai
|
|
205
273
|
# @!attribute [r] display
|
206
274
|
# @return [String] the displayed name (not used when sending)
|
207
275
|
# @!method from(**kwargs)
|
208
|
-
#
|
276
|
+
# Form an {AtMessage}. The `display` option has no effect when
|
277
|
+
# sending at messages.
|
278
|
+
# @option kwargs [Integer] :target the target id
|
209
279
|
# @return [AtMessage] the message object
|
210
280
|
set_message :At, :target, :display
|
211
281
|
end
|
212
282
|
|
213
283
|
# The At All message type
|
214
284
|
class AtAllMessage < Message
|
215
|
-
# @!method from(
|
216
|
-
# @param kwargs [Hash{Symbol => Object}] the fields to set
|
285
|
+
# @!method from()
|
217
286
|
# @return [AtAllMessage] the message object
|
218
287
|
set_message :AtAll
|
219
288
|
end
|
@@ -224,6 +293,12 @@ module Rubirai
|
|
224
293
|
# @return [Integer] the face's id
|
225
294
|
# @!attribute [r] name
|
226
295
|
# @return [String, nil] the face's name
|
296
|
+
# @!method from(**kwargs)
|
297
|
+
# Form a {Rubirai::FaceMessage}. Only needs to give one of the two arguments.
|
298
|
+
# @option kwargs [Integer] :face_id the face id (high priority)
|
299
|
+
# @option kwargs [String] :name the face's name (low priority)
|
300
|
+
# @return [Rubirai::FaceMessage] the message object
|
301
|
+
# @!scope class
|
227
302
|
set_message :Face, :face_id, :name
|
228
303
|
end
|
229
304
|
|
@@ -231,6 +306,10 @@ module Rubirai
|
|
231
306
|
class PlainMessage < Message
|
232
307
|
# @!attribute [r] text
|
233
308
|
# @return [String] the text
|
309
|
+
# @!method from(**kwargs)
|
310
|
+
# @option kwargs [String] :text the plain text
|
311
|
+
# @return [PlainMessage] the message object
|
312
|
+
# @!scope class
|
234
313
|
set_message :Plain, :text
|
235
314
|
end
|
236
315
|
|
@@ -238,21 +317,54 @@ module Rubirai
|
|
238
317
|
# Only one out of the three fields is needed to form the message.
|
239
318
|
class ImageMessage < Message
|
240
319
|
# @!attribute [r] image_id
|
241
|
-
# @return [
|
320
|
+
# @return [String, nil] the image id from mirai
|
242
321
|
# @!attribute [r] url
|
243
322
|
# @return [String, nil] the url of the image
|
244
323
|
# @!attribute [r] path
|
245
324
|
# @return [String, nil] the local path of the image
|
325
|
+
# @!method from(**kwargs)
|
326
|
+
# Form an {Rubirai::ImageMessage}. Only needs to give one of the three arguments.
|
327
|
+
# @option kwargs [String] :image_id the image id
|
328
|
+
# @option kwargs [String] :url the url of the image
|
329
|
+
# @option kwargs [String] :path the local path of the image
|
330
|
+
# @return [Rubirai::ImageMessage] the message object
|
331
|
+
# @!scope class
|
246
332
|
set_message :Image, :image_id, :url, :path
|
247
333
|
end
|
248
334
|
|
249
335
|
# The flash image message type
|
250
336
|
class FlashImageMessage < Message
|
337
|
+
# @!attribute [r] image_id
|
338
|
+
# @return [String, nil] the image id from mirai
|
339
|
+
# @!attribute [r] url
|
340
|
+
# @return [String, nil] the url of the image
|
341
|
+
# @!attribute [r] path
|
342
|
+
# @return [String, nil] the local path of the image
|
343
|
+
# @!method from(**kwargs)
|
344
|
+
# Form a {Rubirai::FlashImageMessage}. Only needs to give one of the three arguments.
|
345
|
+
# @option kwargs [String] :image_id the image id
|
346
|
+
# @option kwargs [String] :url the url of the image
|
347
|
+
# @option kwargs [String] :path the local path of the image
|
348
|
+
# @return [Rubirai::FlashImageMessage] the message object
|
349
|
+
# @!scope class
|
251
350
|
set_message :FlashImage, :image_id, :url, :path
|
252
351
|
end
|
253
352
|
|
254
353
|
# The voice message type
|
255
354
|
class VoiceMessage < Message
|
355
|
+
# @!attribute [r] voice_id
|
356
|
+
# @return [String, nil] the voice id from mirai
|
357
|
+
# @!attribute [r] url
|
358
|
+
# @return [String, nil] the url of the voice
|
359
|
+
# @!attribute [r] path
|
360
|
+
# @return [String, nil] the local path of the voice
|
361
|
+
# @!method from(**kwargs)
|
362
|
+
# Form a {Rubirai::VoiceMessage}. Only needs to give one of the three arguments.
|
363
|
+
# @option kwargs [String] :voice_id the voice id
|
364
|
+
# @option kwargs [String] :url the url of the voice
|
365
|
+
# @option kwargs [String] :path the local path of the voice
|
366
|
+
# @return [Rubirai::VoiceMessage] the message object
|
367
|
+
# @!scope class
|
256
368
|
set_message :Voice, :voice_id, :url, :path
|
257
369
|
end
|
258
370
|
|
@@ -260,6 +372,11 @@ module Rubirai
|
|
260
372
|
class XmlMessage < Message
|
261
373
|
# @!attribute [r] xml
|
262
374
|
# @return [String] the xml content
|
375
|
+
# @!method from(**kwargs)
|
376
|
+
# Form a {Rubirai::XmlMessage}.
|
377
|
+
# @option kwargs [String] :xml the xml body
|
378
|
+
# @return [Rubirai::XmlMessage] the message object
|
379
|
+
# @!scope class
|
263
380
|
set_message :Xml, :xml
|
264
381
|
end
|
265
382
|
|
@@ -267,6 +384,11 @@ module Rubirai
|
|
267
384
|
class JsonMessage < Message
|
268
385
|
# @!attribute [r] json
|
269
386
|
# @return [String] the json content
|
387
|
+
# @!method from(**kwargs)
|
388
|
+
# Form a {Rubirai::JsonMessage}.
|
389
|
+
# @option kwargs [String] :json the json body
|
390
|
+
# @return [Rubirai::JsonMessage] the message object
|
391
|
+
# @!scope class
|
270
392
|
set_message :Json, :json
|
271
393
|
end
|
272
394
|
|
@@ -274,27 +396,72 @@ module Rubirai
|
|
274
396
|
class AppMessage < Message
|
275
397
|
# @!attribute [r] content
|
276
398
|
# @return [String] the app content
|
399
|
+
# @!method from(**kwargs)
|
400
|
+
# Form an {Rubirai::AppMessage}.
|
401
|
+
# @option kwargs [String] :content the app body
|
402
|
+
# @return [Rubirai::AppMessage] the message object
|
403
|
+
# @!scope class
|
277
404
|
set_message :App, :content
|
278
405
|
end
|
279
406
|
|
407
|
+
# The poke message type
|
280
408
|
class PokeMessage < Message
|
409
|
+
# @!attribute [r] name
|
410
|
+
# @return [String] type (name) of the poke
|
411
|
+
# @!method from(**kwargs)
|
412
|
+
# Form an {Rubirai::PokeMessage}.
|
413
|
+
# @option kwargs [String] :name the name (type) of poke
|
414
|
+
# @return [Rubirai::PokeMessage] the message object
|
415
|
+
# @!scope class
|
281
416
|
set_message :Poke, :name
|
282
417
|
end
|
283
418
|
|
419
|
+
# The forward message type
|
284
420
|
class ForwardMessage < Message
|
421
|
+
# A message node in the forward message list
|
422
|
+
#
|
423
|
+
# @!attribute [r] sender_id
|
424
|
+
# @return [Integer] sender id
|
425
|
+
# @!attribute [r] time
|
426
|
+
# @return [Integer] send timestamp (second)
|
427
|
+
# @!attribute [r] sender_name
|
428
|
+
# @return [String] the sender name
|
429
|
+
# @!attribute [r] message_chain
|
430
|
+
# @return [MessageChain] the message chain
|
285
431
|
class Node
|
286
432
|
attr_reader :sender_id, :time, :sender_name, :message_chain
|
287
433
|
|
434
|
+
# @private
|
288
435
|
def initialize(hash, bot = nil)
|
436
|
+
return unless hash
|
289
437
|
@sender_id = hash['senderId']
|
290
438
|
@time = hash['time']
|
291
439
|
@sender_name = hash['senderName']
|
292
440
|
@message_chain = MessageChain.make(*hash['messageChain'], bot: bot)
|
293
441
|
end
|
442
|
+
|
443
|
+
def self.from(**kwargs)
|
444
|
+
n = new({})
|
445
|
+
%i[sender_id time sender_name message_chain].each do |attr|
|
446
|
+
n.instance_variable_set("@#{attr}", kwargs[attr])
|
447
|
+
end
|
448
|
+
end
|
294
449
|
end
|
295
450
|
|
451
|
+
# @!attribute [r] title
|
452
|
+
# @return [String] the title
|
453
|
+
# @!attribute [r] brief
|
454
|
+
# @return [String] the brief text
|
455
|
+
# @!attribute [r] source
|
456
|
+
# @return [String] the source text
|
457
|
+
# @!attribute [r] summary
|
458
|
+
# @return [String] the summary text
|
459
|
+
# @!attribute [r] node_list
|
460
|
+
# @return [Array<Node>] the node list
|
461
|
+
# @see Node
|
296
462
|
set_message :Forward, :title, :brief, :source, :summary, :node_list
|
297
463
|
|
464
|
+
# @private
|
298
465
|
def initialize(hash, bot = nil)
|
299
466
|
super :Forward, bot
|
300
467
|
@title = hash['title']
|
@@ -307,15 +474,42 @@ module Rubirai
|
|
307
474
|
end
|
308
475
|
end
|
309
476
|
|
477
|
+
# The file message type
|
310
478
|
class FileMessage < Message
|
479
|
+
# @!attribute [r] id
|
480
|
+
# @return [String] the file id
|
481
|
+
# @!attribute [r] internal_id
|
482
|
+
# @return [Integer] the internal id needed by server
|
483
|
+
# @!attribute [r] name
|
484
|
+
# @return [String] the filename
|
485
|
+
# @!attribute [r] size
|
486
|
+
# @return [Integer] the file size
|
311
487
|
set_message :File, :id, :internal_id, :name, :size
|
312
488
|
end
|
313
489
|
|
490
|
+
# The music share card message
|
314
491
|
class MusicShareMessage < Message
|
492
|
+
# List all kinds of music providers
|
493
|
+
#
|
494
|
+
# @return [Array<String>] kinds
|
315
495
|
def self.all_kinds
|
316
496
|
%w[NeteaseCloudMusic QQMusic MiguMusic]
|
317
497
|
end
|
318
498
|
|
499
|
+
# @!attribute [r] kind
|
500
|
+
# @return [String] the kind of music provider
|
501
|
+
# @!attribute [r] title
|
502
|
+
# @return [String] the music card title
|
503
|
+
# @!attribute [r] summary
|
504
|
+
# @return [String] the music card summary
|
505
|
+
# @!attribute [r] jump_url
|
506
|
+
# @return [String] the jump url
|
507
|
+
# @!attribute [r] picture_url
|
508
|
+
# @return [String] the picture's url
|
509
|
+
# @!attribute [r] music_url
|
510
|
+
# @return [String] the music's url
|
511
|
+
# @!attribute [r] brief
|
512
|
+
# @return [String, nil] the brief message (optional)
|
319
513
|
set_message :MusicShare, :kind, :title, :summary, :jump_url, :picture_url, :music_url, :brief do |hash|
|
320
514
|
raise(RubiraiError, 'non valid music type') unless all_kinds.include? hash['kind']
|
321
515
|
end
|
@@ -12,11 +12,13 @@ module Rubirai
|
|
12
12
|
# @return [Bot] the bot object
|
13
13
|
# @!attribute [r] id
|
14
14
|
# @return [Integer, nil] the message id, may be `nil`
|
15
|
+
# @!attribute [r] raw
|
16
|
+
# @return [Hash{String => Object}, nil] the raw message chain, may be `nil`
|
15
17
|
# @!attribute [r] send_time
|
16
18
|
# @return [Integer, nil] the send time of the message chain, may be `nil`
|
17
19
|
# @!attribute [r] messages
|
18
20
|
# @return [Array<Message>] the raw message array
|
19
|
-
attr_reader :bot, :id, :send_time, :messages
|
21
|
+
attr_reader :bot, :id, :raw, :send_time, :messages
|
20
22
|
|
21
23
|
# Makes a message chain from a list of messages
|
22
24
|
#
|
@@ -87,10 +89,10 @@ module Rubirai
|
|
87
89
|
# @private
|
88
90
|
# @param bot [Rubirai::Bot, nil]
|
89
91
|
# @param source [Array, nil]
|
90
|
-
# @param id [Integer, nil]
|
91
92
|
def initialize(bot = nil, source = nil)
|
92
93
|
@bot = bot
|
93
94
|
@messages = []
|
95
|
+
@raw = source
|
94
96
|
return unless source
|
95
97
|
raise(MiraiError, 'source is not array') unless source.is_a? Array
|
96
98
|
raise(MiraiError, 'length is zero') if source.empty?
|
@@ -130,6 +132,9 @@ module Rubirai
|
|
130
132
|
end
|
131
133
|
|
132
134
|
# Makes a message chain. See {MessageChain#make}.
|
135
|
+
#
|
136
|
+
# @return [MessageChain] the message chain made.
|
137
|
+
# @see MessageChain#make
|
133
138
|
def self.MessageChain(*messages, bot: nil)
|
134
139
|
MessageChain.make(*messages, bot: bot)
|
135
140
|
end
|
@@ -5,6 +5,11 @@ require 'rubirai/utils'
|
|
5
5
|
module Rubirai
|
6
6
|
# The abstract class for group information
|
7
7
|
# @abstract
|
8
|
+
#
|
9
|
+
# @!attribute [r] raw
|
10
|
+
# @return [Hash{String => Object}] the raw hash
|
11
|
+
# @!attribute [r] bot
|
12
|
+
# @return [Bot] the bot
|
8
13
|
class GroupInfo
|
9
14
|
# @private
|
10
15
|
def self.set_fields(*fields, **default_values)
|
@@ -34,10 +39,6 @@ module Rubirai
|
|
34
39
|
attr_writer(*fields)
|
35
40
|
end
|
36
41
|
|
37
|
-
# @!attribute [r] raw
|
38
|
-
# @return [Hash{String => Object}] the raw hash
|
39
|
-
# @!attribute [r] bot
|
40
|
-
# @return [Bot] the bot
|
41
42
|
attr_reader :raw, :bot
|
42
43
|
|
43
44
|
# @private
|
data/lib/rubirai/version.rb
CHANGED
data/spec/events/event_spec.rb
CHANGED
@@ -76,6 +76,7 @@ describe Rubirai::Event do
|
|
76
76
|
expect(e.class.type).to eq(:BotGroupPermissionChangeEvent)
|
77
77
|
expect(e.origin).to eq(Rubirai::Group::Permission::MEMBER)
|
78
78
|
expect(e.new).to eq(Rubirai::Group::Permission::ADMINISTRATOR)
|
79
|
+
expect(e.group).to be_a(Rubirai::Group)
|
79
80
|
expect(e.group.id).to eq(123456789)
|
80
81
|
end
|
81
82
|
end
|
data/spec/message_spec.rb
CHANGED
@@ -84,4 +84,62 @@ describe 'message api' do
|
|
84
84
|
expect(@mirai_bot.recall(123)).to be_nil
|
85
85
|
end.not_to raise_error
|
86
86
|
end
|
87
|
+
|
88
|
+
it 'should be able to respond to a message event' do
|
89
|
+
e = Rubirai::Event.parse({
|
90
|
+
"type": 'GroupMessage',
|
91
|
+
"messageChain": [
|
92
|
+
{
|
93
|
+
"type": 'Source',
|
94
|
+
"id": 123456,
|
95
|
+
"time": 123456789
|
96
|
+
}.stringify_keys,
|
97
|
+
{
|
98
|
+
"type": 'Plain',
|
99
|
+
"text": 'Miral牛逼'
|
100
|
+
}.stringify_keys
|
101
|
+
],
|
102
|
+
"sender": {
|
103
|
+
"id": 123456789,
|
104
|
+
"memberName": '化腾',
|
105
|
+
"permission": 'MEMBER',
|
106
|
+
"group": {
|
107
|
+
"id": 1234567890,
|
108
|
+
"name": 'Miral Technology',
|
109
|
+
"permission": 'MEMBER'
|
110
|
+
}.stringify_keys
|
111
|
+
}.stringify_keys
|
112
|
+
}.stringify_keys, @mirai_bot)
|
113
|
+
stub_request(:post, @mirai_bot.gen_uri('/sendGroupMessage'))
|
114
|
+
.with(body: {
|
115
|
+
"sessionKey": 'test_session_key',
|
116
|
+
"target": 1234567890,
|
117
|
+
"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
|
+
{ "text": 'hi', "type": 'Plain' }
|
134
|
+
]
|
135
|
+
})
|
136
|
+
.to_return(status: 200, body: %({
|
137
|
+
"code": 0,
|
138
|
+
"msg": "success",
|
139
|
+
"messageId": 1234567890
|
140
|
+
}))
|
141
|
+
expect(e).to be_a(Rubirai::GroupMessageEvent)
|
142
|
+
msg_id = e.respond 'hi', quote: true
|
143
|
+
expect(msg_id).to eq(1234567890)
|
144
|
+
end
|
87
145
|
end
|
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.0
|
4
|
+
version: 0.1.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-
|
11
|
+
date: 2021-06-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: concurrent-ruby
|
@@ -111,9 +111,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
111
111
|
version: '2.6'
|
112
112
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
113
113
|
requirements:
|
114
|
-
- - "
|
114
|
+
- - ">="
|
115
115
|
- !ruby/object:Gem::Version
|
116
|
-
version:
|
116
|
+
version: '0'
|
117
117
|
requirements: []
|
118
118
|
rubygems_version: 3.0.3.1
|
119
119
|
signing_key:
|