Class: Rubirai::Bot

Inherits:
Object
  • Object
show all
Defined in:
lib/rubirai.rb,
lib/rubirai/auth.rb,
lib/rubirai/listing.rb,
lib/rubirai/message.rb,
lib/rubirai/session.rb,
lib/rubirai/listener.rb,
lib/rubirai/multipart.rb,
lib/rubirai/event_recv.rb,
lib/rubirai/event_resp.rb,
lib/rubirai/management.rb,
lib/rubirai/plugin_info.rb

Overview

Bot represents a QQ bot at mirai side. All functions are API calls to the http plugin.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port = nil) ⇒ Bot

Initializes the bot

Parameters:

  • host (String)

    the host (IP or domain)

  • port (String, Integer, nil) (defaults to: nil)

    the port number (default is 80 for http)



35
36
37
38
# File 'lib/rubirai.rb', line 35

def initialize(host, port = nil)
  @base_uri = "http://#{host}#{":#{port}" if port}"
  @listener_funcs = []
end

Instance Attribute Details

#base_uriString (readonly)

Returns the base uri of mirai-api-http which the bot will send messages to.

Returns:

  • (String)

    the base uri of mirai-api-http which the bot will send messages to



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rubirai.rb', line 26

class Bot
  attr_reader :base_uri, :session, :qq

  alias id qq

  # Initializes the bot
  #
  # @param host [String] the host (IP or domain)
  # @param port [String, Integer, nil] the port number (default is 80 for http)
  def initialize(host, port = nil)
    @base_uri = "http://#{host}#{":#{port}" if port}"
    @listener_funcs = []
  end

  # @private
  def gen_uri(path)
    URI.join(base_uri, path)
  end

  # @private
  def self.ensure_type_in(type, *types)
    types = types.map { |x| x.to_s.downcase }
    type.to_s.downcase.must_be_one_of! types, RubiraiError, "not valid type: should be one of #{types}"
  end

  private

  def call(method, path, **kwargs)
    return unless %i[get post].include?(method) && HTTP.respond_to?(method)

    resp = HTTP.send method, gen_uri(path), kwargs
    raise(HttpResponseError, resp.code) unless resp.status.success?

    body = JSON.parse(resp.body)
    if (body.is_a? Hash) && (body.include? 'code') && (body['code'] != 0)
      raise MiraiError.new(body['code'], body['msg'])
    end

    body
  end
end

#qqString, Integer (readonly) Also known as: id

Returns the qq of the bot.

Returns:

  • (String, Integer)

    the qq of the bot



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rubirai.rb', line 26

class Bot
  attr_reader :base_uri, :session, :qq

  alias id qq

  # Initializes the bot
  #
  # @param host [String] the host (IP or domain)
  # @param port [String, Integer, nil] the port number (default is 80 for http)
  def initialize(host, port = nil)
    @base_uri = "http://#{host}#{":#{port}" if port}"
    @listener_funcs = []
  end

  # @private
  def gen_uri(path)
    URI.join(base_uri, path)
  end

  # @private
  def self.ensure_type_in(type, *types)
    types = types.map { |x| x.to_s.downcase }
    type.to_s.downcase.must_be_one_of! types, RubiraiError, "not valid type: should be one of #{types}"
  end

  private

  def call(method, path, **kwargs)
    return unless %i[get post].include?(method) && HTTP.respond_to?(method)

    resp = HTTP.send method, gen_uri(path), kwargs
    raise(HttpResponseError, resp.code) unless resp.status.success?

    body = JSON.parse(resp.body)
    if (body.is_a? Hash) && (body.include? 'code') && (body['code'] != 0)
      raise MiraiError.new(body['code'], body['msg'])
    end

    body
  end
end

#sessionString (readonly)

Returns the session key.

Returns:

  • (String)

    the session key



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rubirai.rb', line 26

class Bot
  attr_reader :base_uri, :session, :qq

  alias id qq

  # Initializes the bot
  #
  # @param host [String] the host (IP or domain)
  # @param port [String, Integer, nil] the port number (default is 80 for http)
  def initialize(host, port = nil)
    @base_uri = "http://#{host}#{":#{port}" if port}"
    @listener_funcs = []
  end

  # @private
  def gen_uri(path)
    URI.join(base_uri, path)
  end

  # @private
  def self.ensure_type_in(type, *types)
    types = types.map { |x| x.to_s.downcase }
    type.to_s.downcase.must_be_one_of! types, RubiraiError, "not valid type: should be one of #{types}"
  end

  private

  def call(method, path, **kwargs)
    return unless %i[get post].include?(method) && HTTP.respond_to?(method)

    resp = HTTP.send method, gen_uri(path), kwargs
    raise(HttpResponseError, resp.code) unless resp.status.success?

    body = JSON.parse(resp.body)
    if (body.is_a? Hash) && (body.include? 'code') && (body['code'] != 0)
      raise MiraiError.new(body['code'], body['msg'])
    end

    body
  end
end

Instance Method Details

#aboutHash{String => Object}

Get Mirai API plugin information.

The information is like

{
  'version' => '1.0.0'
}

Returns:

  • (Hash{String => Object})

    plugin data



15
16
17
18
# File 'lib/rubirai/plugin_info.rb', line 15

def about
  v = call :get, '/about'
  v['data']
end

#add_listener(&listener_block) ⇒ void

This method returns an undefined value.

Add a listener



39
40
41
# File 'lib/rubirai/listener.rb', line 39

def add_listener(&listener_block)
  @listener_funcs << listener_block
end

#bind(qq, session = nil) ⇒ void

This method returns an undefined value.

Verify and start a session. Also bind the session to a bot with the qq id.

Parameters:

  • qq (String, Integer)

    qq id

  • session (String, nil) (defaults to: nil)

    the session key. Set to nil will use the saved credentials.



17
18
19
20
21
22
23
24
# File 'lib/rubirai/auth.rb', line 17

def bind(qq, session = nil)
  check qq, session

  call :post, '/bind', json: { "sessionKey": @session || session, "qq": qq.to_i }
  @session ||= session
  @qq = qq
  nil
end

#clear_listenervoid

This method returns an undefined value.

Clear all listeners



46
47
48
# File 'lib/rubirai/listener.rb', line 46

def clear_listener
  @listener_funcs.clear
end

#count_cached_messageInteger

Get the number of cached messages in mirai-http-api

Returns:

  • (Integer)

    the number of cached messages



64
65
66
67
68
69
# File 'lib/rubirai/event_recv.rb', line 64

def count_cached_message
  resp = call :get, '/countMessage', params: {
    sessionKey: @session
  }
  resp['data']
end

#fetch_latest_message(count = 10) ⇒ Array<Event> Also known as: fetch_latest_messages, fetch_latest_event, fetch_latest_events

Fetch count number of latest events.

Parameters:

  • count (Integer) (defaults to: 10)

    the number of events to fetch

Returns:

  • (Array<Event>)

    the event objects



21
22
23
# File 'lib/rubirai/event_recv.rb', line 21

def fetch_latest_message(count = 10)
  get_events '/fetchLatestMessage', count
end

#fetch_message(count = 10) ⇒ Array<Event> Also known as: fetch_messages, fetch_event, fetch_events

Fetch count number of oldest events.

Parameters:

  • count (Integer) (defaults to: 10)

    the number of events to fetch

Returns:

  • (Array<Event>)

    the event objects.



10
11
12
# File 'lib/rubirai/event_recv.rb', line 10

def fetch_message(count = 10)
  get_events '/fetchMessage', count
end

#friend_listArray<User>

Get friend list of the bot

Returns:

  • (Array<User>)

    list of friends



10
11
12
13
14
15
# File 'lib/rubirai/listing.rb', line 10

def friend_list
  resp = call :get, '/friendList', params: {
    sessionKey: @session
  }
  resp['data'].map { |friend| User.new(friend, self) }
end

#get_group_config(group_id) ⇒ GroupConfig

Get group config

Parameters:

  • group_id (Integer)

    group id

Returns:



93
94
95
96
97
98
99
# File 'lib/rubirai/management.rb', line 93

def get_group_config(group_id)
  resp = call :get, '/groupConfig', params: {
    sessionKey: @session,
    target: group_id
  }
  GroupConfig.new resp, self
end

#get_group_file_info(group_id, file_or_id) ⇒ GroupFile

Get the info about a group file

Parameters:

  • group_id (Integer)

    the group id

  • file_or_id (GroupFileSimple, String)

    the file id, e.g. /xxx-xxx-xxx-xxx

Returns:

Raises:



175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/rubirai/management.rb', line 175

def get_group_file_info(group_id, file_or_id)
  file_or_id.must_be! [GroupFileSimple, String], RubiraiError, 'must be GroupFileSimple or String'
  raise RubiraiError, "#{file_or_id} is not a file" if file_or_id.is_a?(GroupFileSimple) && !file_or_id.is_file?
  id = file_or_id.is_a? String ? file_or_id : file_or_id.id

  resp = call :get, '/groupFileInfo', params: {
    sessionKey: @session,
    target: group_id,
    id: id
  }
  GroupFile.new resp, self
end

#get_group_file_list(group_id, dir = nil) ⇒ Array<GroupFileSimple>

Get the group files as a list

Parameters:

  • group_id (Integer)

    the group id

  • dir (String, nil) (defaults to: nil)

    the directory to get. If nil, then the root directory is asserted.

Returns:



160
161
162
163
164
165
166
167
168
# File 'lib/rubirai/management.rb', line 160

def get_group_file_list(group_id, dir = nil)
  resp = call :get, '/groupFileList', params: {
    sessionKey: @session,
    target: group_id,
    dir: dir
  }.compact
  resp.must_be! Array # assert resp is Array
  resp.map { |f| GroupFileSimple.new(f, self) }
end

#get_member_info(group_id, member_id) ⇒ MemberInfo

Get member info

Parameters:

  • group_id (Integer)

    group id

  • member_id (Integer)

    the member's qq id

Returns:



125
126
127
128
129
130
131
132
# File 'lib/rubirai/management.rb', line 125

def get_member_info(group_id, member_id)
  resp = call :get, '/memberInfo', params: {
    sessionKey: @session,
    target: group_id,
    memberId: member_id
  }
  MemberInfo.new resp, self
end

#get_session_cfgHash{String => Object}

Get the config related to this session

Returns:

  • (Hash{String => Object})

    the config



7
8
9
10
11
# File 'lib/rubirai/session.rb', line 7

def get_session_cfg
  call :get, '/config', params: {
    sessionKey: @session
  }
end

#group_file_delete(group_id, file_id) ⇒ Object



217
218
219
220
221
222
223
224
# File 'lib/rubirai/management.rb', line 217

def group_file_delete(group_id, file_id)
  call :post, '/groupFileDelete', json: {
    sessionKey: @session,
    target: group_id,
    id: file_id
  }
  nil
end

#group_file_mv(group_id, file_id, path) ⇒ Object



207
208
209
210
211
212
213
214
215
# File 'lib/rubirai/management.rb', line 207

def group_file_mv(group_id, file_id, path)
  call :post, '/groupFileMove', json: {
    sessionKey: @session,
    target: group_id,
    id: file_id,
    movePath: path
  }
  nil
end

#group_listArray<Group>

Get group list of the bot

Returns:

  • (Array<Group>)

    list of groups



19
20
21
22
23
24
# File 'lib/rubirai/listing.rb', line 19

def group_list
  resp = call :get, '/groupList', params: {
    sessionKey: @session
  }
  resp['data'].map { |group| Group.new(group, self) }
end

#group_mkdir(group_id, dir) ⇒ Object



198
199
200
201
202
203
204
205
# File 'lib/rubirai/management.rb', line 198

def group_mkdir(group_id, dir)
  call :post, '/groupMkdir', json: {
    sessionKey: @session,
    group: group_id,
    dir: dir
  }
  nil
end

#group_set_essence(msg_id) ⇒ Object



226
227
228
229
230
231
232
# File 'lib/rubirai/management.rb', line 226

def group_set_essence(msg_id)
  call :post, '/setEssence', json: {
    sessionKey: @session,
    target: msg_id
  }
  nil
end

#kick(group_id, member_id, msg = nil) ⇒ void

This method returns an undefined value.

Kick a member from a group

Parameters:

  • group_id (Integer)

    group id

  • member_id (Integer)

    member id

  • msg (String, nil) (defaults to: nil)

    the message for the kicked



42
43
44
45
46
47
48
49
50
51
# File 'lib/rubirai/management.rb', line 42

def kick(group_id, member_id, msg = nil)
  json = {
    sessionKey: @session,
    target: group_id,
    memberId: member_id
  }
  json[:msg] = msg if msg
  call :post, '/kick', json: json
  nil
end

#login(qq, verify_key, single_mode: false) ⇒ void Also known as: connect

This method returns an undefined value.

Log you in.

Parameters:

  • qq (String, Integer)

    qq id

  • verify_key (String)

    the auth key set in the settings file for mirai-api-http.

  • single_mode (Boolean) (defaults to: false)

    if to skip binding (need to enable singeMode for mirai-http-api)

See Also:



51
52
53
54
55
# File 'lib/rubirai/auth.rb', line 51

def (qq, verify_key, single_mode: false)
  verify verify_key
  single_mode or bind(qq)
  nil
end

#logoutvoid Also known as: disconnect

This method returns an undefined value.

Log you out.

See Also:



63
64
65
# File 'lib/rubirai/auth.rb', line 63

def logout
  release
end

#member_list(group_id) ⇒ Array<GroupUser>

Get member list of a group

Parameters:

  • group_id (Integer, String)

    group id

Returns:



29
30
31
32
33
34
35
# File 'lib/rubirai/listing.rb', line 29

def member_list(group_id)
  resp = call :get, '/memberList', params: {
    sessionKey: @session,
    target: group_id
  }
  resp['data'].map { |member| GroupUser.new(member, self) }
end

#message_from_id(msg_id) ⇒ Event

Get a message event from message id

Parameters:

  • msg_id (Integer)

    message id

Returns:

  • (Event)

    the event object



54
55
56
57
58
59
60
# File 'lib/rubirai/event_recv.rb', line 54

def message_from_id(msg_id)
  resp = call :get, '/messageFromId', params: {
    sessionKey: @session,
    id: msg_id
  }
  Event.parse resp['data'], self
end

#mute(group_id, member_id, time = 0) ⇒ Object

Mute a group member

Parameters:

  • group_id (Integer)

    group id

  • member_id (Integer)

    member id

  • time (Integer) (defaults to: 0)

    the mute time



12
13
14
15
16
17
18
19
20
# File 'lib/rubirai/management.rb', line 12

def mute(group_id, member_id, time = 0)
  call :post, '/mute', json: {
    sessionKey: @session,
    target: group_id,
    memberId: member_id,
    time: time
  }
  nil
end

#mute_all(group_id) ⇒ void

This method returns an undefined value.

Mute all in a group

Parameters:

  • group_id (Integer)

    group id



69
70
71
72
73
74
75
# File 'lib/rubirai/management.rb', line 69

def mute_all(group_id)
  call :post, '/muteAll', json: {
    sessionKey: @session,
    target: group_id
  }
  nil
end

#peek_latest_message(count = 10) ⇒ Array<Event> Also known as: peek_latest_messages, peek_latest_event, peek_latest_events

Peek count number of latest events. (Will not delete from cache)

Parameters:

  • count (Integer) (defaults to: 10)

    the number of events to peek

Returns:

  • (Array<Event>)

    the event objects



43
44
45
# File 'lib/rubirai/event_recv.rb', line 43

def peek_latest_message(count = 10)
  get_events '/peekLatestMessage', count
end

#peek_message(count = 10) ⇒ Array<Event> Also known as: peek_messages, peek_event, peek_events

Peek count number of oldest events. (Will not delete from cache)

Parameters:

  • count (Integer) (defaults to: 10)

    the number of events to peek

Returns:

  • (Array<Event>)

    the event objects



32
33
34
# File 'lib/rubirai/event_recv.rb', line 32

def peek_message(count = 10)
  get_events '/peekMessage', count
end

#quit(group_id) ⇒ void

This method returns an undefined value.

Quit a group

Parameters:

  • group_id (Integer)

    group id



57
58
59
60
61
62
63
# File 'lib/rubirai/management.rb', line 57

def quit(group_id)
  call :post, '/quit', json: {
    sessionKey: @session,
    target: group_id
  }
  nil
end

#recall(msg_id) ⇒ void

This method returns an undefined value.

Recall a message

Parameters:



70
71
72
73
74
75
76
77
# File 'lib/rubirai/message.rb', line 70

def recall(msg_id)
  msg_id = msg_id.id if msg_id.is_a? Rubirai::MessageChain
  call :post, '/recall', json: {
    sessionKey: @session,
    target: msg_id
  }
  nil
end

#release(qq = nil, session = nil) ⇒ void

This method returns an undefined value.

Release a session. Only fill in the arguments when you want to control another bot on the same Mirai process.

Parameters:

  • qq (String, Integer, nil) (defaults to: nil)

    qq id. Set to nil will use the logged in bot id.

  • session (String, nil) (defaults to: nil)

    the session key. Set to nil will use the logged in credentials.

Raises:



31
32
33
34
35
36
37
38
39
40
# File 'lib/rubirai/auth.rb', line 31

def release(qq = nil, session = nil)
  qq ||= @qq
  raise RubiraiError, "not same qq: #{qq} and #{@qq}" if qq != @qq
  check qq, session

  call :post, '/release', json: { "sessionKey": @session || session, "qq": qq.to_i }
  @session = nil
  @qq = nil
  nil
end

#rename_group_file(group_id, file_id, new_name) ⇒ Object



188
189
190
191
192
193
194
195
196
# File 'lib/rubirai/management.rb', line 188

def rename_group_file(group_id, file_id, new_name)
  call :post, '/groupFileRename', json: {
    sessionKey: @session,
    target: group_id,
    id: file_id,
    rename: new_name
  }
  nil
end

#respond_to_group_invite(event_id, from_id, group_id, operation, message = '') ⇒ void

This method returns an undefined value.

Respond to group invitations (raw)

Parameters:

  • event_id (Integer)

    the event id

  • from_id (Integer)

    id of the sender

  • group_id (Integer)

    the group id

  • operation (Integer)
  • message (String) (defaults to: '')

    the message to reply



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/rubirai/event_resp.rb', line 97

def respond_to_group_invite(event_id, from_id, group_id, operation, message = '')
  call :post, '/resp/botInvitedJoinGroupRequestEvent', json: {
    sessionKey: @session,
    eventId: event_id,
    fromId: from_id,
    groupId: group_id,
    operate: operation,
    message: message
  }
  nil
end

#respond_to_member_join(event_id, from_id, group_id, operation, message = '') ⇒ void

This method returns an undefined value.

Respond to join group request (raw)

Parameters:

  • event_id (Integer)

    the event id

  • from_id (Integer)

    id of the requester

  • operation (Integer)
  • group_id (Integer)

    the group where the request is from. 0 if not from group.

  • message (String) (defaults to: '')

    the message to reply



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/rubirai/event_resp.rb', line 77

def respond_to_member_join(event_id, from_id, group_id, operation, message = '')
  call :post, '/resp/memberJoinRequestEvent', json: {
    sessionKey: @session,
    eventId: event_id,
    fromId: from_id,
    groupId: group_id,
    operate: operation,
    message: message
  }
  nil
end

#respond_to_new_friend_request(event_id, from_id, operation, group_id = 0, message = '') ⇒ void

This method returns an undefined value.

Respond to new friend request (raw)

Parameters:

  • event_id (Integer)

    the event id

  • from_id (Integer)

    id of the requester

  • operation (Integer)
  • group_id (Integer) (defaults to: 0)

    the group where the request is from. 0 if not from group.

  • message (String) (defaults to: '')

    the message to reply



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rubirai/event_resp.rb', line 57

def respond_to_new_friend_request(event_id, from_id, operation, group_id = 0, message = '')
  call :post, '/resp/newFriendRequestEvent', json: {
    sessionKey: @session,
    eventId: event_id,
    fromId: from_id,
    groupId: group_id,
    operate: operation,
    message: message
  }
  nil
end

#send_friend_msg(target_qq, *msgs, quote: nil) ⇒ Integer

Send friend message

Parameters:

  • target_qq (Integer)

    target qq id

  • msgs (Array<Rubirai::Message, Hash, String, Object>)

    messages to form a chain, can be any type

  • quote (NilClass, Integer) (defaults to: nil)

    the message id to quote, nil for not quote

Returns:

  • (Integer)

    message id



52
53
54
# File 'lib/rubirai/message.rb', line 52

def send_friend_msg(target_qq, *msgs, quote: nil)
  send_msg :friend, target_qq, *msgs, quote: quote
end

#send_group_msg(target_group_id, *msgs, quote: nil) ⇒ Integer

Send group message

Parameters:

  • target_group_id (Integer)

    group id

  • msgs (Array<Rubirai::Message, Hash, String, Object>)

    messages to form a chain, can be any type

  • quote (NilClass, Integer) (defaults to: nil)

    the message id to quote, nil for not quote

Returns:

  • (Integer)

    message id



62
63
64
# File 'lib/rubirai/message.rb', line 62

def send_group_msg(target_group_id, *msgs, quote: nil)
  send_msg :group, target_group_id, *msgs, quote: quote
end

#send_image_msg(urls, **kwargs) ⇒ Array<String>

Send image messages

Parameters:

  • urls (Array<String>)

    the urls of the images

  • kwargs (Hash)

    keys are one of [target, qq, group].

Returns:

  • (Array<String>)

    the image ids



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/rubirai/message.rb', line 84

def send_image_msg(urls, **kwargs)
  urls.must_be! Array
  urls.each do |url|
    url.must_be! String
  end
  valid = %w[target qq group]
  res = {
    sessionKey: @session,
    urls: urls
  }
  kwargs.each do |k, v|
    res[k.to_s.downcase.to_sym] = v if valid.include? k.to_s.downcase
  end
  call :post, '/sendImageMessage', json: res
end

#send_msg(type, target_id, *msgs, quote: nil) ⇒ Integer

Send friend or group message

Parameters:

  • type (Symbol, String)

    options: [group, friend]

  • target_id (Integer)

    target qq/group id

  • msgs (Array<Rubirai::Message, Hash, String, Object>)

    messages to form a chain, can be any type

  • quote (NilClass, Integer) (defaults to: nil)

    the message id to quote, nil for not quote

Returns:

  • (Integer)

    message id



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rubirai/message.rb', line 34

def send_msg(type, target_id, *msgs, quote: nil)
  self.class.ensure_type_in type, 'group', 'friend'
  chain = Rubirai::MessageChain.make(*msgs, bot: self)
  resp = call :post, "/send#{type.to_s.snake_to_camel}Message", json: {
    sessionKey: @session,
    target: target_id.to_i,
    quote: quote,
    messageChain: chain.to_a
  }.compact
  resp['messageId']
end

#send_nudge(target_id, subject_id, kind) ⇒ void

This method returns an undefined value.

Send nudge

Parameters:

  • target_id (Integer)

    target id

  • subject_id (Integer)

    the id of the place where the nudge will be seen. Should be a group id or a friend's id

  • kind (String, Symbol)

    options: [Group, Friend]



107
108
109
110
111
112
113
114
115
116
# File 'lib/rubirai/message.rb', line 107

def send_nudge(target_id, subject_id, kind)
  kind.to_s.downcase.must_be_one_of! %w[group friend], RubiraiError, 'kind must be one of group or friend'
  call :post, '/sendNudge', json: {
    sessionKey: @session,
    target: target_id,
    subject: subject_id,
    kind: kind.to_s.capitalize
  }
  nil
end

#send_temp_msg(target_qq, group_id, *msgs, quote: nil) ⇒ Integer

Send temp message

Parameters:

  • target_qq (Integer)

    target qq id

  • group_id (Integer)

    group id

  • msgs (Array<Rubirai::Message, Hash, String, Object>)

    messages to form a chain, can be any type

  • quote (NilClass, Integer) (defaults to: nil)

    the message id to quote, nil for not quote

Returns:

  • (Integer)

    message id



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rubirai/message.rb', line 15

def send_temp_msg(target_qq, group_id, *msgs, quote: nil)
  chain = Rubirai::MessageChain.make(*msgs, bot: self)
  resp = call :post, '/sendTempMessage', json: {
    sessionKey: @session,
    qq: target_qq.to_i,
    group: group_id.to_i,
    quote: quote,
    messageChain: chain.to_a
  }.compact
  resp['messageId']
end

#set_group_config(group_id, config) ⇒ void

This method returns an undefined value.

Set group config

Parameters:

  • group_id (Integer)

    group id

  • config (GroupConfig, Hash{String, Symbol => Object})

    the configuration. If given as a hash, then it should be exactly named the same as given in mirai-api-http docs.



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/rubirai/management.rb', line 108

def set_group_config(group_id, config)
  config.must_be! [GroupConfig, Hash], RubiraiError, 'must be GroupConfig or Hash'
  config.stringify_keys! if config.is_a? Hash
  config = config.to_h if config.is_a? GroupConfig
  call :post, '/groupConfig', json: {
    sessionKey: @session,
    target: group_id,
    config: config
  }
  nil
end

#set_member_info(group_id, member_id, info) ⇒ void

This method returns an undefined value.

Set member info

Parameters:

  • group_id (Integer)

    group id

  • member_id (Integer)

    the member's qq id

  • info (MemberInfo, Hash{String,Symbol => Object})

    the info to set. If given as a hash, then it should be exactly named the same as given in mirai-api-http docs.



142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/rubirai/management.rb', line 142

def set_member_info(group_id, member_id, info)
  info.must_be! [MemberInfo, Hash], RubiraiError, 'must be MemberInfo or Hash'
  info.stringify_keys! if info.is_a? Hash
  info = info.to_h if info.is_a? MemberInfo
  call :post, '/memberInfo', json: {
    sessionKey: @session,
    target: group_id,
    memberId: member_id,
    info: info
  }
  nil
end

#set_session_cfg(cache_size: nil, enable_websocket: nil) ⇒ void

This method returns an undefined value.

Set the config related to this session

Parameters:

  • cache_size (Integer, nil) (defaults to: nil)

    the cache size

  • enable_websocket (Boolean, nil) (defaults to: nil)

    if to enable websocket



17
18
19
20
21
22
23
24
# File 'lib/rubirai/session.rb', line 17

def set_session_cfg(cache_size: nil, enable_websocket: nil)
  call :post, '/config', json: {
    sessionKey: @session,
    cacheSize: cache_size,
    enableWebsocket: enable_websocket
  }.compact
  nil
end

#start_listen(interval, is_blocking: false, ignore_error: false) ⇒ void

This method returns an undefined value.

Start to listen for events

Parameters:

  • interval (Integer, Float)

    the interval to fetch events in seconds.

  • is_blocking (Boolean) (defaults to: false)

    if the listen thread should block the current thread

  • ignore_error (Boolean) (defaults to: false)

    if errors should generate error events (see RubiraiErrorEvent)

Raises:



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rubirai/listener.rb', line 14

