Logic Machine Forum
LUA scripting using ChatGPT for the LogicMachine - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: LUA scripting using ChatGPT for the LogicMachine (/showthread.php?tid=4780)



LUA scripting using ChatGPT for the LogicMachine - managementboy - 22.05.2023

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!