Class: Rubirai::Bot
- Inherits:
-
Object
- Object
- Rubirai::Bot
- 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
-
#base_uri ⇒ String
readonly
The base uri of mirai-api-http which the bot will send messages to.
-
#qq ⇒ String, Integer
(also: #id)
readonly
The qq of the bot.
-
#session ⇒ String
readonly
The session key.
Instance Method Summary collapse
-
#about ⇒ Hash{String => Object}
Get Mirai API plugin information.
-
#add_listener(&listener_block) ⇒ void
Add a listener.
-
#bind(qq, session = nil) ⇒ void
Verify and start a session.
-
#clear_listener ⇒ void
Clear all listeners.
-
#count_cached_message ⇒ Integer
Get the number of cached messages in mirai-http-api.
-
#fetch_latest_message(count = 10) ⇒ Array<Event>
(also: #fetch_latest_messages, #fetch_latest_event, #fetch_latest_events)
Fetch
count
number of latest events. -
#fetch_message(count = 10) ⇒ Array<Event>
(also: #fetch_messages, #fetch_event, #fetch_events)
Fetch
count
number of oldest events. -
#friend_list ⇒ Array<User>
Get friend list of the bot.
-
#get_group_config(group_id) ⇒ GroupConfig
Get group config.
-
#get_group_file_info(group_id, file_or_id) ⇒ GroupFile
Get the info about a group file.
-
#get_group_file_list(group_id, dir = nil) ⇒ Array<GroupFileSimple>
Get the group files as a list.
-
#get_member_info(group_id, member_id) ⇒ MemberInfo
Get member info.
-
#get_session_cfg ⇒ Hash{String => Object}
Get the config related to this session.
- #group_file_delete(group_id, file_id) ⇒ Object
- #group_file_mv(group_id, file_id, path) ⇒ Object
-
#group_list ⇒ Array<Group>
Get group list of the bot.
- #group_mkdir(group_id, dir) ⇒ Object
- #group_set_essence(msg_id) ⇒ Object
-
#initialize(host, port = nil) ⇒ Bot
constructor
Initializes the bot.
-
#kick(group_id, member_id, msg = nil) ⇒ void
Kick a member from a group.
-
#login(qq, verify_key, single_mode: false) ⇒ void
(also: #connect)
Log you in.
-
#logout ⇒ void
(also: #disconnect)
Log you out.
-
#member_list(group_id) ⇒ Array<GroupUser>
Get member list of a group.
-
#message_from_id(msg_id) ⇒ Event
Get a message event from message id.
-
#mute(group_id, member_id, time = 0) ⇒ Object
Mute a group member.
-
#mute_all(group_id) ⇒ void
Mute all in a group.
-
#peek_latest_message(count = 10) ⇒ Array<Event>
(also: #peek_latest_messages, #peek_latest_event, #peek_latest_events)
Peek
count
number of latest events. -
#peek_message(count = 10) ⇒ Array<Event>
(also: #peek_messages, #peek_event, #peek_events)
Peek
count
number of oldest events. -
#quit(group_id) ⇒ void
Quit a group.
-
#recall(msg_id) ⇒ void
Recall a message.
-
#release(qq = nil, session = nil) ⇒ void
Release a session.
- #rename_group_file(group_id, file_id, new_name) ⇒ Object
-
#respond_to_group_invite(event_id, from_id, group_id, operation, message = '') ⇒ void
Respond to group invitations (raw).
-
#respond_to_member_join(event_id, from_id, group_id, operation, message = '') ⇒ void
Respond to join group request (raw).
-
#respond_to_new_friend_request(event_id, from_id, operation, group_id = 0, message = '') ⇒ void
Respond to new friend request (raw).
-
#send_friend_msg(target_qq, *msgs, quote: nil) ⇒ Integer
Send friend message.
-
#send_group_msg(target_group_id, *msgs, quote: nil) ⇒ Integer
Send group message.
-
#send_image_msg(urls, **kwargs) ⇒ Array<String>
Send image messages.
-
#send_msg(type, target_id, *msgs, quote: nil) ⇒ Integer
Send friend or group message.
-
#send_nudge(target_id, subject_id, kind) ⇒ void
Send nudge.
-
#send_temp_msg(target_qq, group_id, *msgs, quote: nil) ⇒ Integer
Send temp message.
-
#set_group_config(group_id, config) ⇒ void
Set group config.
-
#set_member_info(group_id, member_id, info) ⇒ void
Set member info.
-
#set_session_cfg(cache_size: nil, enable_websocket: nil) ⇒ void
Set the config related to this session.
-
#start_listen(interval, is_blocking: false, ignore_error: false) ⇒ void
Start to listen for events.
-
#stop_listen ⇒ void
Stop listening to events.
-
#unmute(group_id, member_id) ⇒ void
Unmute a group member.
-
#unmute_all(group_id) ⇒ void
Unmute all in a group.
-
#upload_file_and_send(path_or_io, target, group_path) ⇒ String
Uploads a file to a group (currently only groups supported).
-
#upload_image(path_or_io, type = :friend) ⇒ Hash
Uploads an image to QQ server.
-
#upload_voice(path_or_io) ⇒ Hash
Uploads a voice file to QQ server Only group uploads available currently.
-
#verify(verify_key) ⇒ String
Start verification.
Constructor Details
#initialize(host, port = nil) ⇒ Bot
Initializes the bot
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_uri ⇒ String (readonly)
Returns 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 |
#qq ⇒ String, Integer (readonly) Also known as: id
Returns 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 |
#session ⇒ String (readonly)
Returns 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
#about ⇒ Hash{String => Object}
Get Mirai API plugin information.
The information is like
{
'version' => '1.0.0'
}
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.
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_listener ⇒ void
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_message ⇒ Integer
Get the number of cached messages in mirai-http-api
64 65 66 67 68 69 |
# File 'lib/rubirai/event_recv.rb', line 64 def 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.
21 22 23 |
# File 'lib/rubirai/event_recv.rb', line 21 def (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.
10 11 12 |
# File 'lib/rubirai/event_recv.rb', line 10 def (count = 10) get_events '/fetchMessage', count end |
#friend_list ⇒ Array<User>
Get friend list of the bot
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
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
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
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
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_cfg ⇒ Hash{String => Object}
Get the config related to this session
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_list ⇒ Array<Group>
Get group list of the bot
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
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.
51 52 53 54 55 |
# File 'lib/rubirai/auth.rb', line 51 def login(qq, verify_key, single_mode: false) verify verify_key single_mode or bind(qq) nil end |
#logout ⇒ void Also known as: disconnect
This method returns an undefined value.
Log you out.
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
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
54 55 56 57 58 59 60 |
# File 'lib/rubirai/event_recv.rb', line 54 def (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
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
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)
43 44 45 |
# File 'lib/rubirai/event_recv.rb', line 43 def (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)
32 33 34 |
# File 'lib/rubirai/event_recv.rb', line 32 def (count = 10) get_events '/peekMessage', count end |
#quit(group_id) ⇒ void
This method returns an undefined value.
Quit a group
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
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.
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)
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, = '') call :post, '/resp/botInvitedJoinGroupRequestEvent', json: { sessionKey: @session, eventId: event_id, fromId: from_id, groupId: group_id, operate: operation, 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)
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, = '') call :post, '/resp/memberJoinRequestEvent', json: { sessionKey: @session, eventId: event_id, fromId: from_id, groupId: group_id, operate: operation, 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)
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, = '') call :post, '/resp/newFriendRequestEvent', json: { sessionKey: @session, eventId: event_id, fromId: from_id, groupId: group_id, operate: operation, message: } nil end |
#send_friend_msg(target_qq, *msgs, quote: nil) ⇒ Integer
Send friend message
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
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
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
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
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
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
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
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
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
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 = 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_listen ⇒ void
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
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
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)
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
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.
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.
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 |