Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- WolfTG.lua (библиотека)
- local component = require("component")
- local term = require("term")
- local internet = require("internet")
- local os = require("os")
- local JSON = require("json")
- -- Таблица для хранения состояния
- local WolfTG = {}
- -- Функция для инициализации бота
- function WolfTG.init(telegramToken, chatId)
- WolfTG.telegramToken = telegramToken
- WolfTG.chatId = chatId
- WolfTG.lastUpdateId = 0
- WolfTG.messageList = {}
- WolfTG.exitProgram = false
- end
- -- Функция для получения нового сообщения и добавления в переменную
- function WolfTG.wolftg_receive(variableName)
- local url = string.format(
- "https://api.telegram.org/bot%s/getUpdates?offset=%d&timeout=60&allowed_updates=[\"message\"]",
- WolfTG.telegramToken, WolfTG.lastUpdateId + 1
- )
- local result, response = pcall(function()
- local handle = internet.request(url)
- if not handle then
- return nil
- end
- 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
- return nil
- end
- if decoded and decoded.ok and decoded.result then
- for _, update in ipairs(decoded.result) do
- if update.update_id > WolfTG.lastUpdateId then
- WolfTG.lastUpdateId = update.update_id
- if update.message.text then
- -- Добавляем сообщение в переданную переменную
- _G[variableName] = update.message.text
- end
- end
- end
- end
- end
- -- Функция для отправки сообщения в Telegram с использованием параметров в URL
- function WolfTG.wolftg_receiving(variableName)
- local message = _G[variableName]
- if not message then
- return "Сообщение пустое!"
- end
- -- Формируем правильный URL для отправки сообщения
- local url = string.format(
- "https://api.telegram.org/bot%s/sendMessage?chat_id=%s&text=%s",
- WolfTG.telegramToken,
- WolfTG.chatId,
- internet.urlencode(message) -- Кодируем сообщение для URL
- )
- -- Отправка GET-запроса
- local result, response = pcall(function()
- local handle = internet.request(url)
- if not handle then
- return nil
- end
- local responseData = ""
- for chunk in handle do
- responseData = responseData .. chunk
- end
- return responseData
- end)
- if not result or not response then
- return "Ошибка отправки: " .. (response or "Неизвестная ошибка")
- end
- -- Печатаем ответ от Telegram
- return response
- end
- -- Функция для получения chat_id
- function WolfTG.wolftg_Registerid()
- return WolfTG.chatId
- end
- -- Функция для получения API токена
- function WolfTG.wolftg_registerapi()
- return WolfTG.telegramToken
- end
- -- Возвращаем библиотеку
- return WolfTG
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement