This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

LUA scripting using ChatGPT for the LogicMachine
#1
Hi everyone,

just tried talking to ChatGPT to ask it to code me a script to get my external IP, store it locally and if there is a change to update my no-ip dyndns for me.

Check out this code it returned on the first try:

Code:
-- Import required libraries
local http = require("socket.http")
local ltn12 = require("ltn12")
local mime = require("mime")
local storage = require("storage")

-- Define your No-IP account details
local username = "your_noip_username"
local password = "your_noip_password"
local hostname = "your_noip_domain"

-- Define the URL for retrieving the external IP address
local ipUrl = "http://api.ipify.org/"

-- Perform the request to retrieve the IP address
local response = {}
local result, status = http.request {
  url = ipUrl,
  method = "GET",
  sink = ltn12.sink.table(response)
}

-- Check the response status
if status == 200 then
  local externalIP = table.concat(response)
  log("External IP address: " .. externalIP)
 
  -- Retrieve the stored IP address from LogicMachine storage
  local storedIP = storage.get("externalip")
 
  -- Compare the retrieved IP with the stored IP
  if storedIP == externalIP then
    log("No change in IP address. No update needed.")
    return
  end
 
  -- Save the updated IP address to LogicMachine storage
  storage.set("externalip", externalIP)
  log("IP address updated and saved to LogicMachine storage.")
 
  -- Continue with No-IP update code
  -- Define the update URL with the retrieved IP address
  local updateUrl = "http://dynupdate.no-ip.com/nic/update?hostname=" .. hostname .. "&myip=" .. externalIP
 
  -- Define the authentication header
  local auth = username .. ":" .. password
  local authHeader = "Basic " .. (mime.b64(auth))

  -- Define the request headers
  local headers = {
    ["Authorization"] = authHeader
  }

  -- Perform the update request
  local updateResponse = {}
  local updateResult, updateStatus = http.request {
    url = updateUrl,
    method = "GET",
    headers = headers,
    sink = ltn12.sink.table(updateResponse)
  }

  -- Check the update response status
  if updateStatus == 200 then
    local responseBody = table.concat(updateResponse)
    log("Update successful. Response from No-IP: " .. responseBody)
  else
    log("Update failed. Status code: " .. updateStatus)
  end
else
  log("Failed to retrieve the external IP address. Status code: " .. status)
end

the only change necessary was removing the
 local storage = require("storage")
line and it runs in a resident scrip without fail!
Reply


Messages In This Thread
LUA scripting using ChatGPT for the LogicMachine - by managementboy - 22.05.2023, 10:38

Forum Jump: