local ansi_decode = {[128] = '\208\130',[129] = '\208\131',[130] = '\226\128\154',[131] = '\209\147',[132] = '\226\128\158',[133] = '\226\128\166',[134] = '\226\128\160',[135] = '\226\128\161',[136] = '\226\130\172',[137] = '\226\128\176',[138] = '\208\137',[139] = '\226\128\185',[140] = '\208\138',[141] = '\208\140',[142] = '\208\139',[143] = '\208\143',[144] = '\209\146',[145] = '\226\128\152',[146] = '\226\128\153',[147] = '\226\128\156',[148] = '\226\128\157',[149] = '\226\128\162',[150] = '\226\128\147',[151] = '\226\128\148',[152] = '\194\152',[153] = '\226\132\162',[154] = '\209\153',[155] = '\226\128\186',[156] = '\209\154',[157] = '\209\156',[158] = '\209\155',[159] = '\209\159',[160] = '\194\160',[161] = '\209\142',[162] = '\209\158',[163] = '\208\136',[164] = '\194\164',[165] = '\210\144',[166] = '\194\166',[167] = '\194\167',[168] = '\208\129',[169] = '\194\169',[170] = '\208\132',[171] = '\194\171',[172] = '\194\172',[173] = '\194\173',[174] = '\194\174',[175] = '\208\135',[176] = '\194\176',[177] = '\194\177',[178] = '\208\134',[179] = '\209\150',[180] = '\210\145',[181] = '\194\181',[182] = '\194\182',[183] = '\194\183',[184] = '\209\145',[185] = '\226\132\150',[186] = '\209\148',[187] = '\194\187',[188] = '\209\152',[189] = '\208\133',[190] = '\209\149',[191] = '\209\151'}
function AnsiToUtf8(s)
local r, b = '', ''
for i = 1, s and s:len() or 0 do
b = s:byte(i)
if b < 128 then
r = r .. string.char(b)
else
if b > 239 then
r = r .. '\209' .. string.char(b - 112)
elseif b > 191 then
r = r .. '\208' .. string.char(b - 48)
elseif ansi_decode[b] then
r = r .. ansi_decode[b]
else
r = r .. '_'
end
end
end
return r
end
function threadHandle(runner, url, args, resolve, reject)
local t = runner(url, args)
local r = t:get(0)
while not r do
r = t:get(0)
wait(0)
end
local status = t:status()
if status == 'completed' then
local ok, result = r[1], r[2]
if ok then
resolve(result)
else
reject(result)
end
elseif err then
reject(err)
elseif status == 'canceled' then
reject(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
newTask(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 sendTG(msg)
msg = msg:gsub('{......}', '')
msg = encodeUrl(msg)
async_http_request('https://api.telegram.org/bot' .. token .. '/sendMessage?chat_id=' .. chatid .. '&text=' .. msg,
'', function(result)
end)
end
function get_telegram_updates()
newTask(function()
while not updateid do
wait(1)
end
local runner = requestRunner()
local reject = function()
end
local args = ''
while true do
url = 'https://api.telegram.org/bot' .. token .. '/getUpdates?chat_id=' .. chatid .. '&offset=-1'
threadHandle(runner, url, args, processing_telegram_messages, reject)
wait(0)
end
end)
end
function processing_telegram_messages(result)
if not result then
return
end
local proc_table = json.decode(result)
if not proc_table.ok then
return
end
local res_table = proc_table.result[1]
if not res_table or res_table.update_id == updateid then
return
end
updateid = res_table.update_id
local message_from_user = res_table.message.text
if not message_from_user then
return
end
local text = u8:decode(message_from_user) .. ' '
print(text)
if text:find('состояние') then
sendTG('Состояние бота на данный момент:\nНик: '..tostring(getBotNick())..'['..tonumber(getBotId())..']'..'\nЗдоровье: '..tonumber(getBotHealth())..'\nИнтерьер: '..tonumber(getBotInterior())..'\nПинг: '..tonumber(getBotPing())..'\nДеньги: $'..tonumber(getBotMoney())..'\nУровень: '..tonumber(getBotScore()))
end
end
function getLastUpdate()
async_http_request('https://api.telegram.org/bot' .. token .. '/getUpdates?chat_id=' .. chatid .. '&offset=-1', '',
function(result)
if result then
local proc_table = json.decode(result)
if proc_table.ok then
if #proc_table.result > 0 then
local res_table = proc_table.result[1]
if res_table then
updateid = res_table.update_id
end
else
updateid = 1
end
end
end
end)
end