Advertisement
Ewgeniy

Remote Control for ALL

Jun 27th, 2024 (edited)
483
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.69 KB | None | 0 0
  1. local internet = require("internet")
  2. local json = require("json")
  3.  
  4. local botToken = "no"
  5. local lastUpdateId = 0  -- Идентификатор последнего обновления
  6.  
  7. local function telegramGET(chatid, text)
  8.     local url = "https://api.telegram.org/bot" .. botToken .. "/sendMessage"
  9.     local requestBody = "chat_id=" .. chatid .. "&text=" .. text
  10.  
  11.     local response = ""
  12.     local handle, err = internet.request(url, requestBody)
  13.     if not handle then
  14.         return "Error: " .. (err or "unknown error")
  15.     end
  16.  
  17.     for chunk in handle do
  18.         response = response .. chunk
  19.     end
  20.  
  21.     return response
  22. end
  23.  
  24. local function processTelegramMessage(message)
  25.     local command = message.text
  26.  
  27.     -- Ваша логика обработки команд
  28.     if command == "/forward" then
  29. robot.forward()
  30.         -- Реализуйте логику движения робота вперед
  31.         telegramGET(chatID, "Moving forward!")
  32.     elseif command == "/back" then
  33.         -- Реализуйте логику движения робота назад
  34. robot.back()
  35.         telegramGET(chatID, "Moving back!")
  36.     elseif command == "/turnleft" then
  37.         -- Реализуйте логику поворота робота налево
  38. robot.turnLeft()        
  39. telegramGET(chatID, "Turning left!")
  40.     elseif command == "/turnright" then
  41.         -- Реализуйте логику поворота робота направо
  42. robot.turnRight()      
  43.  telegramGET(chatID, "Turning right!")
  44.     else
  45.         telegramGET(chatID, "Unknown command: " .. command)
  46.     end
  47. end
  48.  
  49. local function getUpdates()
  50.     local url = "https://api.telegram.org/bot" .. botToken .. "/getUpdates?offset=" .. (lastUpdateId + 1)
  51.  
  52.     local response = ""
  53.     local handle, err = internet.request(url)
  54.     if not handle then
  55.         print("Error fetching updates: " .. (err or "unknown error"))
  56.         return
  57.     end
  58.  
  59.     for chunk in handle do
  60.         response = response .. chunk
  61.     end
  62.  
  63.     local data = json.decode(response)
  64.     if data.ok then
  65.         for _, update in ipairs(data.result) do
  66.             lastUpdateId = update.update_id  -- Обновляем идентификатор последнего обновления
  67.             processTelegramMessage(update.message)
  68.         end
  69.     else
  70.         print("Error fetching updates: " .. (data.description or "unknown error"))
  71.     end
  72. end
  73.  
  74. -- Цикл для постоянного получения обновлений от Telegram
  75. while true do
  76.     getUpdates()
  77.     os.sleep(1)  -- Пауза между проверками обновлений, чтобы не нагружать систему
  78. end
  79.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement