Class: Onebot::Http::API
- Inherits:
-
Object
- Object
- Onebot::Http::API
- Defined in:
- lib/Core/Http/API.rb
Instance Attribute Summary collapse
-
#url ⇒ URI
HTTP API链接.
Instance Method Summary collapse
-
#acceptFriendRequest(flag, reason = nil) ⇒ Boolean
接受好友邀请.
-
#acceptGroupRequest(flag, sub_type) ⇒ Boolean
接受加群请求.
-
#get_msg(message_id) ⇒ Hash
获取消息.
-
#getImage(file) ⇒ Hash
获取图片信息.
-
#initialize(apiIp: '127.0.0.1', apiPort: 5700) ⇒ URI
constructor
(初始化) 设置API地址.
-
#refuseFriendRequest(flag) ⇒ Boolean
拒绝好友邀请.
-
#refuseGroupRequest(flag, sub_type, reason = nil) ⇒ Boolean
拒绝加群请求.
-
#sendGroupMessage(msg, group_id) ⇒ Hash
发送群聊消息.
-
#sendPrivateMessage(msg, user_id) ⇒ Hash
发送私聊消息.
-
#setGroupName(group_id, group_name) ⇒ Hash
设置群名.
- #setLogger(logger) ⇒ Object
Constructor Details
#initialize(apiIp: '127.0.0.1', apiPort: 5700) ⇒ URI
(初始化) 设置API地址
17 18 19 20 |
# File 'lib/Core/Http/API.rb', line 17 def initialize(apiIp: '127.0.0.1', apiPort: 5700) @url = URI::HTTP.build(host: apiIp, port: apiPort) @logger = Logging::EventLogger.new end |
Instance Attribute Details
#url ⇒ URI
Returns HTTP API链接.
10 11 12 |
# File 'lib/Core/Http/API.rb', line 10 def url @url end |
Instance Method Details
#acceptFriendRequest(flag, reason = nil) ⇒ Boolean
接受好友邀请
113 114 115 116 117 118 119 120 121 122 |
# File 'lib/Core/Http/API.rb', line 113 def acceptFriendRequest(flag, reason = nil) data = send('set_friend_add_request', { flag:, approve: true, remark: reason }) if data['status'] == 'ok' @logger.log '已通过好友请求' true else @logger.log '请求通过失败', Logger::WARN false end end |
#acceptGroupRequest(flag, sub_type) ⇒ Boolean
接受加群请求
146 147 148 149 150 151 152 153 154 155 |
# File 'lib/Core/Http/API.rb', line 146 def acceptGroupRequest(flag, sub_type) data = send('set_group_add_request', { flag:, sub_type:, approve: true }) if data['status'] == 'ok' @logger.log '已通过加群请求' true else @logger.log '请求通过失败', Logger::WARN false end end |
#get_msg(message_id) ⇒ Hash
获取消息
63 64 65 66 67 68 69 70 71 |
# File 'lib/Core/Http/API.rb', line 63 def get_msg() data = send('get_msg', { message_id: }) if data['status'] == 'ok' @logger.log '消息获取成功' else @logger.log '消息获取失败', Logger::WARN end data['data'] end |
#getImage(file) ⇒ Hash
获取图片信息
48 49 50 51 52 53 54 55 56 |
# File 'lib/Core/Http/API.rb', line 48 def getImage(file) data = send('get_image', { file: }) if data['status'] == 'ok' @logger.log '下载图片成功' else @logger.log '下载图片失败', Logger::WARN end data['data'] end |
#refuseFriendRequest(flag) ⇒ Boolean
拒绝好友邀请
129 130 131 132 133 134 135 136 137 138 |
# File 'lib/Core/Http/API.rb', line 129 def refuseFriendRequest(flag) data = send('set_friend_add_request', { flag:, approve: false }) if data['status'] == 'ok' @logger.log '已拒绝好友请求' true else @logger.log '请求拒绝失败', Logger::WARN false end end |
#refuseGroupRequest(flag, sub_type, reason = nil) ⇒ Boolean
拒绝加群请求
164 165 166 167 168 169 170 171 172 173 |
# File 'lib/Core/Http/API.rb', line 164 def refuseGroupRequest(flag, sub_type, reason = nil) data = send('set_group_add_request', { flag:, sub_type:, approve: false, reason: }) if data['status'] == 'ok' @logger.log '已拒绝加群请求' true else @logger.log '请求拒绝失败', Logger::WARN false end end |
#sendGroupMessage(msg, group_id) ⇒ Hash
发送群聊消息
96 97 98 99 100 101 102 103 104 105 |
# File 'lib/Core/Http/API.rb', line 96 def sendGroupMessage(msg, group_id) data = send('send_group_msg', { group_id:, message: msg }) if data['status'] == 'ok' = data['data']['message_id'] @logger.log "发送至群 #{group_id} 的消息: #{msg} (#{})" else @logger.log '发送消息失败', Logger::WARN end data['data'] end |
#sendPrivateMessage(msg, user_id) ⇒ Hash
发送私聊消息
79 80 81 82 83 84 85 86 87 88 |
# File 'lib/Core/Http/API.rb', line 79 def sendPrivateMessage(msg, user_id) data = send('send_private_msg', { user_id:, message: msg }) if data['status'] == 'ok' = data['data']['message_id'] @logger.log "发送至私聊 #{user_id} 的消息: #{msg} (#{})" else @logger.log '发送消息失败', Logger::WARN end data['data'] end |
#setGroupName(group_id, group_name) ⇒ Hash
设置群名
33 34 35 36 37 38 39 40 41 |
# File 'lib/Core/Http/API.rb', line 33 def setGroupName(group_id, group_name) data = send('set_group_name', { group_id: group_id.to_i, group_name: }) if data['status'] == 'ok' @logger.log '设置群头像成功' else @logger.log '设置群头像失败', Logger::WARN end data['data'] end |
#setLogger(logger) ⇒ Object
22 23 24 25 |
# File 'lib/Core/Http/API.rb', line 22 def setLogger(logger) @logger = Logging::EventLogger.new(logger) self end |