Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- ComputerCraft 1.7 debug polling bot
- SERVER = "http://YOUR_ROOT_SERVER_IP:8080"
- ID = "minebot1"
- function poll()
- local h = http.get(SERVER .. "/poll?computer_id=" .. ID)
- if not h then
- print("DEBUG: http.get returned nil")
- return nil
- end
- local resp = h.readAll()
- print("DEBUG: RAW RESPONSE:", resp)
- h.close()
- local data = nil
- local ok, err = pcall(function()
- data = textutils.unserialize(resp)
- end)
- if not ok then
- print("DEBUG: Failed to parse response:", err)
- return nil
- end
- print("DEBUG: PARSED DATA:", data)
- return data
- end
- function sendResult(text)
- local payload = textutils.serialize({computer_id = ID, output = text})
- print("DEBUG: Sending result:", payload)
- local ok, err = pcall(function()
- http.post(
- SERVER .. "/result",
- payload,
- { ["Content-Type"] = "application/json" }
- )
- end)
- if not ok then
- print("DEBUG: Failed to send result:", err)
- end
- end
- while true do
- data = poll()
- if data ~= nil and data.command ~= nil then
- print("DEBUG: COMMAND RECEIVED:", data.command)
- local ok, err = pcall(function()
- shell.run(data.command)
- end)
- if ok then
- sendResult("OK: " .. data.command)
- else
- sendResult("ERROR: " .. tostring(err))
- end
- else
- print("DEBUG: No command received")
- end
- sleep(1)
- end
Advertisement
Add Comment
Please, Sign In to add comment