Class: CQHttp::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/Bot/Api.rb

Overview

OneBot标准API

OneBot文档

(github.com/howmanybots/onebot)

Example:

CQHttp::Api.getImage file

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#apiUrlURI

Returns HTTP API链接.

Returns:

  • (URI)

    HTTP API链接


9
10
11
# File 'lib/Bot/Api.rb', line 9

def apiUrl
  @apiUrl
end

Class Method Details

.acceptFriendRequest(flag, reason = nil, url = @apiUrl) ⇒ Boolean

接受好友邀请

Parameters:

  • flag (String)
  • reason (String) (defaults to: nil)
  • url (URI) (defaults to: @apiUrl)

Returns:

  • (Boolean)

117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/Bot/Api.rb', line 117

def acceptFriendRequest(flag, reason=nil, url=@apiUrl)
  url.path = "/set_friend_add_request"
  ret = { flag: flag, approve: true, remark: reason }.to_json
  data = JSON.parse(Utils.httpPost(url, ret))
  if data['status'] == 'ok'
    Utils.log '已通过好友请求'
    true
  else
    Utils.log '请求通过失败', Logger::WARN
    false
  end
end

.acceptGroupRequest(flag, sub_type, url = @apiUrl) ⇒ Boolean

接受加群请求

Parameters:

  • flag (String)
  • sub_type (String)
  • url (URI) (defaults to: @apiUrl)

Returns:

  • (Boolean)

154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/Bot/Api.rb', line 154

def acceptGroupRequest(flag, sub_type, url=@apiUrl)
  url.path = "/set_group_add_request"
  ret = { flag: flag, sub_type: sub_type, approve: true }.to_json
  data = JSON.parse(Utils.httpPost(url, ret))
  if data['status'] == 'ok'
    Utils.log '已通过加群请求'
    true
  else
    Utils.log '请求通过失败', Logger::WARN
    false
  end
end

.get_msg(message_id, url = @apiUrl) ⇒ Hash

获取消息

Parameters:

  • message_id (Number)
  • url (URI) (defaults to: @apiUrl)

Returns:

  • (Hash)

61
62
63
64
65
66
67
68
69
70
71
# File 'lib/Bot/Api.rb', line 61

def get_msg(message_id, url=@apiUrl)
  url.path = "/get_msg"
  ret = { message_id: message_id }.to_json
  data = JSON.parse(Utils.httpPost(url, ret))
  if data['status'] == 'ok'
    Utils.log '消息获取成功'
  else
    Utils.log '消息获取失败', Logger::WARN
  end
  return data['data']
end

.getImage(file, url = @apiUrl) ⇒ Hash

下载图片(未完成)

Parameters:

  • file (String)
  • url (URI) (defaults to: @apiUrl)

Returns:

  • (Hash)

44
45
46
47
48
49
50
51
52
53
54
# File 'lib/Bot/Api.rb', line 44

def getImage(file, url=@apiUrl)
  url.path = "/get_image"
  ret = { file: file }.to_json
  data = JSON.parse(Utils.httpPost(url, ret))
  if data['status'] == 'ok'
    Utils.log '下载图片成功'
  else
    Utils.log '下载图片失败', Logger::WARN
  end
  return data['data']
end

.refuseFriendRequest(flag, url = @apiUrl) ⇒ Boolean

拒绝好友邀请

Parameters:

  • flag (String)
  • url (URI) (defaults to: @apiUrl)

Returns:

  • (Boolean)

135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/Bot/Api.rb', line 135

def refuseFriendRequest(flag, url=@apiUrl)
  url.path = "/set_friend_add_request"
  ret = { flag: flag, approve: false }.to_json
  user_id = JSON.parse(Utils.httpPost(url, ret))
  if data['status'] == 'ok'
    Utils.log '已拒绝好友请求'
    true
  else
    Utils.log '请求拒绝失败', Logger::WARN
    false
  end
end

.refuseGroupRequest(flag, sub_type, reason = nil, url = @apiUrl) ⇒ Boolean

拒绝加群请求

Parameters:

  • flag (String)
  • sub_type (String)
  • reason (String) (defaults to: nil)
  • url (URI) (defaults to: @apiUrl)

Returns:

  • (Boolean)

174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/Bot/Api.rb', line 174

def refuseGroupRequest(flag, sub_type, reason=nil, url=@apiUrl)
  url.path = "/set_group_add_request"
  ret = { flag: flag, sub_type: sub_type, approve: false, reason: reason }.to_json
  data = JSON.parse(Utils.httpPost(url, ret))
  if data['status'] == 'ok'
    Utils.log '已拒绝加群请求'
    true
  else
    Utils.log '请求拒绝失败', Logger::WARN
    false
  end
end

.sendGroupMessage(msg, group_id, url = @apiUrl) ⇒ Hash

发送群聊消息

Parameters:

  • msg (String)
  • group_id (Number)
  • url (URI) (defaults to: @apiUrl)

Returns:

  • (Hash)

98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/Bot/Api.rb', line 98

def sendGroupMessage(msg, group_id, url=@apiUrl)
  url.path = "/send_group_msg"
  ret = { group_id: group_id, message: msg }.to_json
  data = JSON.parse(Utils.httpPost(url, ret))
  if data['status'] == 'ok'
    message_id = data['data']['message_id']
    Utils.log "发送至群 #{group_id} 的消息: #{msg} (#{message_id})"
  else
    Utils.log '发送消息失败', Logger::WARN
  end
  return data['data']
end

.sendPrivateMessage(msg, user_id, url = @apiUrl) ⇒ Hash

发送私聊消息

Parameters:

  • msg (String)
  • user_id (Number)
  • url (URI) (defaults to: @apiUrl)

Returns:

  • (Hash)

79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/Bot/Api.rb', line 79

def sendPrivateMessage(msg, user_id, url=@apiUrl)
  url.path = "/send_private_msg"
  ret = { user_id: user_id, message: msg }.to_json
  data = JSON.parse(Utils.httpPost(url, ret))
  if data['status'] == 'ok'
    message_id = data['data']['message_id']
    Utils.log "发送至私聊 #{user_id} 的消息: #{msg} (#{message_id})"
  else
    Utils.log '发送消息失败', Logger::WARN
  end
  return data['data']
end

.setGroupName(group_id, group_name, url = @apiUrl) ⇒ Hash

设置群名

Parameters:

  • group_id (Number)
  • group_name (String)
  • url (URI) (defaults to: @apiUrl)

Returns:

  • (Hash)

27
28
29
30
31
32
33
34
35
36
37
# File 'lib/Bot/Api.rb', line 27

def setGroupName(group_id, group_name, url=@apiUrl)
  url.path = "/set_group_name"
  ret = { group_id: group_id.to_i, group_name: group_name }.to_json
  data = JSON.parse(Utils.httpPost(url, ret))
  if data['status'] == 'ok'
    Utils.log '设置群头像成功'
  else
    Utils.log '设置群头像失败', Logger::WARN
  end
  return data['data']
end

.setUrl(apiIp: '127.0.0.1', apiPort: 5700) ⇒ URI

设置API地址

Parameters:

  • apiIp (String) (defaults to: '127.0.0.1')
  • apiPort (Number) (defaults to: 5700)

Returns:

  • (URI)

17
18
19
# File 'lib/Bot/Api.rb', line 17

def setUrl(apiIp:'127.0.0.1', apiPort:5700)
  @apiUrl = URI::HTTP.build(host: apiIp, port: apiPort)
end