function createBot(token, onMessage, onDocument) local cjson = require("cjson") local api_url = "https://api.telegram.org/bot" .. token local last_update_id = nil local onMessage = onMessage or function() end local onDocument = onDocument or function() end local function apiRequest(method, parameters) local url = api_url .. "/" .. method local res, header, body if not parameters or next(parameters) == nil then res, header, body = net.curl.get(url) else res, header, body = net.curl.post(url, parameters) end if res then return cjson.decode(body) else print(header, body) end end local function sendMessage(chat_id, text) apiRequest("sendMessage", { chat_id = chat_id, text = text }) end local function getMe() apiRequest("getMe") end local function sendDocument(chat_id, filename, documentPath, caption) local parameters = { chat_id = chat_id, caption = caption, --document = net.curl.form.file(documentPath), } --parameters['_FILE_'..filename] = documentPath --apiRequest("sendDocument", parameters) print("Not implemented yet!") end local function saveDocument(file_id, file_name) local fileInfo = apiRequest("getFile", {file_id = file_id}) if fileInfo and fileInfo.result and fileInfo.result.file_path then local file_path = fileInfo.result.file_path local download_url = "https://api.telegram.org/file/bot" .. token .. "/" .. file_path local res, header, body = net.curl.get(download_url, file_name) return res or header end end local function getUpdates() local updates = apiRequest("getUpdates", { timeout = 100, offset = last_update_id and last_update_id + 1 or nil }) if updates and updates.result then for i, update in ipairs(updates.result) do if update.message then if update.message.document then onDocument(update.message.chat.id, update.message.document, sendMessage, saveDocument) else local chat_id = update.message.chat.id local text = update.message.text onMessage(chat_id, text, sendMessage, sendDocument) end last_update_id = update.update_id end end end end return { getMe = getMe, getUpdates = getUpdates, sendMessage = sendMessage, sendDocument = sendDocument } end return createBot --[[ local bot = createBot(token, function(chat_id, text, sendMessage, sendDocument) -- input text sendMessage(chat_id, "Вы сказали: " .. text) --sendDocument(chat_id, 'autorun', '/romfs/autorun.lua', 'Report') end, function(chat_id, document, sendMessage, saveDocument) -- input file local res, msg = saveDocument(document.file_id, "/downloads/"..document.file_name) sendMessage(chat_id, "Загрузка документа: " .. (res and "успешно" or msg)) end ) while true do bot.getUpdates() thread.sleepms(200) end ]]--