- 356
- 71
- Версия MoonLoader
- Другое
Lua:
local updateid
local effil = require 'effil'
function threadHandle(runner, url, args, resolve, reject)
if not resolve or type(resolve) ~= "function" then
error("The 'resolve' parameter must be a valid function.")
end
if not reject or type(reject) ~= "function" then
error("The 'reject' parameter must be a valid function.")
end
local t = runner(url, args)
if not t then
reject("Failed to create runner thread")
return
end
local get_result = function()
local r = t:get(0)
while not r do
r = t:get(0)
wait(0)
end
return r
end
local status = t:status()
local r = get_result()
if status == 'completed' then
local ok, result = r[1], r[2]
if ok then
resolve(result)
else
reject(result)
end
elseif status == 'canceled' then
reject("Operation was canceled.")
else
reject("Unexpected error: " .. tostring(status))
end
t:cancel(0)
end
function requestRunner()
return effil.thread(function(u, a)
local https = require 'ssl.https'
local ok, result = pcall(https.request, u, a)
if ok then
return {true, result}
else
return {false, result}
end
end)
end
function async_http_request(url, args, resolve, reject)
local runner = requestRunner()
if not reject then reject = function() end end
lua_thread.create(function()
threadHandle(runner, url, args, resolve, reject)
end)
end
function encodeUrl(str)
str = str:gsub(' ', '%+')
str = str:gsub('\n', '%%0A')
return u8:encode(str, 'CP1251')
end
function sendTelegramNotification(msg) -- функция для отправки сообщения юзеру
msg = msg:gsub('{......}', '') --тут типо убираем цвет
msg = encodeUrl(msg) -- ну тут мы закодируем строку
async_http_request('https://api.telegram.org/bot' .. mainIni.Telegram.api_key .. '/sendMessage?chat_id=' .. mainIni.Telegram.chat_id .. '&text='..msg,'', function(result) end) -- а тут уже отправка
end
function get_telegram_updates()
while not updateid do wait(1000) end
while true do
wait(5000)
if mainIni.Telegram.status then
getLastUpdate()
end
end
end
function getLastUpdate()
async_http_request('https://api.telegram.org/bot'..mainIni.Telegram.api_key..'/getUpdates?chat_id='..mainIni.Telegram.chat_id..'&offset=-1','',function(result)
if result then
local proc_table = decodeJson(result)
if proc_table.ok then
if #proc_table.result > 0 then
local res_table = proc_table.result[1]
if res_table then
if res_table.update_id ~= updateid then
updateid = res_table.update_id
if res_table.message and res_table.message.text then
local textTg = u8:decode(res_table.message.text)
-- Обработка команд
if textTg:match('^/qt') then
sampSendChat("/q")
sendTelegramNotification("Игрок вышел из игры.")
elseif textTg:match('^/chat (.+)') then
local sendArg = textTg:match('^/chat (.+)')
sampSendChat(sendArg)
sendTelegramNotification('Вы написали в чат: "'..sendArg..'"')
elseif textTg:match('^/renplayer') then
local text = ""
for _, v in pairs(getAllChars()) do
local result, id = sampGetPlayerIdByCharHandle(v)
if result and id ~= sampGetPlayerIdByCharHandle(PLAYER_PED) then
local nick = sampGetPlayerNickname(id)
local xx,yy,zz = getCharCoordinates(v)
local x,y,z = getCharCoordinates(PLAYER_PED)
local dist = getDistanceBetweenCoords3d(xx, yy, zz, x, y, z)
text = text .. nick .. " Расстояние: " .. dist .. "м\n"
end
end
sendTelegramNotification(text)
end
end
end
end
else
updateid = 1
end
end
end
end)
end
Lua:
function main()
while not isSampAvailable() do wait(0) end
getLastUpdate()
lua_thread.create(get_telegram_updates)
while true do
wait(0)
end
end