Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- EchoStream.lua
- local component = require("component")
- local internet = require("internet")
- local JSON = require("json")
- local EchoStream = {}
- -- Инициализация бота
- function EchoStream.init(telegramToken)
- EchoStream.telegramToken = telegramToken
- EchoStream.lastUpdateId = 0
- end
- -- Функция для получения сообщения
- function EchoStream.receive()
- local url = string.format(
- "https://api.telegram.org/bot%s/getUpdates?offset=%d&timeout=60&allowed_updates=[\"message\"]",
- EchoStream.telegramToken, EchoStream.lastUpdateId + 1
- )
- print("Запрос для получения сообщений: " .. url)
- local result, response = pcall(function()
- local handle = internet.request(url)
- local responseData = ""
- for chunk in handle do
- responseData = responseData .. chunk
- end
- return responseData
- end)
- if not result or not response then
- return nil
- end
- local decoded, err = JSON:decode(response)
- if err then
- print("Ошибка декодирования JSON: " .. err)
- return nil
- end
- if decoded and decoded.ok and decoded.result then
- for _, update in ipairs(decoded.result) do
- if update.update_id > EchoStream.lastUpdateId then
- EchoStream.lastUpdateId = update.update_id
- if update.message.text then
- print("Получено сообщение: " .. update.message.text)
- return update.message.text
- end
- end
- end
- end
- return nil
- end
- return EchoStream
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement