script_name('Telegram Notifications Source')
script_authors('ronnyscripts, ronny_evans')
local effil = require("effil")
local encoding = require("encoding")
encoding.default = 'CP1251'
u8 = encoding.UTF8
chat_id = ''
token = ''
local updateid
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
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' .. token .. '/sendMessage?chat_id=' .. chat_id .. '&text='..msg,'', function(result) end)
end
function get_telegram_updates()
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='..chat_id..'&offset=-1'
threadHandle(runner, url, args, processing_telegram_messages, reject)
wait(0)
end
end
function calc(str)
return assert(load("return "..str))()
end
function processing_telegram_messages(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
local message_from_user = res_table.message.text
if message_from_user then
local text = u8:decode(message_from_user) .. ' '
if text:match('^!qq') then
sendTelegramNotification('Ку')
elseif text:match('^!q') then
sendTelegramNotification('Привет!')
elseif text:match('^!stats') then
sendTelegramNotification('Это тестовая версия блин')
elseif text:match('^!calc') then
local arg = text:gsub('!calc ','',1)
if #arg > 0 then
local result_calc = calc(arg)
if result_calc then
sendTelegramNotification('Вы ввели пример: '..arg..'\nОтвет: '..result_calc)
else
sendTelegramNotification('Неверный пример!')
end
else
sendTelegramNotification('Эм, ты не ввел аргумент')
end
else
sendTelegramNotification('Неизвестная команда!')
end
end
end
end
end
end
end
end
function getLastUpdate()
async_http_request('https://api.telegram.org/bot'..token..'/getUpdates?chat_id='..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
updateid = res_table.update_id
end
else
updateid = 1
end
end
end
end)
end
function main()
while not isSampAvailable() do
wait(0)
end
getLastUpdate()
sampRegisterChatCommand('telegram',function()
sampAddChatMessage('[Telegram] Отправляю тестовое сообщение',-1)
sendTelegramNotification('Тестовое сообщение от '..sampGetPlayerNickname(select(2, sampGetPlayerIdByCharHandle(PLAYER_PED))))
end)
lua_thread.create(get_telegram_updates)
while true do
wait(0)
end
end