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 ⇒ Object
readonly
Returns the value of attribute qq.
-
#session ⇒ String
readonly
The session key.
Class Method Summary collapse
Instance Method Summary collapse
-
#about ⇒ Hash{String => Object}
Get Mirai API plugin information such as.
- #add_listener(&listener_block) ⇒ Object
-
#auth(auth_key) ⇒ String
Start authentication.
- #clear_listener ⇒ Object
-
#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
countnumber of latest events. -
#fetch_message(count = 10) ⇒ Array<Event>
(also: #fetch_messages, #fetch_event, #fetch_events)
Fetch
countnumber of oldest events. -
#friend_list ⇒ Array<User>
Get friend list of the bot.
- #gen_uri(path) ⇒ Object
- #get_group_config(group_id) ⇒ Object
-
#get_group_file_info(group_id, file_id) ⇒ Object
Get the info about a group file.
- #get_group_file_list(group_id, dir = nil) ⇒ Object
- #get_member_info(group_id, member_id) ⇒ Object
-
#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, auth_key) ⇒ 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 group.
-
#peek_latest_message(count = 10) ⇒ Array<Event>
(also: #peek_latest_messages, #peek_latest_event, #peek_latest_events)
Peek
countnumber of latest events. -
#peek_message(count = 10) ⇒ Array<Event>
(also: #peek_messages, #peek_event, #peek_events)
Peek
countnumber of oldest events. -
#quit(group_id) ⇒ void
Quit a group.
- #recall(msg_id) ⇒ Object
-
#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 = '') ⇒ Object
-
#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) ⇒ Object
- #send_group_msg(target_group_id, *msgs) ⇒ Object
-
#send_image_msg(urls, **kwargs) ⇒ Array<String>
Send image messages.
- #send_msg(type, target_id, *msgs) ⇒ Object
- #send_nudge(target_id, subject_id, kind) ⇒ Object
-
#send_temp_msg(target_qq, group_id, *msgs) ⇒ Integer
Send temp message.
-
#set_group_config(group_id, config) ⇒ void
Set group config.
- #set_member_info(group_id, member_id, info) ⇒ Object
-
#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) ⇒ Object
- #stop_listen ⇒ Object
-
#unmute(group_id, member_id) ⇒ void
Unmute a group member.
- #unmute_all(group_id) ⇒ Object
-
#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(qq, session = nil) ⇒ void
Verify and start a session.
Constructor Details
#initialize(host, port = nil) ⇒ Bot
Initializes the bot
24 25 26 27 |
# File 'lib/rubirai.rb', line 24 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.
18 19 20 |
# File 'lib/rubirai.rb', line 18 def base_uri @base_uri end |
#qq ⇒ Object (readonly)
Returns the value of attribute qq.
18 |
# File 'lib/rubirai.rb', line 18 attr_reader :base_uri, :session, :qq |
#session ⇒ String (readonly)
Returns the session key.
18 |
# File 'lib/rubirai.rb', line 18 attr_reader :base_uri, :session, :qq |
Class Method Details
.ensure_type_in(type, *types) ⇒ Object
33 34 35 36 |
# File 'lib/rubirai.rb', line 33 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 |
Instance Method Details
#about ⇒ Hash{String => Object}
Get Mirai API plugin information such as
{
'version' => '1.0.0'
}
14 15 16 17 |
# File 'lib/rubirai/plugin_info.rb', line 14 def about v = call :get, '/about' v['data'] end |
#add_listener(&listener_block) ⇒ Object
30 31 32 |
# File 'lib/rubirai/listener.rb', line 30 def add_listener(&listener_block) @listener_funcs << listener_block end |
#auth(auth_key) ⇒ String
Start authentication. Will store the session.
8 9 10 11 |
# File 'lib/rubirai/auth.rb', line 8 def auth(auth_key) v = call :post, '/auth', json: { "authKey": auth_key } @session = v['session'] end |
#clear_listener ⇒ Object
34 35 36 |
# File 'lib/rubirai/listener.rb', line 34 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.map { |friend| User.new(friend, self) } end |
#gen_uri(path) ⇒ Object
29 30 31 |
# File 'lib/rubirai.rb', line 29 def gen_uri(path) URI.join(base_uri, path) end |
#get_group_config(group_id) ⇒ Object
85 86 87 88 89 90 91 |
# File 'lib/rubirai/management.rb', line 85 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_id) ⇒ Object
Get the info about a group file
145 146 147 148 149 150 151 152 |
# File 'lib/rubirai/management.rb', line 145 def get_group_file_info(group_id, file_id) resp = call :get, '/groupFileInfo', params: { sessionKey: @session, target: group_id, id: file_id } GroupFile.new resp, self end |
#get_group_file_list(group_id, dir = nil) ⇒ Object
132 133 134 135 136 137 138 139 140 |
# File 'lib/rubirai/management.rb', line 132 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) ⇒ Object
110 111 112 113 114 115 116 117 |
# File 'lib/rubirai/management.rb', line 110 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
183 184 185 186 187 188 189 190 |
# File 'lib/rubirai/management.rb', line 183 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
173 174 175 176 177 178 179 180 181 |
# File 'lib/rubirai/management.rb', line 173 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.map { |group| Group.new(group, self) } end |
#group_mkdir(group_id, dir) ⇒ Object
164 165 166 167 168 169 170 171 |
# File 'lib/rubirai/management.rb', line 164 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
192 193 194 195 196 197 198 |
# File 'lib/rubirai/management.rb', line 192 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, auth_key) ⇒ void Also known as: connect
This method returns an undefined value.
Log you in.
49 50 51 52 |
# File 'lib/rubirai/auth.rb', line 49 def login(qq, auth_key) auth auth_key verify qq end |
#logout ⇒ void Also known as: disconnect
This method returns an undefined value.
Log you out.
60 61 62 |
# File 'lib/rubirai/auth.rb', line 60 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.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 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) ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/rubirai/message.rb', line 44 def recall(msg_id) 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
154 155 156 157 158 159 160 161 162 |
# File 'lib/rubirai/management.rb', line 154 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 = '') ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/rubirai/event_resp.rb', line 77 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)
65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/rubirai/event_resp.rb', line 65 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)
45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/rubirai/event_resp.rb', line 45 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) ⇒ Object
36 37 38 |
# File 'lib/rubirai/message.rb', line 36 def send_friend_msg(target_qq, *msgs) send_msg :friend, target_qq, *msgs end |
#send_group_msg(target_group_id, *msgs) ⇒ Object
40 41 42 |
# File 'lib/rubirai/message.rb', line 40 def send_group_msg(target_group_id, *msgs) send_msg :group, target_group_id, *msgs end |
#send_image_msg(urls, **kwargs) ⇒ Array<String>
Send image messages
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/rubirai/message.rb', line 57 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) ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/rubirai/message.rb', line 25 def send_msg(type, target_id, *msgs) 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, messageChain: chain.to_a } resp['messageId'] end |
#send_nudge(target_id, subject_id, kind) ⇒ Object
73 74 75 76 77 78 79 80 81 82 |
# File 'lib/rubirai/message.rb', line 73 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) ⇒ Integer
Send temp message
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/rubirai/message.rb', line 14 def send_temp_msg(target_qq, group_id, *msgs) chain = Rubirai::MessageChain.make(*msgs, bot: self) resp = call :post, '/sendTempMessage', json: { sessionKey: @session, qq: target_qq.to_i, group: group_id.to_i, messageChain: chain.to_a } resp['messageId'] end |
#set_group_config(group_id, config) ⇒ void
This method returns an undefined value.
Set group config
98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/rubirai/management.rb', line 98 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) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/rubirai/management.rb', line 119 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) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/rubirai/listener.rb', line 8 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 ⇒ Object
38 39 40 41 42 |
# File 'lib/rubirai/listener.rb', line 38 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) ⇒ Object
77 78 79 80 81 82 83 |
# File 'lib/rubirai/management.rb', line 77 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(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 verify(qq, session = nil) check qq, session call :post, '/verify', json: { "sessionKey": @session || session, "qq": qq.to_i } @session = session if session @qq = qq nil end |