package.path = getRakBotPath().."/scripts/libs/?.lua;"..getRakBotPath().."/scripts/libs/?/init.lua;";
package.cpath = getRakBotPath().."/scripts/libs/?.dll";
local json = require('cjson')
local requests = require('requests')
local encoding = require('encoding')
require 'Tasking'
encoding.default = 'CP1251'
local u8 = encoding.UTF8
local settings = {}
local lastPeerId = 0
function onScriptStart()
local f = io.open(getRakBotPath() .. '/scripts/remoteadmin_settings.json', 'r')
settings = json.decode(f:read('*a'))
f:close()
Tasking.new(function()
while true do
Tasking.wait(1000)
local dialogs = vkRequest('messages.getConversations', {}).response.items
for k, dialog in pairs(dialogs) do
local peerId = dialog.conversation.peer.id
local data = vkRequest('messages.getHistory', {
peer_id = peerId,
count = 1
}).response.items[1]
if data.out == 0 and data.text:len() > 0 then
if data.text:sub(1, 1) == '/' then
--[[
if data.text:sub(1, 4) == '/say' then
data.text = data.text:sub(6, data.text:len())
end]]
lastPeerId = peerId
if data.text:find('/di') then
button = data.text:match('/di (%d+)')
vkRequest('messages.send', {
peer_id = lastPeerId,
message = url_encode(u8('Отправлен ответ на диалог button: '..button)),
random_id = 0
})
sendDialog(dId, tonumber(button), -1, '')
else
vkRequest('messages.send', {
peer_id = lastPeerId,
message = url_encode(u8('Принял к исполнению команду.\n✅ '..data.text..'\nОжидайте ответа сервера.')),
random_id = 0
})
sendInput(data.text)
end
break
end
end
end
end
end)
end
function onDialogShow(dialogId, dialogStyle, dialogTitle, okButtonText, cancelButtonText, dialogText)
dId = dialogId
message = '[' .. dialogTitle .. '] \n' .. '\n' .. dialogText .. '\n\nbutton 0: ' .. okButtonText .. '\nbutton 1: ' .. cancelButtonText
local keyboard = vkKeyboard()
keyboard = u8(keyboard)
keyboard = url_encode(keyboard)
-- message = message .. '&keyboard=' .. keyboard
vkRequest('messages.send', {
peer_id = lastPeerId,
message = url_encode(u8(message)),
random_id = 0
})
end
function vkKeyboard() --создает конкретную клавиатуру для бота VK, как сделать для более общих случаев пока не задумывался
local keyboard = {}
keyboard.one_time = false
keyboard.inline = true
keyboard.buttons = {}
keyboard.buttons[1] = {}
local row = keyboard.buttons[1]
row[1] = {}
row[1].action = {}
row[1].color = 'positive'
row[1].action.type = 'text'
row[1].action.payload = '{"button": "status"}'
row[1].action.label = 'Статус'
row[2] = {}
row[2].action = {}
row[2].color = 'negative'
row[2].action.type = 'text'
row[2].action.payload = '{"button": "help"}'
row[2].action.label = 'Помощь'
return json.encode(keyboard)
end
function onServerMessage(message)
if message:find('цензура') or (message:find('заблокирован') and message:find('админом')) then
vkRequest('messages.send', {
peer_id = lastPeerId,
message = message,
random_id = 0
})
end
end
function url_encode(str)
local str = string.gsub(str, "\\", "\\")
local str = string.gsub(str, "([^%w])", char_to_hex)
return str
end
function char_to_hex(str)
return string.format("%%%02X", string.byte(str))
end
function vkRequest(method, args)
args['v'] = 5.131
args['access_token'] = settings.token
local s = ''
for k, v in pairs(args) do
s = s .. string.format('%s=%s&', k, tostring(v))
end
s = u8('?' .. s:sub(1, -2))
while s:find(' ') do
s = s:gsub(' ', '+')
end
local response = requests.post{
url = string.format('https://api.vk.com/method/%s%s', method, s)
}
url = string.format('https://api.vk.com/method/%s%s', method, s)
return json.decode(u8:decode(response.text))
end
function onScriptUpdate()
Tasking.tick()
end