def start_listen(interval, is_blocking: false, ignore_error: false)
  raise RubiraiError, 'listener is already running' if @listener&.running?
  @listener_stop_event = Concurrent::Event.new if is_blocking
  bot = self
  @listener = Concurrent::TimerTask.new(execution_interval: interval) do
    loop do
      events = fetch_message
      events.each do |e|
        @listener_funcs.each { |f| f.call e }
      rescue RuntimeError => e
        @listener_funcs.each { |f| f.call RubiraiErrorEvent.new(e, bot) unless ignore_error }
      end
      break if events.length < 10
    rescue RuntimeError => e
      @listener_funcs.each { |f| f.call RubiraiErrorEvent.new(e, bot) unless ignore_error }
      break
    end
  end
  @listener.execute
  @listener_stop_event.wait if is_blocking
end

#stop_listenvoid

This method returns an undefined value.

Stop listening to events. Will unblock the thread if is_blocking is true when calling #start_listen



54
55
56
57
58
# File 'lib/rubirai/listener.rb', line 54

def stop_listen
  @listener.shutdown
  @listener_stop_event&.set
  @listener_stop_event = nil
end

#unmute(group_id, member_id) ⇒ void

This method returns an undefined value.

Unmute a group member

Parameters:

  • group_id (Integer)

    group id

  • member_id (Integer)

    member id



27
28
29
30
31
32
33
34
# File 'lib/rubirai/management.rb', line 27

def unmute(group_id, member_id)
  call :post, '/unmute', json: {
    sessionKey: @session,
    target: group_id,
    memberId: member_id
  }
  nil
end

#unmute_all(group_id) ⇒ void

This method returns an undefined value.

Unmute all in a group

Parameters:

  • group_id (Integer)

    group id



81
82
83
84
85
86
87
# File 'lib/rubirai/management.rb', line 81

def unmute_all(group_id)
  call :post, '/unmuteAll', json: {
    sessionKey: @session,
    target: group_id
  }
  nil
end

#upload_file_and_send(path_or_io, target, group_path) ⇒ String

Uploads a file to a group (currently only groups supported)

Parameters:

  • path_or_io (String, Pathname, IO)

    path to file

  • target (Integer)

    group id

  • group_path (String)

    path to file in group files

Returns:

  • (String)

    the string id of the mirai file



33
34
35
36
37
38
39
40
41
42
# File 'lib/rubirai/multipart.rb', line 33

def upload_file_and_send(path_or_io, target, group_path)
  res = call :post, '/uploadFileAndSend', form: {
    sessionKey: @session,
    type: 'Group',
    target: target,
    path: group_path,
    file: HTTP::FormData::File.new(path_or_io)
  }, headers: { content_type: 'multipart/form-data' }
  res['id']
end

#upload_image(path_or_io, type = :friend) ⇒ Hash

Uploads an image to QQ server

Returns:

  • (Hash)

    hash string keys are: { imageId, url, path }



7
8
9
10
11
12
13
14
# File 'lib/rubirai/multipart.rb', line 7

def upload_image(path_or_io, type = :friend)
  self.class.ensure_type_in type, 'friend', 'group', 'temp'
  call :post, '/uploadImage', form: {
    sessionKey: @session,
    type: type.to_s.downcase,
    img: HTTP::FormData::File.new(path_or_io)
  }, headers: { content_type: 'multipart/form-data' }
end

#upload_voice(path_or_io) ⇒ Hash

Uploads a voice file to QQ server Only group uploads available currently.

Parameters:

  • path_or_io (String, Pathname, IO)

    path to voice file

Returns:

  • (Hash)

    hash string keys are: { voiceId, url, path }



20
21
22
23
24
25
26
# File 'lib/rubirai/multipart.rb', line 20

def upload_voice(path_or_io)
  call :post, '/uploadVoice', form: {
    sessionKey: @session,
    type: 'group',
    img: HTTP::FormData::File.new(path_or_io)
  }, headers: { content_type: 'multipart/form-data' }
end

#verify(verify_key) ⇒ String

Start verification. Will store the session.

Parameters:

  • verify_key (String)

    the auth key defined in config file

Returns:

  • (String)

    the session key which will also be stored in the bot



8
9
10
11
# File 'lib/rubirai/auth.rb', line 8

def verify(verify_key)
  v = call :post, '/verify', json: { "verifyKey": verify_key }
  @session = v['session']
end