class CQHttp::Api

OneBot标准API

OneBot文档

(github.com/howmanybots/onebot)

Example:

CQHttp::Api.getImage file

Attributes

apiUrl[RW]

@return [URI] HTTP API链接

Public Class Methods

acceptFriendRequest(flag, reason=nil, url=@apiUrl) click to toggle source

接受好友邀请

@param flag [String] @param reason [String] @param url [URI] @return [Boolean]

# 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) click to toggle source

接受加群请求

@param flag [String] @param sub_type [String] @param url [URI] @return [Boolean]

# 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
getImage(file, url=@apiUrl) click to toggle source

下载图片(未完成)

@param file [String] @param url [URI] @return [Hash]

# 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
get_msg(message_id, url=@apiUrl) click to toggle source

获取消息

@param message_id [Number] @param url [URI] @return [Hash]

# 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
refuseFriendRequest(flag, url=@apiUrl) click to toggle source

拒绝好友邀请

@param flag [String] @param url [URI] @return [Boolean]

# 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) click to toggle source

拒绝加群请求

@param flag [String] @param sub_type [String] @param reason [String] @param url [URI] @return [Boolean]

# 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) click to toggle source

发送群聊消息

@param msg [String] @param group_id [Number] @param url [URI] @return [Hash]

# 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) click to toggle source

发送私聊消息

@param msg [String] @param user_id [Number] @param url [URI] @return [Hash]

# 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) click to toggle source

设置群名

@param group_id [Number] @param group_name [String] @param url [URI] @return [Hash]

# 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) click to toggle source

设置API地址

@param apiIp [String] @param apiPort [Number] @return [URI]

# 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