Module: Onebot::Utils
Overview
Instance Method Summary collapse
-
#cqEscape(msg) ⇒ String
消息转义 & -> & [ -> [ ] -> ].
-
#cqParse(cqmsg) ⇒ Array
CQ码解析, 将字符串格式转换成 Onebot v11 的消息段数组格式.
-
#cqUnescape(msg) ⇒ String
消息反转义 & -> & [ -> [ ] -> ].
-
#httpPost(url, ret) ⇒ String
(also: #post)
post发包.
Instance Method Details
#cqEscape(msg) ⇒ String
消息转义& -> & [ -> [ ] -> ]
30 31 32 33 34 35 |
# File 'lib/Core/Utils.rb', line 30 def cqEscape(msg) msg.gsub!('&', '&') msg.gsub!('[', '[') msg.gsub!(']', ']') msg end |
#cqParse(cqmsg) ⇒ Array
CQ码解析, 将字符串格式转换成 Onebot v11 的消息段数组格式
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/Core/Utils.rb', line 55 def cqParse(cqmsg) msgary = [] cqary = cqmsg.scan(/\[CQ:(.*?),(.*?)\]/m) isCode = false i = 0 temp = '' cqmsg.each_char do |c| if isCode if c == ']' isCode = false matches = cqary[i] cqcode = { type: matches[0], data: {} } matches[1].split(',').each do |arg| args = arg.split('=') cqcode[:data][args[0].to_sym] = args[1] end msgary << cqcode end elsif c == '[' msgary << { type: 'text', data: { text: cqEscape(temp) } } temp = '' isCode = true else temp << c end end msgary << { type: 'text', data: { text: cqEscape(temp) } } unless temp.empty? msgary end |
#cqUnescape(msg) ⇒ String
消息反转义& -> & [ -> [ ] -> ]
44 45 46 47 48 49 |
# File 'lib/Core/Utils.rb', line 44 def cqUnescape(msg) msg.gsub!('&', '&') msg.gsub!('[', '[') msg.gsub!(']', ']') msg end |
#httpPost(url, ret) ⇒ String Also known as: post
post发包
13 14 15 16 17 18 19 20 |
# File 'lib/Core/Utils.rb', line 13 def httpPost(url, ret) req = Net::HTTP::Post.new(url.path, { 'Content-Type' => 'application/json' }) req.body = ret res = Net::HTTP.start(url.hostname, url.port) do |http| http.request(req) end res.body end |