ruby-cqhttp 0.0.6 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/lib/Bot/Api.rb DELETED
@@ -1,115 +0,0 @@
1
- module CQHttp
2
- class Api
3
- attr_accessor :apiUrl
4
- class << self
5
- def setUrl(apiIp:'127.0.0.1', apiPort:5700)
6
- @apiUrl = URI::HTTP.build(host: apiIp, port: apiPort)
7
- end
8
- def setGroupName(group_id, group_name, url=@apiUrl)
9
- url.path = "/set_group_name"
10
- ret = { group_id: group_id.to_i, group_name: group_name }.to_json
11
- data = JSON.parse(Utils.httpPost(url, ret))
12
- if data['status'] == 'ok'
13
- Utils.log '设置群头像成功'
14
- else
15
- Utils.log '设置群头像失败', Logger::WARN
16
- end
17
- end
18
-
19
- def getImage(file, url=@apiUrl) # UNFINSHED
20
- url.path = "/get_image"
21
- ret = { file: file }.to_json
22
- data = JSON.parse(Utils.httpPost(url, ret))
23
- if data['status'] == 'ok'
24
- Utils.log '下载图片成功'
25
- return data['data']
26
- else
27
- Utils.log '下载图片失败', Logger::WARN
28
- end
29
- end
30
-
31
- def get_msg(message_id, url=@apiUrl) # UNFINSHED
32
- url.path = "/get_msg"
33
- ret = { message_id: message_id }.to_json
34
- data = JSON.parse(Utils.httpPost(url, ret))
35
- if data['status'] == 'ok'
36
- Utils.log '消息获取成功'
37
- return data['data']
38
- else
39
- Utils.log '消息获取失败', Logger::WARN
40
- end
41
-
42
- end
43
-
44
- def sendPrivateMessage(msg, user_id, url=@apiUrl)
45
- url.path = "/send_private_msg"
46
- ret = { user_id: user_id, message: msg }.to_json
47
- data = JSON.parse(Utils.httpPost(url, ret))
48
- if data['status'] == 'ok'
49
- message_id = data['data']['message_id']
50
- Utils.log "发送至私聊 #{user_id} 的消息: #{msg} (#{message_id})"
51
- return message_id
52
- else
53
- Utils.log '发送消息失败', Logger::WARN
54
- end
55
- end
56
-
57
- def sendGroupMessage(msg, group_id, url=@apiUrl)
58
- url.path = "/send_group_msg"
59
- ret = { group_id: group_id, message: msg }.to_json
60
- data = JSON.parse(Utils.httpPost(url, ret))
61
- if data['status'] == 'ok'
62
- message_id = data['data']['message_id']
63
- Utils.log "发送至群 #{group_id} 的消息: #{msg} (#{message_id})"
64
- return message_id
65
- else
66
- Utils.log '发送消息失败', Logger::WARN
67
- end
68
- end
69
-
70
- def acceptFriendRequest(flag, url=@apiUrl)
71
- url.path = "/set_friend_add_request"
72
- ret = { flag: flag, approve: true }.to_json
73
- data = JSON.parse(Utils.httpPost(url, ret))
74
- if data['status'] == 'ok'
75
- Utils.log '已通过好友请求'
76
- else
77
- Utils.log '请求通过失败', Logger::WARN
78
- end
79
- end
80
-
81
- def refuseFriendRequest(flag, url=@apiUrl)
82
- url.path = "/set_friend_add_request"
83
- ret = { flag: flag, approve: false }.to_json
84
- user_id = JSON.parse(Utils.httpPost(url, ret))
85
- if data['status'] == 'ok'
86
- Utils.log '已拒绝好友请求'
87
- else
88
- Utils.log '请求拒绝失败', Logger::WARN
89
- end
90
- end
91
-
92
- def acceptGroupRequest(flag, sub_type, url=@apiUrl)
93
- url.path = "/set_group_add_request"
94
- ret = { flag: flag, sub_type: sub_type, approve: true }.to_json
95
- data = JSON.parse(Utils.httpPost(url, ret))
96
- if data['status'] == 'ok'
97
- Utils.log '已通过加群请求'
98
- else
99
- Utils.log '请求通过失败', Logger::WARN
100
- end
101
- end
102
-
103
- def refuseGroupRequest(flag, sub_type, url=@apiUrl)
104
- url.path = "/set_group_add_request"
105
- ret = { flag: flag, sub_type: sub_type, approve: false }.to_json
106
- data = JSON.parse(Utils.httpPost(url, ret))
107
- if data['status'] == 'ok'
108
- Utils.log '已拒绝加群请求'
109
- else
110
- Utils.log '请求拒绝失败', Logger::WARN
111
- end
112
- end
113
- end
114
- end
115
- end
data/lib/Bot/Bot.rb DELETED
@@ -1,137 +0,0 @@
1
- module CQHttp
2
- class Bot
3
- Sender = Struct.new(:age, :member_role, :card, :qqlevel, :nickname, :title, :sex)
4
- Target = Struct.new(:messagetype, :time, :group_id, :user_id, :message_id, :message)
5
- def self.connect(host:, port:)
6
- url = URI::WS.build(host: host, port: port)
7
- Api.setUrl()
8
- Utils.log '正在连接到 ' << url.to_s
9
- client = ::CQHttp::Bot::WebSocket.new(url)
10
- yield client if block_given?
11
- client.connect
12
- client
13
- end
14
-
15
- class WebSocket
16
- attr_accessor :url
17
- attr_accessor :selfID
18
-
19
- include EventEmitter
20
-
21
- def initialize(url)
22
- @url = url
23
- end
24
-
25
- def connect
26
- EM.run do
27
- @ws = Faye::WebSocket::Client.new(@url.to_s)
28
-
29
- @ws.on :message do |event|
30
- Thread.new { dataParse(event.data)}
31
- end
32
-
33
- @ws.on :close do |event|
34
- emit :close, event
35
- Utils.log '连接断开'
36
- @ws = nil
37
- exit
38
- end
39
-
40
- @ws.on :error do |event|
41
- emit :error, event
42
- @ws = nil
43
- end
44
- end
45
- end
46
-
47
-
48
- def sendPrivateMessage(msg, user_id)
49
- ret = { action: 'send_private_msg', params: { user_id: user_id, message: msg }, echo: 'BotPrivateMessage' }.to_json
50
- Utils.log "发送至私聊 #{user_id} 的消息: #{msg}"
51
- @ws.send ret
52
- end
53
-
54
- def sendGroupMessage(msg, group_id)
55
- ret = { action: 'send_group_msg', params: { group_id: group_id, message: msg }, echo: 'BotGroupMessage' }.to_json
56
- Utils.log "发送至群 #{group_id} 的消息: #{msg}"
57
- @ws.send ret
58
- end
59
-
60
- def sendMessage(msg, target)
61
- sendGroupMessage msg, target.group_id if target.messagetype == 'group'
62
- sendPrivateMessage msg, target.user_id if target.messagetype == 'private'
63
- end
64
-
65
- private
66
-
67
- def dataParse(data)
68
- msg = JSON.parse(data)
69
- sdr = Sender.new
70
- tar = Target.new
71
- tar.time = msg['time']
72
- if msg['meta_event_type'] == 'lifecycle' && msg['sub_type'] == 'connect'
73
- @selfID = msg['self_id']
74
- Utils.log "连接成功, BotQQ: #{@selfID}"
75
- emit :logged, @selfID
76
- end
77
- Utils.log msg, Logger::DEBUG if msg['meta_event_type'] != 'heartbeat' # 过滤心跳
78
- case msg['post_type']
79
- #
80
- # 请求事件
81
- #
82
- when 'request'
83
- case msg['request_type']
84
- when 'group'
85
- if msg['sub_type'] == 'invite' # 加群邀请
86
- Utils.log "收到用户 #{msg['user_id']} 的加群 #{msg['group_id']} 请求 (#{msg['flag']})"
87
- end
88
- when 'friend' # 加好友邀请
89
- Utils.log "收到用户 #{msg['user_id']} 的好友请求 (#{msg['flag']})"
90
- end
91
- emit :request, msg['request_type'], msg['sub_type'], msg['flag']
92
- #
93
- # 提醒事件
94
- #
95
- when 'notice'
96
- case msg['notice_type']
97
- when 'group_decrease' # 群数量减少
98
- if msg['sub_type'] == 'kick_me' # 被踢出
99
- Utils.log "被 #{msg['operator_id']} 踢出群 #{msg['group_id']}"
100
- end
101
- when 'group_recall'
102
- Utils.log "群 #{msg['group_id']} 中 #{msg['user_id']} 撤回了一条消息 (#{msg['message_id']})"
103
- when 'friend_recall'
104
- Utils.log "好友 #{msg['user_id']} 撤回了一条消息 (#{msg['message_id']})"
105
- end
106
- emit :notice, msg['notice_type'], msg
107
-
108
- #
109
- # 消息事件
110
- #
111
- when 'message'
112
- tar.user_id = msg['user_id'] # 用户id
113
- tar.message_id = msg['message_id'] # 消息id
114
- tar.message = msg['message'] # 消息内容
115
- sdr.age = msg['sender']['age'] # 年龄
116
- sdr.nickname = msg['sender']['nickname'] # 原有用户名
117
- sdr.sex = msg['sender']['sex'] # 性别
118
- tar.messagetype = msg['message_type'] # 消息类型
119
- # 下面仅群聊
120
- tar.group_id = msg['group_id'] # 群id
121
- sdr.card = msg['sender']['card'] # 群昵称
122
- sdr.title = msg['sender']['title'] # 头衔
123
- sdr.member_role = msg['sender']['role'] # 群成员地位
124
- sdr.qqlevel = msg['sender']['level'] # 群成员等级
125
- if tar.messagetype == 'group' # 判断是否为群聊
126
- Utils.log "收到群 #{tar.group_id} 内 #{sdr.nickname}(#{tar.user_id}) 的消息: #{tar.message} (#{tar.message_id})"
127
- emit :groupMessage, tar.message, sdr, tar
128
- else
129
- Utils.log "收到好友 #{sdr.nickname}(#{tar.user_id}) 的消息: #{tar.message} (#{tar.message_id})"
130
- emit :privateMessage, tar.message, sdr, tar
131
- end
132
- emit :message, tar.message, sdr, tar
133
- end
134
- end
135
- end
136
- end
137
- end
data/lib/Bot/Utils.rb DELETED
@@ -1,42 +0,0 @@
1
- module CQHttp
2
- class Utils
3
- attr_accessor :stdLogger, :fileLogger, :loggerFile
4
- class << self
5
- def setLogger(logger)
6
- logger.level = 'INFO'
7
- logger.formatter = proc do |severity, datetime, progname, msg|
8
- if progname == nil
9
- "[#{datetime.strftime('%Y-%m-%d %H:%M:%S')}][#{severity}]: #{msg.gsub(/\n/,'\n').gsub(/\r/,'\r') }\n"
10
- else
11
- "[#{datetime.strftime('%Y-%m-%d %H:%M:%S')}][#{progname}][#{severity}]: #{msg.gsub(/\n/,'\n').gsub(/\r/,'\r') }\n"
12
- end
13
- end
14
- logger
15
- end
16
- def initLogger(loggerFile=nil)
17
- @loggerFile = loggerFile
18
- @stdLogger = setLogger(Logger.new(STDOUT))
19
- @fileLogger = setLogger(Logger.new(@loggerFile, 'daily')) if @loggerFile
20
- end
21
-
22
- def setLoggerLevel(loggerLevel)
23
- @stdLogger.level = loggerLevel
24
- @fileLogger.level = loggerLevel if @loggerFile
25
- end
26
-
27
- def log(str, severity=Logger::INFO, app="RUBY-CQHTTP")
28
- @stdLogger.log(severity, str, app)
29
- @fileLogger.log(severity, str, app) if @loggerFile
30
- end
31
-
32
- def httpPost(url, ret)
33
- req = Net::HTTP::Post.new(url.path, { 'Content-Type' => 'application/json' })
34
- req.body = ret
35
- res = Net::HTTP.start(url.hostname, url.port) do |http|
36
- http.request(req)
37
- end
38
- res.body
39
- end
40
- end
41
- end
42
- end
data/lib/ruby-cqhttp.rb DELETED
@@ -1,16 +0,0 @@
1
- require 'faye/websocket'
2
- require 'eventmachine'
3
- require 'json'
4
- require 'event_emitter'
5
- require 'net/http'
6
- require 'uri/ws'
7
- require 'uri'
8
- require 'logger'
9
-
10
- module CQHttp
11
- autoload :Bot, File.expand_path('Bot/Bot', __dir__)
12
- autoload :Api, File.expand_path('Bot/Api', __dir__)
13
- autoload :Utils, File.expand_path('Bot/Utils', __dir__)
14
- end
15
-
16
- CQHttp::Utils.initLogger