Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local internet = require("internet")
- local json = require("json")
- local botToken = "no"
- local lastUpdateId = 0 -- Идентификатор последнего обновления
- local function telegramGET(chatid, text)
- local url = "https://api.telegram.org/bot" .. botToken .. "/sendMessage"
- local requestBody = "chat_id=" .. chatid .. "&text=" .. text
- local response = ""
- local handle, err = internet.request(url, requestBody)
- if not handle then
- return "Error: " .. (err or "unknown error")
- end
- for chunk in handle do
- response = response .. chunk
- end
- return response
- end
- local function processTelegramMessage(message)
- local command = message.text
- -- Ваша логика обработки команд
- if command == "/forward" then
- robot.forward()
- -- Реализуйте логику движения робота вперед
- telegramGET(chatID, "Moving forward!")
- elseif command == "/back" then
- -- Реализуйте логику движения робота назад
- robot.back()
- telegramGET(chatID, "Moving back!")
- elseif command == "/turnleft" then
- -- Реализуйте логику поворота робота налево
- robot.turnLeft()
- telegramGET(chatID, "Turning left!")
- elseif command == "/turnright" then
- -- Реализуйте логику поворота робота направо
- robot.turnRight()
- telegramGET(chatID, "Turning right!")
- else
- telegramGET(chatID, "Unknown command: " .. command)
- end
- end
- local function getUpdates()
- local url = "https://api.telegram.org/bot" .. botToken .. "/getUpdates?offset=" .. (lastUpdateId + 1)
- local response = ""
- local handle, err = internet.request(url)
- if not handle then
- print("Error fetching updates: " .. (err or "unknown error"))
- return
- end
- for chunk in handle do
- response = response .. chunk
- end
- local data = json.decode(response)
- if data.ok then
- for _, update in ipairs(data.result) do
- lastUpdateId = update.update_id -- Обновляем идентификатор последнего обновления
- processTelegramMessage(update.message)
- end
- else
- print("Error fetching updates: " .. (data.description or "unknown error"))
- end
- end
- -- Цикл для постоянного получения обновлений от Telegram
- while true do
- getUpdates()
- os.sleep(1) -- Пауза между проверками обновлений, чтобы не нагружать систему
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement