local effil = require 'effil'
local encoding = require 'encoding'
encoding.default = 'CP1251'
u8 = encoding.UTF8
local url = 'юрл вебхука'
local data = {
['content'] = 'text',
['username'] = 'Sended from .lua script!'
}
function main()
while not isSampAvailable() do wait(0) end
sampRegisterChatCommand('ds.msg', function(arg)
data['content'] = u8(arg)
asyncHttpRequest('POST', url, {headers = {['content-type'] = 'application/json'}, data = encodeJson(data)},
function(response)
sampAddChatMessage('[WebHook] [OK] SENDED', -1)
for k, v in pairs(response) do
print(k, v)
end
end,
function(err)
sampAddChatMessage('[WebHook] [ERROR] error: '..err, -1)
end)
end)
wait(-1)
end
function asyncHttpRequest(method, url, args, resolve, reject)
local request_thread = effil.thread(function (method, url, args)
local requests = require 'requests'
local result, response = pcall(requests.request, method, url, args)
if result then
response.json, response.xml = nil, nil
return true, response
else
return false, response
end
end)(method, url, args)
-- Если запрос без функций обработки ответа и ошибок.
if not resolve then resolve = function() end end
if not reject then reject = function() end end
-- Проверка выполнения потока
lua_thread.create(function()
local runner = request_thread
while true do
local status, err = runner:status()
if not err then
if status == 'completed' then
local result, response = runner:get()
if result then
resolve(response)
else
reject(response)
end
return
elseif status == 'canceled' then
return reject(status)
end
else
return reject(err)
end
wait(0)
end
end)
end